Sunday, September 03, 2006

Calculation of Sum, Average and Standard Deviation using Functions and Pointers.

Write a function that receives 5 integers and returns the sum, average and standard
deviation of these numbers. Call this function from main() and print the results in main().


int calc (float a, float b, float c, float d, float e, float *sum, float *avg,float *sd);

int main()
float a, b, c, d, e, sum=0.0, avg=0.0;

float sd=0.0;

printf("Enter Five Numbers:");
scanf("%f %f %f %f %f",&a,&b,&c,&d,&e);

calc (a, b, c, d, e, &sum, &amp;avg, &sd);

printf("\nSum=%f", sum);
printf("\nAverage=%f", avg);

printf("\nStandard Deviation=%f\n", sd);


return 0;

calc (float a, float b, float c, float d, float e, float *sum, float *avg, float *sd)

float Calc=0.0;

*sum = a+b+c+d+e;

*avg = *sum / 5.0;

Calc += ( a - *avg) * ( a - *avg);

Calc += ( b - *avg) * ( b - *avg);

Calc += ( c - *avg) * ( c - *avg);
Calc += ( d - *avg) * ( d - *avg);

Calc += ( e - *avg) * ( e - *avg);

*sd = sqrt((double)Calc/5.0);


Notes for calculation of Standard Deviation:

The program can be found at:
Download File

Calculation of Product of Two Numbers using Function - Returns a Float

This program seems to be rather simple. But there's one little thing to be noted in this particular program. The thing is that the function in this program returns a float. The function declaration is usually given outside main..but due to some other standards that I am following, I have prototyed it inside main..but that doesn't cause much of a difference in this simple program.

Write a function which receives a float and an int from main(), finds the product
of these two and returns the product which is printed through main().


int i;
float j, prod;
float product (int x, float y);

printf("Enter the i(int) and j(float):");
scanf ("%d %f", &i, &j);

prod = product(i,j);

printf("Product:%f", prod);


product (int x, float y)

float product;
product = x*y;
return (product);


The program can be found at:
Download File.

C Program - Calculation of Area and Circumference of a Circle using Pointers

The following program is one good example that illustrates how we can return more than one value in a function. The answer is certainly using Pointers. The following program demonstrates the method.

Write a function that calculates both Area and Perimeter/ Circumference of the Circle, whose Radius is
entered through the keyboard.


int radius;
float area, perimeter;

printf("\nEnter radius of a circle:");

scanf ("%d", &radius);
areaperi (radius, &area, &perimeter);

printf("Area=%f", area);
printf("\nPerimeter=%f", perimeter);


areaperi(int r, float *a, float *p)



//This Program exhibits the use of Call By Reference.

The program can be found at:
Download File.