it keep give in me the same result ! (image)

hi there ..

i tried to create program to calculate average of n numbers ..

and when i run it , it keep give in me 0 no matter what ..

the code ..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>

using namespace std; //program to calculate average of n numbers

int main()

{
    int n,ctr;
    float x,sum,avg;
    ctr= 0;
    sum=0.0;
    cout<<"Enter N"<<endl;
    cin>>n;
   
   while(ctr!=n)// we used while loop
   {
                cout<<"Enter x"<<endl;
                cin>>x;
                ctr++;
                sum=sum+x;
                }

    cout<<"The average is "<<avg<<endl;
    avg=sum/n;
    
    system ("PAUSE");
    
    return 0; //END OF THE PROGRAM
}


see the result ..

http://i.imgur.com/hv1y3tM.png

help me :(
you never set
avg
equal to something when you are printing it out. you have to put
avg=sum/n
infront of
cout<<"The average is "<<avg<<endl;
Last edited on
thanks work oyt great
Last edited on
glad to help.. (:
Topic archived. No new replies allowed.