Solved

# populating an array, adding the numbers and averaging them?

Posted on 2004-08-25
227 Views
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
Question by:Pres_Bush

LVL 11

Expert Comment

game ;

accomplish ?

i guess you can remove those as a first step
0

LVL 11

Expert Comment

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

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

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

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

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

PB
0

LVL 11

Expert Comment

showme what code you have now .. remove all commented out code ..then we can see whats wrong now.

0

Author Comment

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

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

ok.. thanks.. .I'll work on that tonight and repost what I have later...  Thanks again!!
0

LVL 11

Expert Comment

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

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.

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

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

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

paste what error you are getting ?
0

Author Comment

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

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

avizit earned 500 total points
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

oh yeah!  thats right!!  absent minded I guess...

0

Author Comment

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

Thank you :)
0

## Featured Post

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++.