Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 131,679 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,466 people online right now. Registration is fast and FREE... Join Now!




Errors in Max & Average Program

 
Reply to this topicStart new topic

Errors in Max & Average Program

sknox1
post 12 Oct, 2006 - 08:40 PM
Post #1


New D.I.C Head

Group Icon
Joined: 30 Sep, 2006
Posts: 29



Dream Kudos: 150
My Contributions


CODE
int main()

{    int j, n;
     double max, sum;
int vector;


     cout << "Program to process real numbers." << endl;
     cout << "Enter number of reals: ";
     cin >> n;

     vector<int> a(n);            /* declare vector of n values,
                                       with subscripts 0, ..., n-1  */
     cout << "Enter " << n << " numbers: ";
     for (j = 0; j < n; j++)
         cin >> a[j];           /* subscripts given in brackets [ ] */

     sum =  max =  a[0];  /* right to left assignment operator */

     for (j = 1; j < n; j++)
       { if (a[j] > max)
            max = a[j];  
     else
max = a[0];
        
       }

     cout << "Maximum:  " << max << endl;
    
     cout << "Average:  " << sum/n << endl << endl;

    
     return 0;

can anybody help me output the max and the average in this program?

Admin Edit: added [code] tags ~skyhawk133
User is offlineProfile CardPM

Go to the top of the page


gregoryH
post 13 Oct, 2006 - 12:26 AM
Post #2


D.I.C Regular

Group Icon
Joined: 4 Oct, 2006
Posts: 417



Dream Kudos: 50
My Contributions


should the sum be set to 0 before starting to loop through the array?
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 13 Oct, 2006 - 04:21 AM
Post #3


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,163



Thanked 32 times

Dream Kudos: 25
My Contributions


Please describe the difficulties that you have encountered - any errors, unexpected behaviour, items like that.
User is online!Profile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/20/08 07:37AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month