Author Topic: Sum, Average, Min and Max for N numbers  (Read 1603 times)

kathir

  • Administrator
  • Sr. Member
  • *****
  • Posts: 283
Sum, Average, Min and Max for N numbers
« on: November 10, 2011, 07:52:06 pm »
Could you please help me out in turbo C.I need to make program which asks user to input how many numbers he would like to input using malloc to create array,q loop to read the numbers and calculate

1)total
2)average
3)minimum
4)maximum

Many Thanks,



Code:
//Source Code Listing


#include <stdio.h>
#include <malloc.h>

void main()
{
   int i, n, sum = 0, min = 0, max = 0;
   double average = 0;
   int *pNumbers = 0;

   printf("Enter the total numbers:");
   scanf("%d", &n);
   pNumbers = (int*) malloc(n * sizeof(int));

   for(i = 0; i < n; i++)
   {
      printf("Enter %d No.: ", i + 1);
      scanf("%d", &pNumbers[i]);
   }

   min = max = pNumbers[0];
   for(i = 0; i < n; i++)
   {
      sum += pNumbers[i];
      if(min > pNumbers[i])
         min = pNumbers[i];
      if(max < pNumbers[i])
         max = pNumbers[i];
   }
   average = (double) sum / n;

   printf("\nSum\t: %d", sum);
   printf("\nAverage\t: %.2lf", average);
   printf("\nMin\t: %d", min);
   printf("\nMax\t: %d", max);

   free(pNumbers);

// output is given below:
/***
Enter the total numbers: 5

Enter 1 No.: 10
Enter 2 No.: 11
Enter 3 No.: 101
Enter 4 No.: 1001
Enter 5 No.: 10001

Sum     : 11124
Average : 2224.80
Min     : 10
Max     : 10001

***/

}
« Last Edit: December 02, 2011, 01:01:23 pm by kathir »

 


Disclaimer: This web site is for educational and informational purposes only. Click here to read the Disclaimer.
Content copyright 2010-2014. Kathiresan. All rights reserved.
This page is using SMF version 2.0.1