Question on Arrays

Apr 24, 2012 at 10:01pm
So I need help on array problems and i have to ask the user for their name, phone number, 5 test scores, the average of their test scores and show in the output their lowest and highest scores. So far I have this:



#include <iostream>
#include <string>
using namespace std;

int main()
{

string name;
string pnumber;
string address;
double scores[4];
int i;
double avg;
int highest = 100, lowest= 0.0;

cout << "What is your name? ";
getline(cin, name);


cout <<"What is your phone number? ";
cin >> pnumber;

//get input
for (i=0;i<=4; i++)
{
cout << "\nPlease enter your scores: ";
cin >> scores[i];
}

avg = (scores[1]+scores[2]+scores[3]+scores[4]+scores[5]) ;



cout << "The average score is:" << avg << endl;



return 0;
}


Ive tried finding the highest and lowest and have had success whatsoever!

Can someone tell me what to do? THANKS!
Apr 24, 2012 at 10:04pm
It is invalid loop

for (i=0;i<=4; i++)

because you are entering 5 values while your array has only 4 elements

le scores[4];

Also you forgot to enter the address.:)
Last edited on Apr 24, 2012 at 10:20pm
Apr 24, 2012 at 10:13pm
Ok so now I have this:


#include <iostream>
#include <string>
using namespace std;

int main()
{

string name;
string pnumber;
string address;
double scores[5];
int i;
double avg;
int highest = 100, lowest= 0.0;

cout << "What is your name? ";
getline(cin, name);

cout << "What is your address? ";
getline(cin, address)


cout <<"What is your phone number? ";
cin >> pnumber;

//get input
for (i=0;i<5; i++)
{
cout << "\nPlease enter your scores: ";
cin >> scores[i];
}

avg = (scores[1]+scores[2]+scores[3]+scores[4]+scores[5]) ;



cout << "The average score is:" << avg << endl;



return 0;
}


but can you please tell me why my average is inaccurate and tell me how to get the highest and lowest test scores to appear on the output??

Thanks!
Apr 24, 2012 at 10:23pm
It is not an average

avg = (scores[1]+scores[2]+scores[3]+scores[4]+scores[5]) ;

As far as I know the average will be

avg = (scores[0]+scores[1]+scores[2]+scores[3]+scores[4]) / 5 ;

And I have not understood what is the problem with highest and lowest scores?

And what do mean the following declarations?

int highest = 100, lowest= 0.0;
Last edited on Apr 24, 2012 at 10:24pm
Apr 24, 2012 at 10:30pm
Oh ok that seems to make sense! And what I was talking about is out of the 5 test scores entered my the user, the program will return the highest and lowest test scores.

Can you please tell me how to do that?
Apr 24, 2012 at 10:33pm
1
2
3
4
5
6
7
highest = lowest = scores[0];

for ( int i = 1; i < 5; i++ )
{
   if ( a[i] < lowest ) lowest = a[i];
   if ( highest < a[i] ) highest = a[i];
}
Apr 25, 2012 at 12:59am
So I did what you said and my program is as follows:



#include <iostream>
#include <string>
using namespace std;

int main()
{
string name;
string pnumber;
double scores[5];
double avg = 0.0;
double highest = 0.0, lowest= 0.0;

cout << "What is your name? ";
getline(cin, name);

cout <<"What is your phone number? ";
cin >> pnumber;

//Get Input
for (int i = 1; i <= 5; i++)
{
cout << "Please enter score " << i << ": ";
cin >> scores[i];
avg += scores[i];

for ( int i = 1; i < 5; i++ )
{
if ( scores[i] < lowest ) lowest = scores[i];
if ( highest < scores[i] ) highest = scores[i];
}
}
avg /= 5;

cout << "The highest score is: " << highest << endl;
cout << "The lowest score is: " << lowest << endl;
cout << "The average score is: " << avg << endl;

return 0;
}


and when I enter the test scores it comes out like this:


What is your name? Bob
What is your phone number? 555-555-5555
Please enter score 1: 1
Please enter score 2: 2
Please enter score 3: 3
Please enter score 4: 4
Please enter score 5: 5
The highest score is: 1.07591e+43
The lowest score is: 0
The average score is: 3

what am I doing wrong?
Apr 25, 2012 at 1:05am
Nevermind I solved the issue! I forgot to add the

int = i;

now my last question is how to get the average score without the highest and lowest scores

Please help


Topic archived. No new replies allowed.