Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

populating an array, adding the numbers and averaging them?

Posted on 2004-08-25
22
Medium Priority
?
241 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
22 Comments
 
LVL 11

Expert Comment

by:avizit
ID: 11899219
what does your line

game ;

accomplish ?

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

Expert Comment

by:avizit
ID: 11899227
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
ID: 11899228
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:gillgates
ID: 11899231
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
ID: 11899237
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
ID: 11899243
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
ID: 11899261
ok.. I have made the changes suggested...  Please continue?

PB
0
 
LVL 11

Expert Comment

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

0
 

Author Comment

by:Pres_Bush
ID: 11899282
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
ID: 11899289
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
ID: 11899299
ok.. thanks.. .I'll work on that tonight and repost what I have later...  Thanks again!!
0
 
LVL 11

Expert Comment

by:avizit
ID: 11899303
 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
ID: 11899654
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
ID: 11899665
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
ID: 11899669
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
ID: 11899672
paste what error you are getting ?
0
 

Author Comment

by:Pres_Bush
ID: 11899746
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
ID: 11899762
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 2000 total points
ID: 11899766
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
ID: 11899791
oh yeah!  thats right!!  absent minded I guess...


 

0
 

Author Comment

by:Pres_Bush
ID: 11899834
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
ID: 11899846
Thank you :)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

598 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