arrays

this is the code that im trying to make and im not sure of the things that i did but the output is correct. im just confused about the last part of the program where the name and score is being printed.

int scores[] = {};
string name[100];
int x, i;

cout << "How many students?: " ;
cin >> x ;

for(i=0; i<x; i++){
cout << "Enter Students " << i+1 << " name: " ;
cin >> name[i];
cout << "Enter Students " << i+1 << " score: " ;
cin >> scores[i];
}

int largesc = scores[0];
int smallsc = scores[0];


for(i=1; i<x; i++)
{
if(scores[i] > largesc)
largesc = scores[i];
else if(scores[i] < smallsc)
smallsc = scores[i];
}

for (i=0; i<x; i++)
{
if(scores[i] == largesc)
cout<<endl<<"highest score is "<<name[i]<<" with "<<largesc<<" score."<<endl;
}
for (i=0; i<x; i++)
{
if(scores[i] == smallsc)
cout<<"lowest score is "<<name[i]<<" with "<<smallsc<<" score."<<endl;
}


My question is .. how come this loop and if statement helps the program to output the correct name?
for (i=0; i<x; i++)
{
if(scores[i] == smallsc)
cout<<"The lowest score is "<<name[i]<<" with "<<smallsc<<" score.";

if i don't put this ( the loop and if statement ) then the program will not output the correct name.. i just want to understand the codes... any help will be appreciated.
closed account (o3hC5Di1)
Hi there,

Here's a very good explanation of how arrays work, and how accessing their values works:
http://cplusplus.com/doc/tutorial/arrays/

Explains it better than I could :)

All the best,
NwN
the output is correct

What? It shouldn't even compile: int scores[] = {}; is not valid C++. What compiler do you use?
And use code tags next time. It makes it easier to read.
thanks NwN.

ahmm... @MiiNiiPaa... im using Dev C++ 4992
here's the complete code:

#include <iostream>
using namespace std;
int main()
{

int scores[] = {};
string name[100];
int x, i;

cout << "How many students?: " ;
cin >> x ;

for(i=0; i<x; i++){
cout << "Enter Students " << i+1 << " name: " ;
cin >> name[i];
cout << "Enter Students " << i+1 << " score: " ;
cin >> scores[i];
}

int largesc = scores[0];
int smallsc = scores[0];


for(i=1; i<x; i++)
{
if(scores[i] > largesc)
largesc = score[i];
else if(scores[i] < smallsc)
smallsc = score[i];
}

for (i=0; i<x; i++)
{
if(scores[i] == largesc)
cout<<endl<<" highest score is "<<name[i]<<" with "<<largesc<<" score."<<endl;
}
for (i=0; i<x; i++)
{
if(scores[i] == smallsc)
cout<<" lowest score is "<<name[i]<<" with "<<smallsc<<" score."<<endl;
}


system("pause");
}
Last edited on
To answer your question, it needs the for loop and if statement because you need to have i set to the correct value. What you need is to make "sentinel" variables that can be set to the value of your high and low spots in your array. (look for the variables hi and low in the example)

You might also want to work on indenting, it would have made it a bit easier to work with if this was a longer code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
int main()
{
	int wait;
int scores[100];
string name[100];
int x, i;

cout << "How many students?: " ;
cin >> x ;

for(i=0; i<x; i++){
cout << "Enter Students " << i+1 << " name: " ;
cin >> name[i];
cout << "Enter Students " << i+1 << " score: " ;
cin >> scores[i];
}

int largesc = scores[0];
int smallsc = scores[0];
int hi = 0 , low = 0;


for(i=1; i<x; i++)
{
if(scores[i] > largesc)
{
largesc = scores[i];
hi = i;
}
else if(scores[i] < smallsc)
{
smallsc = scores[i];
low = i;
}
}


cout<<endl<<"highest score is "<<name[hi]<<" with "<<largesc<<" score."<<endl;
cout<<"lowest score is "<<name[low]<<" with "<<smallsc<<" score."<<endl;

cin>>wait;
	return 0;
}


I'm also curious what compiler you're using, it's pretty user-friendly if it would run after "int scores[] = {};" but it's also teaching you bad coding if it didn't throw an error for that.
Last edited on
line 6: int scores[] = {};
line 21: largesc = score[i];
It wont work.

After fixing errors it works fine.
As for your question:
in first part of the program you have saved high and low values but haven't saved their indexes. So you know that lowest score is, say, 40, but don't know, who has such score.
Part you have questions with iterates over array and output names of people, whose scores equals lowest score and their score (lowest). If there is one people with lowest score it will output him. If there is two, it will outpur both and so on.
Last edited on
thanks for your help guys...
Topic archived. No new replies allowed.