1) Array elements start counting from 0, not 1. That means the first element of Total is Total[0] and loop counters should count from 0 to but not including the number of elements in the array.
Example: The loop counters beginning on line 58 should be (and that's how I write simple bubble sort algorithm too :) )
for(int i=0; i < (n-1); i++)
{
for(int j = (i+1); j < n; j++)
{
// blabla
}
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Myself, I would have used a structure for that information so that you can easily sort structures instead of so many simple arrays.
struct student
{
int total;
// rest of student info goes here
};
But if you want to leave it the way you have it, then during the swap you have to swap all the arrays, not just the Total array.
for(int i=0; i < (n-1); i++)
{
for(int j = (i+1); j < n; j++)
{
if (Total[j]>Total[i])
{
temp=Total[i];
Total[i]=Total[j];
Total[j]=temp;
temp = Grade[i];
Grade[i] = Grade[j];
Grade[j] = temp;
// etc etc for each of the arrays.
}//if statement
}
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
You could do something like this:
do
{
cout<<"Enter English marks ";
cin>>english[i];
if( english[i] > 100)
cout << "Error\n";
} while( english[i] > 100 );
And if you are going to do that several times just write a function that returns the value
int GetEntry(string prompt)
{
int value = 0;
do
{
cout << prompt << "\n";
cin >> value;
if( value > 100)
cout << "Error\n";
} while( value > 100);
return value;
}
int main()
{
...
English[i] = GetEntry("Enter English Marks");
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
The loops on line 58 and 59 are still wrong. Use the < operator, not the <= operator when counting from 0.
for(int i=0;i<(n-1);i++)
{ for(int j=(i+1);j<n;j++)
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
you forgot do { for Math Marks on line 54
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
in this program...if i want to rank the students on the basis of their total marks wat do i do?...i mean where do i assign the rank.....
Well I would imagine that you would sort the records by Total before displaying them. So use any sorting algorithm you like, but it's a little more complicated since if you do any swaps, you'll have to swap the elements in ALL of the arrays, not just the Total array since this is grouped data. Then, after everything is sorted, display as you do. You've already written the display code, right?
I would actually set up a struct instead of having half a dozen or so arrays. Have a struct with the individual scores, the total, the grade, etc., and then have a single array of that struct. Makes the swapping and everything else much much easier in my opinion.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
lines 71-79: You assign an 'A' to everybody.
I ran the program and got this
PROGRESS REPORT
Rollno Total Grade
100 380 A
300 200 E FAIL
200 150 E FAIL
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
ok thatassignment A to everyone was a mistake...but still when i run it..n enter 3 values i can c only 2 ..y is that happening?
I ran the code you posted last and I entered 3 students that that's exactly what it displayed at the end.
What compiler are you using? I used vc++ 2008 Express, replaced iostream.h with iostream (no .h extension) and added using namespace std; But those changes should not have caused the problem you describe.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
.i need to know how to rank the students after sorting them in decreasing order of their total...any suggestions..plz reply..
what do you mean by "rank them" ? assign a number like the first one in the list is 1, the second one is 2, and the third (last) one is 3? That should be easy to do, just use the loop counter as shown inRED below.
for(int i=0;i<n;i++)
{
cout<<"\n\n\t "<<rollno[i]<<" \t\t"<<Total[i]<<" \t\t"<<Grade[i] << " Rank: " << i+1;
if (Grade[i]=='E')
{cout<<"\t FAIL" ;}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343