/* * To compute the avarage of the elements of an array using pointers * @author-Karan Narain(karan@iitk.ac.in) * */ #include void avg(int *p) { int *q,sum=0,rounded_avg,i=0; float av; q=p+1; while(*q!=0) { sum=sum + *q; i++; q++; } av=((float)sum)/i; rounded_avg=av; //here,rounded_avg would hold the integer(truncated) value of av if(av-rounded_avg>=0.5) //if decimal part is >=0.5 rounded_avg=rounded_avg+1; //store the average in A[0] using p (as p points to A[0]) *p=rounded_avg; printf("\nThe rounded average is %d\n",*p); return; } int main() { int A[10],i=1,temp,len,*p=NULL; //len holds the number of elements in array A.It would be equal to the last value of i before the while loop ends or breaks while(i<10) { printf("Enter a positive element(To stop entering,enter a zero)\n"); scanf("%d",&temp); if(temp==0) { A[i]=temp; break; } else if(temp<0) continue; //continue will take the control back to while(i<10) for a new iteration,i.e. another element will be input else { A[i]=temp; i++; } } len=i; //if no element was entered if(len==1) printf("The array has no elements\n"); else { printf("The array you entered is :\n"); for(i=1;i