Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   the sum and average of given numbers using Arrays (http://www.go4expert.com/forums/sum-average-using-arrays-t20900/)

ashleykathy 7Feb2010 15:04

the sum and average of given numbers using Arrays
 
"Write a program in C++ to find the sum and average of given numbers using Arrays."

Please help me find out the source code in C++! please!

Deadly Ghos7 7Feb2010 20:45

Re: the sum and average of given numbers using Arrays
 
Ok here it is:
Code: cpp

#include <iostream>
#include <conio.h>

using namespace std;

int main (int argc, char *argv[])
{
    int num[100];
    int n;
    register int i;
    int sum = 0;
    float avg;
    cout << "Enter number of items in array: ";
    cin >> n;
    for (i = 0; i < n; i++)
    {
        cout << "Enter the " << i+1 << "th number: ";
        cin >> num[i];
        sum += num[i];
    }
    avg = (float) sum/n;
    cout << "Sum is " << sum << "\nAnd Average = " << avg;
    getch();
}


But if you wanna learn programming for yourself, try coding it yourself. I have given you the program, get the idea from it and try to write your code yourself.
Anyway, its my first try on C++, had never coded in C++.

murugaperumal 26Feb2010 09:58

Re: the sum and average of given numbers using Arrays
 
Dear Friend,

The following coding to calculate the sum and average of an array element using C program.

Code:

#include<stdio.h>
main ()
{
    int array[100];
    int n,i,sum=0;
    float avg;
    printf("Enter number of elements in array:\n");
  scanf("%d",&n);
  for (i = 0; i < n; i++)
    {
        scanf("%d" ,&array[i]);
        sum += array[i];
    }
    avg = (float) sum/n;
 printf("The sum is :%d\n",sum);
 printf("The sum is :%f\n",avg);
}



All times are GMT +5.5. The time now is 06:28.