Write a function in C to dynamically allocate using mallocca

Write a function in C to dynamically allocate (using malloc/calloc) a buffer of a user specified number of double precision numbers. Ensure user does not try to allocate more than 1,000,000 numbers. Write another function in C to randomly fill buffer, and another to average these numbers.

Solution

#include<stdio.h>
int main()
{
int average;
   buffer();
   average=randombuffer();
   printf(\"\ Average of randomly filled numbers:%d\",average);
      
}

void buffer()
{
double num;
printf(\"\ Enter the user specified number:\ \");
scanf(\"%lf\",&num)
if(num > 1000000)
{
printf(\"\ You exceeded the maximum size:Entering Default Value 500000\");
num=500000;
}
else
{
double *ptr;
ptr = (double *)malloc(sizeof(double)*num);
if (ptr_one == 0)
{
           printf(\"ERROR: Out of memory\ \");
           return 1;
}
*ptr=50;
printf(\"Buffer Generated successfully,Printing Assigned value in buffer:%lf\ \", *ptr);
free(ptr);
}
return 0;
}

int randombuffer()
{
int i,sum=0,avg;
uint8_t size = 1000000;
uint8_t* buffer = malloc (size*sizeof(uint8_t));
for (i=0; i<size; i++)
{
buffer[i]=(rand()%100)+1;
}
printf(\"Content of buffer = %d\ \", buffer);
for (i = 0; i < size; i++)
{
sum+=buffer[i];
printf(\"%d\ \", buffer[i]) ;
}
avg=averagebuffer(sum,size);
return avg;
}

int averagebuffer(int sum,int size)
{
int average;
average=sum/size;
return average;
}

Write a function in C to dynamically allocate (using malloc/calloc) a buffer of a user specified number of double precision numbers. Ensure user does not try to
Write a function in C to dynamically allocate (using malloc/calloc) a buffer of a user specified number of double precision numbers. Ensure user does not try to

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site