1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

the sum and average of given numbers using Arrays

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

  1. ashleykathy

    ashleykathy New Member

    Joined:
    Feb 7, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    "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

    Joined:
    Dec 19, 2009
    Messages:
    55
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Earth
    Home Page:
    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

    Joined:
    Feb 20, 2010
    Messages:
    15
    Likes Received:
    1
    Trophy Points:
    0
    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