### 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.
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.
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243`` ``````int main() { int wait; int scores[100]; string name[100]; int x, i; cout << "How many students?: " ; cin >> x ; for(i=0; i> 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 largesc) { largesc = scores[i]; hi = i; } else if(scores[i] < smallsc) { smallsc = scores[i]; low = i; } } cout<>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.