the sum and average of given numbers using Arrays

ashleykathy's Avatar
Newbie Member
"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!
0
Deadly Ghos7's Avatar, Join Date: Dec 2009
Contributor
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++.
0
murugaperumal's Avatar, Join Date: Feb 2010
Go4Expert Member
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);
}