Sunday, July 31, 2011

Define a pointer to an integer; read a list of n numbers using dynamic memory allocation and find average of these numbers

"
#include<stdio.h>
#include<conio.h>

void main()
{
 int *i,count=1,x=1,sum=0;
 //Enter 0 to terminate input
 i=(int *)malloc(sizeof(int));
 scanf("%d",i);
 sum+=*i;
 while(i[count-1]!=0)
 {
 i=(int *)realloc(i,sizeof(int));
 scanf("%d",&i[count]);
 sum+=i[count];
 count++;
}
count--;
printf("\nAverage is: %d",sum/count);
 getch();
}
 "

No comments:

Post a Comment