1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

the sum and average of given numbers using Arrays

Discussion in 'C++' started by ashleykathy, Feb 7, 2010.

  1. ashleykathy

    ashleykathy New 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!
     
  2. Deadly Ghos7

    Deadly Ghos7 New Member

    Ok here it is:
    Code:
    #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++.
     
  3. murugaperumal

    murugaperumal New 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);
    }
    
     

Share This Page