Thread: Little bit help for Array Structure

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    11

    Little bit help for Array Structure

    I finished most of code as you guys see below, however, the code only summaries the highest final score of student, doesnt show the "Number" of student who got highest final score.(In fact, the "Number" of the student doesnt match the student who got the highest score) I tried couple times, but failed..please someone gives me a hint or explain why that part of my code is wrong(I have bolded it already)~~Thank you so much~~!

    Code:
    #include <iostream.h>
    #include <iomanip>
    
    using namespace std;
    const int lscore=7, qscore=5, fscore=1;  
    struct studentinfo
    {   
    
           char name[60];
           int lab[lscore];
           int quiz[qscore];
           int final[fscore];
           };
     
           
    int main()
    {
      fstream outFile; 
      const int snum=10;
    
      studentinfo student[snum];
      int Qindex, Lindex, Findex, index, num, qtotal, ltotal, ftotal, hscore, hs, Hscore, Hs;
      float qavg, lavg, favg;
      
      cout<<"Enter number of students: ";
      cin>>num;
      for (index=0; index<num; index++)
      { 
          cout<<"Enter the name of student #"<<index+1<<": ";
          cin.ignore();
          cin.getline(student[index].name, 60);
          for (Qindex=0; Qindex<qscore; Qindex++)
          {
              cout<<"Enter quiz "<<Qindex+1<<" for student "<<index+1<<": ";
              cin>>student[index].quiz[Qindex];
          }  
              cout<<endl; 
          for (Lindex=0; Lindex<lscore; Lindex++)
          {
              cout<<"Enter lab "<<Lindex+1<<" for student "<<index+1<<": ";
              cin>>student[index].lab[Lindex];
              }
              cout<<endl;
          for (Findex=0; Findex<fscore; Findex++)
          {
              cout<<"Enter final score for student "<<index+1<<": ";
              cin>>student[index].final[Findex];
              }   
          cout<<endl;
          }
       
      for (index=0; index<num; index++)
      {
          cout<<"Summary for student #"<<index+1<<endl;
          cout<<"Name of Student "<<student[index].name<<endl;
          cout<<"Quiz Scores: ";
          for (Qindex=0; Qindex<qscore; Qindex++)
          {
              cout<<student[index].quiz[Qindex]<<" ";
              }    
          cout<<"  Avg quiz score = ";
          qtotal=0;
          for (Qindex=0; Qindex<qscore; Qindex++)
          {
              qavg=((qtotal+=student[index].quiz[Qindex])/(qscore*1.0));
              }
          cout<<setprecision(3)<<qavg<<endl;
          cout<<"Lab Score: ";
          for (Lindex=0; Lindex<lscore; Lindex++)
          {
              cout<<student[index].lab[Lindex]<<" ";
              }
          cout<<"  Avg quiz score = ";
          ltotal=0;
          for (Lindex=0; Lindex<lscore; Lindex++)
          {
              lavg=((ltotal+=student[index].lab[Lindex])/(lscore*1.0));
              }
          cout<<setprecision(3)<<lavg<<endl;
          cout<<"Final Exam Score = ";
          for (Findex=0; Findex<fscore; Findex++)
          {
              cout<<student[index].final[Findex];
              }
          cout<<endl;
          }
          hscore=student[0].final[0];
          ftotal=0;
          hs=0;
          for (index=0; index<num; index++)
          {
                  for (Findex=0; Findex<fscore; Findex++)      
                  {
                     if(student[index].final[Findex]>hscore)
                     hscore=student[index].final[Findex];
                     hs=index;
                     favg=((ftotal+=student[index].final[Findex])/(num*1.0));
                  }
                  
          }
          Hscore=hscore;
          Hs=hs;
          cout<<"Highest score for final = "<<Hscore<<" by student #"<<Hs<<", ";
          cout<<setprecision(4)<<"Avg for final = "<<favg<<endl;
          system("pause");
          return 0;

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Instead of this
    Code:
                     if(student[index].final[Findex]>hscore)
                     hscore=student[index].final[Findex];
                     hs=index;
    Try this
    Code:
                     if(student[index].final[Findex]>hscore){
                         hscore=student[index].final[Findex];
                         hs=index;
                     }
    Note: the avg logic is not something that made any sense to me.
    The normal way to to do the sum in the loop and find the average outside the loop.

    Tim S.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    11
    Quote Originally Posted by stahta01 View Post
    Instead of this
    Code:
                     if(student[index].final[Findex]>hscore)
                     hscore=student[index].final[Findex];
                     hs=index;
    Try this
    Code:
                     if(student[index].final[Findex]>hscore){
                         hscore=student[index].final[Findex];
                         hs=index;
                     }
    Note: the avg logic is not something that made any sense to me.
    The normal way to to do the sum in the loop and find the average outside the loop.

    Tim S.
    Thank you for the advise Tim~~I have tried ur code, but it is still not working as it suppose to be~~>_<

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    11
    Quote Originally Posted by stahta01 View Post
    Instead of this
    Code:
                     if(student[index].final[Findex]>hscore)
                     hscore=student[index].final[Findex];
                     hs=index;
    Try this
    Code:
                     if(student[index].final[Findex]>hscore){
                         hscore=student[index].final[Findex];
                         hs=index;
                     }
    Note: the avg logic is not something that made any sense to me.
    The normal way to to do the sum in the loop and find the average outside the loop.

    Tim S.
    Hey tim~~Thank you for ur help, it works~~!!..ahah, I forgot to add the indentation last time..my bad~~

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 12-14-2007, 03:34 PM
  2. bit fields in a structure
    By onebrother in forum C Programming
    Replies: 2
    Last Post: 04-18-2007, 12:10 PM
  3. porting application from 32 bit to 64 bit error
    By gandalf_bar in forum Linux Programming
    Replies: 1
    Last Post: 09-14-2005, 09:20 AM
  4. Bit processing in C
    By eliomancini in forum C Programming
    Replies: 8
    Last Post: 06-07-2005, 10:54 AM
  5. binary numbers
    By watshamacalit in forum C Programming
    Replies: 4
    Last Post: 01-14-2003, 11:06 PM