Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions hw3.c
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
#include <stdio.h>
#include <stdlib.h>

void allocate_memory(double ** buf, long N)
int allocate_memory(double ** buf, long N) /* Returns a 0 on success, 1 on failed */
{
if (N>1000000)
{
printf("Tried to hog up too much memory!\n");
exit(0);
}
* buf=(double *)malloc(N*sizeof(double));
else
{
* buf=(double *)malloc(N*sizeof(double));
if (*buf==NULL)
{
printf("Allocation failed\n");
exit(0);
}
else
{
return 0; /* Only returned for N<=1000000 and *buf != NULL */
}
return 1; /* Defaults to returning a failed 1 for N>1000000 or Allocation failed - could later distinguish these! */
}

}
void gen_random_numbers(double *buf,long N)
{
Expand Down Expand Up @@ -41,10 +48,12 @@ int main(int argc, char *argv[])
exit(0);
}
N=atoi(argv[1]);
allocate_memory(&buffer, N);
if (allocate_memory(&buffer, N) == 0) /* Updated code here so that following steps only run on successful allocation */
{
gen_random_numbers(buffer,N);
average=compute_average(buffer,N);
printf("The average we have been waiting on is %f\n",average);
}
return(0);
}