Solved

populating an array, adding the numbers and averaging them?

Posted on 2004-08-25
22
227 Views
Last Modified: 2010-04-01
Greetings again,
  I am now trying to create an array, populate it with golf scores, and then pull them back out, add them and then give an average.  I can do it without an array.. but .. when I put it in an array.. I get bogus information.  Please help!!!  


Thanks,
PB

I'm trying to do this with an array but I don't think the array is doing anything.

This is what I have so far..

//Listing 11.4 - An array of objects

#include <iostream>
#include <math.h>
using namespace std;

struct GOLF
{
};
int main()
{


const int size=10;


int game1, game2, game3, game4, game5, game6, game7, game8, game9, game10;
int game[size] = {game1, game2, game3, game4, game5, game6, game7, game8, game9, game10};    

game;
cout << "please enter score for Game # 1?";
cin >> game1;

game;
cout << "please enter score for Game # 2?";
cin >> game2;

game;
cout << "please enter score for Game # 3?";
cin >> game3;

game;
cout << "please enter score for Game # 4?";
cin >> game4;

game;
cout << "please enter score for Game # 5?";
cin >> game5;

game;
cout << "please enter score for Game # 6?";
cin >> game6;

game;
cout << "please enter score for Game # 7?";
cin >> game7;

game;
cout << "please enter score for Game # 8?";
cin >> game8;

game;
cout << "please enter score for Game # 9?";
cin >> game9;

game;
cout << "please enter score for Game # 10?";
cin >> game10;
   
    //int game [10];
    int i;
 //game=i;
    for (int i = 1; i < size; ++i)
    game[++i];        //if (min > game[i];
    //min = game[i];
    //return 0;


cout << "\nGame # 1: "<<game1;
cout << ", 2: "<<game2;    
cout << ", 3: "<<game3;    
cout << ", 4: "<<game4;    
cout << ", 5: "<<game5;    
cout << ", 6: "<<game6;    
cout << ", 7: "<<game7;    
cout << ", 8: "<<game8;    
cout << ", 9: "<<game9;    
cout << ", 10: "<<game10;        

cout << game;


//float Average;
float Average2;
double score =game1 + game2 + game3 + game4 + game5 + game6 + game7 + game8 + game9 + game10;            
cout<< "\nYour scores are: "<<score ;

Average2 = score/10;
cout <<"\nYour average score is: "<<Average2;
        return 0;
}                
0
Comment
Question by:Pres_Bush
22 Comments
 
LVL 11

Expert Comment

by:avizit
Comment Utility
what does your line

game ;

accomplish ?

i guess you can remove those as a first step
0
 
LVL 11

Expert Comment

by:avizit
Comment Utility
secondly

nt game1, game2, game3, game4, game5, game6, game7, game8, game9, game10;
int game[size] = {game1, game2, game3, game4, game5, game6, game7, game8, game9, game10};    

is not really necessary

you can just have

int game[size] ;

and then you can refer to them as

game[0] ...game[9]

e.g
cout << "please enter score for Game # 8?";
cin >> game[7];  

//remember the subscripts are from 0  to 9




0
 

Author Comment

by:Pres_Bush
Comment Utility
ok.. thanks... they were my first attempt at pointing to the array.  I'm a bit confused (as you can see from my code).  

Thanks.
pb
0
 
LVL 3

Expert Comment

by:gillgates
Comment Utility
int game[size] = {game1, game2, game3, game4, game5, game6, game7, game8, game9, game10};    

cout << "please enter score for Game # 1?";
cin >> game1;


Doing this doesn't put the value for game1 into the array... You have to do

cin >> game[1];
0
 
LVL 11

Expert Comment

by:avizit
Comment Utility
and the error junk you are getting is due to thsi line

 for (int i = 1; i < size; ++i)


remember the susbscripts are from 0 - 9

hence you should have

for ( int i = 0 ; i < size ; ++i )


0
 
LVL 11

Expert Comment

by:avizit
Comment Utility
also


 game[++i];  

this line also doesnt really do anything

you are just incrementing i in this line but not assigning any value to game[i] etc

so the value of game doesnt get changed.

0
 

Author Comment

by:Pres_Bush
Comment Utility
ok.. I have made the changes suggested...  Please continue?

PB
0
 
LVL 11

Expert Comment

by:avizit
Comment Utility
showme what code you have now .. remove all commented out code ..then we can see whats wrong now.

0
 

Author Comment

by:Pres_Bush
Comment Utility
ok.. here is what I have done so far... Again... Thank you both !!



#include <iostream>
#include <math.h>
using namespace std;

struct GOLF
{
};
int main()
{


const int size=10;


int game[size];

cout << "please enter score for Game # 1?";
cin >> game[0];


cout << "please enter score for Game # 2?";
cin >> game[1];


cout << "please enter score for Game # 3?";
cin >> game[2];


cout << "please enter score for Game # 4?";
cin >> game[3];


cout << "please enter score for Game # 5?";
cin >> game[4];


cout << "please enter score for Game # 6?";
cin >> game[5];


cout << "please enter score for Game # 7?";
cin >> game[6];


cout << "please enter score for Game # 8?";
cin >> game[7];


cout << "please enter score for Game # 9?";
cin >> game[8];


cout << "please enter score for Game # 10?";
cin >> game[9];
   

    int i;

    for (int i = 1; i < size; ++i)


cout << "\nGame # 1: "<<game[size];



float Average2;
double score =game[0] + game[1] + game[2] + game[3] + game[4] + game[5] + game[6] + game[7] + game[8] + game[9];            
cout<< "\nYour scores are: "<<score ;

Average2 = score/10;
cout <<"\nYour average score is: "<<Average2;
        return 0;
}                
0
 
LVL 11

Expert Comment

by:avizit
Comment Utility
okay i wil give you a hint to make it simpler


int game[10] ;

now read all the game values in a for loop ( thats why for loops are easier youdont need to write the same cin<< game ... 10 times )

sum all the values

yu can do it in a separate for loop  or you can also add to a variable as the user enters the numbers

divide the sum by size and you have the average

0
 

Author Comment

by:Pres_Bush
Comment Utility
ok.. thanks.. .I'll work on that tonight and repost what I have later...  Thanks again!!
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 11

Expert Comment

by:avizit
Comment Utility
 for (int i = 1; i < size; ++i)
   cout << "\nGame # 1: "<<game[size];


this is wrong  to display all the differentgames you will have to use i inside the forloop

as in

 for (int i = 1; i < size; ++i){
        cout<<"\ngame#"<< i << game[i];
}

now two things
1. it is advisable to use  {} for for loops , if thenelse etc so you can easily find out the limits of a block of code
2. you were outputting   games : 1 .. so your 1 was fixed. and all the time you were displaying game[size] i.e game[10]

so one value all the time



0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
FYI:
 for (int i = 1; i < size; ++i){
       cout<<"\ngame#"<< i << game[i];
}

When ever you see a for loop starting with a 1, and using a < for the condition statement, that's usually an indication of erroneous code logic.

Most for loop start with a zero
for (int i = 0; i < size; ++i){
       cout<<"\ngame#"<< i+1 << game[i];
}

This is especially so if you're using the for-loop variable as an index to an array.

You could also do it as the following:
for (int i = 1; i <= size; ++i){
       cout<<"\ngame#"<< i << game[i-1];
}

But IMHO, the first method is cleaner, and uses a more common format for C/C++ zero based coding.

0
 

Author Comment

by:Pres_Bush
Comment Utility
ok.. I'm getting an error on this part"cout<<"\ngame#"<< i << game[i];"  it doesn't liek the <<i<< part.  I am thinking that it is supose to show the number of game that the score is for.  Any suggestions?
0
 
LVL 11

Expert Comment

by:avizit
Comment Utility
sorry i intented to start from 0 ..i copy pasted his code into mine without correction.

paid more attention on cout << "\nGame # 1: "<<game[size]; vs cout<<"\ngame#"<< i+1 << game[i];  part

I have actually pointed out the same error in my 3rd( ?) reply.

0
 
LVL 11

Expert Comment

by:avizit
Comment Utility
paste what error you are getting ?
0
 

Author Comment

by:Pres_Bush
Comment Utility
sorry .. I figured it out on my own.  My mistake...

Next question... How do I get it to all print on one line? (the game numbers)

:-)
Thanks for being so helpful (but still allowing me to learn).

:-)

pb
0
 
LVL 11

Expert Comment

by:avizit
Comment Utility
when you print "\n " or endl it prints a newline

remove t he "\n"'s from the couts..

replace

for (int i = 0; i < size; ++i){
       cout<<"\ngame#"<< i+1 << game[i];
}

for (int i = 0; i < size; ++i){
       cout<<"game#"<< i+1 << game[i];
}

cout<<endl;


to

0
 
LVL 11

Accepted Solution

by:
avizit earned 500 total points
Comment Utility
i mean change

for (int i = 0; i < size; ++i){
       cout<<"\ngame#"<< i+1 << game[i];
}



TO

for (int i = 0; i < size; ++i){
       cout<<"game#"<< i+1 << game[i];
}
cout<<endl;
0
 

Author Comment

by:Pres_Bush
Comment Utility
oh yeah!  thats right!!  absent minded I guess...


 

0
 

Author Comment

by:Pres_Bush
Comment Utility
This was the best help I have ever recieved using Experts-Exchange.  Great job!  I would have given you more points if I could!!

Thanks Again
Pres_Bush
0
 
LVL 11

Expert Comment

by:avizit
Comment Utility
Thank you :)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now