954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

plz respond

This is a program i made..but i wanted it to arrnge the total in decreasing order showin the highest scorer 1st....what more should i do 4 that in this program....???

#include<iostream.h>
#include<conio.h>


void main()
{
int n;
const int limit=50;
int rollno[limit];
int maths[limit],english[limit],science[limit],social[limit],hindi[limit];
int Total[limit];
char Grade[limit];
int temp;
cout<<"\n\n\n\tEnter the no of students (Max:50)  :" ;
cin>>n;


for(int i=0;i<n;i++)

{
clrscr();
cout<<"\n\n\n\tEnter roll number\t";
cin>>rollno[i];
cout<<"\n\n\tEACH OUT OF 100\n\n\n";
cout<<"Enter English marks  ";
cin>>english[i];
cout<<"Enter Science marks  ";
cin>>science[i];
cout<<"Enter Social marks   ";
cin>>social[i];
cout<<"Enter Hindi marks    ";
cin>>hindi[i];
cout<<"Enter Maths marks    ";
cin>>maths[i];

}



for(int i=0;i<n;i++)
{
       clrscr();

 Total[i]=maths[i]+english[i]+science[i]+social[i]+hindi[i];

if(Total[i]>450)
Grade[i]='A';
else if(Total[i]>400)
Grade[i]='A';
else if(Total [i]>300)
Grade[i]='A';
else if(Total[i]>200)
Grade[i]='A';
else Grade[i]='E';

}

for(int i=1;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;
                 }//if statement
           }  //inner for loop
  }//outer for loop


cout<<"\n\n\n\t\tPROGRESS REPORT\n\n\n\n\t";

cout<<"\n\n\tRollno\t\tTotal\t\tGrade";

for(int i=0;i<n;i++)
{


 cout<<"\n\n\t "<<rollno[i]<<" \t\t"<<Total[i]<<" \t\t"<<Grade[i];

         }



getch();

}
anuizath2007
Light Poster
49 posts since Jul 2008
Reputation Points: -12
Solved Threads: 0
 

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
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

thankz....but when i do that the problem is ...only the highest total comes ist but the roll no and grade are not corresponding to it...ie...in the process of getting the total in the decreasing order the roll no and grade gets interchanged...so what can i do to correct it?....any suggestions

anuizath2007
Light Poster
49 posts since Jul 2008
Reputation Points: -12
Solved Threads: 0
 

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
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

thank you!!......i m using arrays coz i havnt studied structures yet...im just a beginner...

and i did what you sugggested and it says "WARNING-you may lose significant digits.."...and when i run it if i enter 3 values..i get only 2 in the final result.....i have shown the program below...why does this happen?

#include<iostream.h>
#include<conio.h>


void main()
{
int n;
const int limit=50;
int rollno[limit];
int maths[limit],english[limit],science[limit],social[limit],hindi[limit];
int Total[limit];
char Grade[limit];
int temp;
cout<<"\n\n\n\tEnter the no of students (Max:50)  :" ;
cin>>n;


for(int i=0;i<n;i++)

{
clrscr();
cout<<"\n\n\n\tEnter roll number\t";
cin>>rollno[i];
cout<<"\n\n\tEACH OUT OF 100\n\n\n";
cout<<"Enter English marks  ";
cin>>english[i];
cout<<"Enter Science marks  ";
cin>>science[i];
cout<<"Enter Social marks   ";
cin>>social[i];
cout<<"Enter Hindi marks    ";
cin>>hindi[i];
cout<<"Enter Maths marks    ";
cin>>maths[i];

}



for(int i=0;i<n;i++)
{
       clrscr();

 Total[i]=maths[i]+english[i]+science[i]+social[i]+hindi[i];

if(Total[i]>450)
Grade[i]='A';
else if(Total[i]>400)
Grade[i]='A';
else if(Total [i]>300)
Grade[i]='A';
else if(Total[i]>200)
Grade[i]='A';
else Grade[i]='E';

}

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;
                 temp = rollno[i];
                 rollno[i] = rollno[j];
                 rollno[j] = temp;


                 }//if statement
           }  //inner for loop
  }//outer for loop


cout<<"\n\n\n\t\tPROGRESS REPORT\n\n\n\n\t";

cout<<"\n\n\tRollno\t\tTotal\t\tGrade";

for(int i=0;i<n;i++)
{

cout<<"\n\n\t "<<rollno[i]<<" \t\t"<<Total[i]<<" \t\t"<<Grade[i];
 if (Grade[i]=='E')
  {cout<<"\t FAIL" ;}
         }



getch();

}
anuizath2007
Light Poster
49 posts since Jul 2008
Reputation Points: -12
Solved Threads: 0
 

i more doubt...suppose while running the program ....eg for maths mark the maximum limit s 100...but if aperson writes 150 also that runs..but i want to say not possible/wrong entry...for all the 5 subjects...is there anything i can put like'some common instruction to not read the value beyond 100'?..any idea?

anuizath2007
Light Poster
49 posts since Jul 2008
Reputation Points: -12
Solved Threads: 0
 

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
Team Colleague
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
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

i havnt studied function..so i tried doin the way u told earlier...n now it says 22 errors..n i cant seem to find where the error is............

#include<iostream.h>
#include<conio.h>


void main()
{
int n;
const int limit=50;
int rollno[limit];
int maths[limit],english[limit],science[limit],social[limit],hindi[limit];
int Total[limit];
char Grade[limit];
int temp;
cout<<"\n\n\n\tEnter the no of students (Max:50)  :" ;
cin>>n;


for(int i=0;i<n;i++)

{
      clrscr();
      cout<<"\n\n\n\tEnter roll number\t";
      cin>>rollno[i];
      cout<<"\n\n\tEACH OUT OF 100\n\n\n";

        do
   {
        cout<<"Enter English marks  ";
        cin>>english[i];
        if( english[i] > 100)
       cout << "\tWrong entry\n";
    } while( english[i] > 100 );
      do
   {
      cout<<"Enter Science marks  ";
       cin>>science[i];
      if( science[i] > 100)
       cout << "\tWrong entry\n";
     } while( science[i] > 100 );
         do
    {
      cout<<"Enter Social marks  ";
      cin>>social[i];
     if( social[i] > 100)
     cout << "\tWrong entry\n";
    } while( social[i] > 100 );
      do
      {
     cout<<"Enter Hindi marks  ";
      cin>>hindi[i];
      if( hindi[i] > 100)
       cout << "\tWrong entry\n";
        } while( hindi[i] > 100 );
      cout<<"Enter Maths marks    ";
      cin>>maths[i];
       if( maths[i] > 100)
       cout << "\tWrong entry\n";
       } while( maths[i] > 100 );


}  //for statement



for(int i=0;i<n;i++)
{
       clrscr();

 Total[i]=maths[i]+english[i]+science[i]+social[i]+hindi[i];

if(Total[i]>450)
Grade[i]='A';
else if(Total[i]>400)
Grade[i]='A';
else if(Total [i]>300)
Grade[i]='A';
else if(Total[i]>200)
Grade[i]='A';
else Grade[i]='E';

}

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;
                 temp = rollno[i];
                 rollno[i] = rollno[j];
                 rollno[j] = temp;


                 }//if statement
           }  //inner for loop
  }//outer for loop


cout<<"\n\n\n\t\tPROGRESS REPORT\n\n\n\n\t";

cout<<"\n\n\tRollno\t\tTotal\t\tGrade";

for(int i=0;i<n;i++)
{

cout<<"\n\n\t "<<rollno[i]<<" \t\t"<<Total[i]<<" \t\t"<<Grade[i];
 if (Grade[i]=='E')
  {cout<<"\t FAIL" ;}
         }



getch();

}
anuizath2007
Light Poster
49 posts since Jul 2008
Reputation Points: -12
Solved Threads: 0
 

you forgot do { for Math Marks on line 54

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

oh thankz...but still that warning is there...when i enetr three values one of them is missing.....is there any problem with the sorting part of the array?

anuizath2007
Light Poster
49 posts since Jul 2008
Reputation Points: -12
Solved Threads: 0
 

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.....

anuizath2007
Light Poster
49 posts since Jul 2008
Reputation Points: -12
Solved Threads: 0
 
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
Team Colleague
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?

anuizath2007
Light Poster
49 posts since Jul 2008
Reputation Points: -12
Solved Threads: 0
 

this is the modified one!!.........

#include<iostream.h>
#include<conio.h>


void main()
{
int n;
const int limit=50;
int rollno[limit];
int maths[limit],english[limit],science[limit],social[limit],hindi[limit];
int Total[limit];
char Grade[limit];
int temp;
cout<<"\n\n\n\tEnter the no of students (Max:50)  :" ;
cin>>n;


for(int i=0;i<n;i++)

{
      clrscr();
      cout<<"\n\n\n\tEnter roll number\t";
      cin>>rollno[i];
      cout<<"\n\n\tEACH OUT OF 100\n\n\n";

        do
   {
        cout<<"Enter English marks  ";
        cin>>english[i];
        if( english[i] > 100)
       cout << "\tWrong entry\n";
    } while( english[i] > 100 );
      do
   {
      cout<<"Enter Science marks  ";
       cin>>science[i];
      if( science[i] > 100)
       cout << "\tWrong entry\n";
     } while( science[i] > 100 );
         do
    {
      cout<<"Enter Social marks  ";
      cin>>social[i];
     if( social[i] > 100)
     cout << "\tWrong entry\n";
    } while( social[i] > 100 );
      do
      {
     cout<<"Enter Hindi marks  ";
      cin>>hindi[i];
      if( hindi[i] > 100)
       cout << "\tWrong entry\n";
        } while( hindi[i] > 100 );
        do
        {
      cout<<"Enter Maths marks    ";
      cin>>maths[i];
       if( maths[i] > 100)
       cout << "\tWrong entry\n";
       } while( maths[i] > 100 );


}  //for statement



for(int i=0;i<n;i++)
{
       clrscr();

 Total[i]=maths[i]+english[i]+science[i]+social[i]+hindi[i];

if(Total[i]>=450)
Grade[i]='A';
else if(Total[i]>=400)
Grade[i]='B';
else if(Total [i]>=300)
Grade[i]='C';
else if(Total[i]>=200)
Grade[i]='D';
else Grade[i]='E';

}

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;
                 temp = rollno[i];
                 rollno[i] = rollno[j];
                 rollno[j] = temp;


                 }//if statement
           }  //inner for loop
  }//outer for loop


cout<<"\n\n\n\t\tPROGRESS REPORT\n\n\n\n\t";

cout<<"\n\n\tRollno\t\tTotal\t\tGrade";

for(int i=0;i<n;i++)
{

cout<<"\n\n\t "<<rollno[i]<<" \t\t"<<Total[i]<<" \t\t"<<Grade[i];
 if (Grade[i]=='E')
  {cout<<"\t FAIL" ;}
         }



getch();

}
anuizath2007
Light Poster
49 posts since Jul 2008
Reputation Points: -12
Solved Threads: 0
 

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 havnt studied structures so i cant use them....but i dint understand what you meant .i need to know how to rank the students after sorting them in decreasing order of their total...any suggestions..plz reply..

anuizath2007
Light Poster
49 posts since Jul 2008
Reputation Points: -12
Solved Threads: 0
 
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
Team Colleague
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
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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 in RED 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" ;}

my compiler is Boeland 5.0 n the error still persists.

....what i meant by ranking them is assign rank 1 to the one with maximum total.....can u please xplain that 'i+1 logic'...
anywayz i tried doing wat you asked me to...
but it says error "ambigous operators need paranthesis'"...............please do reply if any ideas to solve this and the modified code is shown below..

#include<iostream.h>
#include<conio.h>


void main()
{
int n;
const int limit=50;
int rollno[limit];
int maths[limit],english[limit],science[limit],social[limit],hindi[limit];
int Total[limit];
char Grade[limit];
int rank[limit];
int temp;
cout<<"\n\n\n\tEnter the no of students (Max:50)  :" ;
cin>>n;


for(int i=0;i<n;i++)

{
      clrscr();
      cout<<"\n\n\n\tEnter roll number\t";
      cin>>rollno[i];
      cout<<"\n\n\tEACH OUT OF 100\n\n\n";

        do
   {
        cout<<"Enter English marks  ";
        cin>>english[i];
        if( english[i] > 100)
       cout << "\tWrong entry\n";
    } while( english[i] > 100 );
      do
   {
      cout<<"Enter Science marks  ";
       cin>>science[i];
      if( science[i] > 100)
       cout << "\tWrong entry\n";
     } while( science[i] > 100 );
         do
    {
      cout<<"Enter Social marks   ";
      cin>>social[i];
     if( social[i] > 100)
     cout << "\tWrong entry\n";
    } while( social[i] > 100 );
      do
      {
     cout<<"Enter Hindi marks    ";
      cin>>hindi[i];
      if( hindi[i] > 100)
       cout << "\tWrong entry\n";
        } while( hindi[i] > 100 );
        do
        {
      cout<<"Enter Maths marks    ";
      cin>>maths[i];
       if( maths[i] > 100)
       cout << "\tWrong entry\n";
       } while( maths[i] > 100 );


}  //for statement



for(int i=0;i<n;i++)
{
       clrscr();

 Total[i]=maths[i]+english[i]+science[i]+social[i]+hindi[i];

if(Total[i]>=450)
Grade[i]='A';
else if(Total[i]>=400)
Grade[i]='B';
else if(Total [i]>=300)
Grade[i]='C';
else if(Total[i]>=200)
Grade[i]='D';
else Grade[i]='E';

}

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;
                 temp = rollno[i];
                 rollno[i] = rollno[j];
                 rollno[j] = temp;


                 }//if statement
           }  //inner for loop
  }//outer for loop


cout<<"\n\n\n\t\t\tPROGRESS REPORT\n\n\n\n\t";

cout<<"\n\n\tRollno\t\tTotal\t\tGrade\t\t Rank ";

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" ;}
         }



getch();

}
anuizath2007
Light Poster
49 posts since Jul 2008
Reputation Points: -12
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You