Thread: Help with nested loops?? Please help!

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    10

    Help with nested loops?? Please help!

    I cannot figure out how to complete this nested loop with the appropriate output ">>>Average of all quizzes: 19.10" I keep getting 18.50. The input I have is this:
    1111 3 20 18 19
    7821 2 18 20
    9102 4 20 20 20 20
    8888 1 16


    After the 3,2,4,1 are the quizzes (10 of them). Can somebody help me with my code to get the correct value ">>>Average of all quizzes: 19.10", instead of ">>>Average of all quizzes: 18.50"?? I know I need to divide the total of all quizzes by the number of quizzes, but I'm not sure how to do this correctly. Please help!


    Here is the code that I have:

    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    
    
    using namespace std;
    
    
    int main()
    {
      ifstream inFile;
      ofstream outFile;
    
    
      inFile.open("in.data");
      outFile.open("out.data");
    
    
      if(inFile.fail()||outFile.fail())
      {
        cout << "Check your files" << endl;
        return 1;
      }
    
    
      outFile.setf(ios::fixed);
      outFile.precision(2);
    
    
      int studId;
      int numOfQuiz;
      int examlow = 0;
      int quizScore = 0;
      int records = 0;
      int lowStudId;
      int highStudId;
      int totNumQuiz;
      float totAvg = 0;
      float quizAvgHigh = 0;
      float quizAvgLow = 20;
      float allAvg = 0;
      float avg = 0;
      float quizTotAllStud = 0;
      float quizTotSingStud = 0;
    
    
      inFile >> studId >> numOfQuiz;
    
    
      outFile << "*~~< Stud Quiz Report >~~*" << endl << endl;
      outFile << setw(20) << left << "Stud Id" << left
                << setw(20) << left << "AVG of Quizzes" << endl;
      outFile << setw(20) << left << "-------" << left
                << setw(20) << left << "--------------" << endl;
    
    
      while(inFile)
      {
        for(int i = 0; i < numOfQuiz; i++)
        {
          inFile >> quizScore;
          totNumQuiz++;
          quizTotAllStud += quizScore;
          quizTotSingStud += quizScore;
        }
        avg = (quizTotAllStud/numOfQuiz);
        totAvg = totAvg + avg;
        outFile << setw(20) << studId << avg << endl;
        quizTotAllStud = 0;
        if(avg >= quizAvgHigh)
        {
          quizAvgHigh = avg;
          highStudId = studId;
        }
    
    
        if(avg <= quizAvgLow)
        {
          quizAvgLow = avg;
          lowStudId = studId;
        }
        records++;
        quizTotSingStud = 0;
    
    
        inFile >> studId >> numOfQuiz;
      }
    
    
      outFile << endl << ">>>Average of all quizzes: " << (totNumQuiz/numOfQuiz) << endl << endl;
      outFile << ">>>Student with id " << highStudId << " has highest quiz average of " << quizAvgHigh << "" << endl;
      outFile << ">>>Student with id " << lowStudId << " has lowest quiz average of " << quizAvgLow << "" << endl << endl;
      outFile << ">>>There were " << records << " students & " << totNumQuiz << " quizzes in file." << endl;
    
    
      inFile.close();
      outFile.close();
    
    
      return 0;
    }
    Last edited by GoBlue13; 02-28-2013 at 01:33 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Please edit your post to include [code][/code] tags.
    And make sure you do "paste as text" when you re-copy from your IDE/Text editor.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    10
    Done.. Sorry about that!

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    10
    I've tried taking the 0's out of the int's and putting (totNumQuiz/numOfQuiz) down at the bottom of the code and I get 10. I'm new at C++ and I've been trying to fix this for 4 hours. I have no idea what I'm doing wrong!

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Your code is still devoid of any indentation. Fix that please. Otherwise it's too hard to read.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Feb 2013
    Posts
    10
    Is that better? I can't get the lower part of the quiz to be any better formatted. This is the most current code that I have. I don't see what I'm doing wrong??

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
    $ ./a.out 
    $ cat out.data 
    *~~< Stud Quiz Report >~~*
    
    Stud Id             AVG of Quizzes      
    -------             --------------      
    1111                19.00
    7821                19.00
    9102                20.00
    8888                16.00
    
    >>>Average of all quizzes: 18.50
    
    >>>Student with id 9102 has highest quiz average of 20.00
    >>>Student with id 8888 has lowest quiz average of 16.00
    
    >>>There were 4 students & 10 quizzes in file.
    19+19+20+16 = 74
    74 / 4 = 18.5

    You're calculating an average of averages, not an average of scores.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Feb 2013
    Posts
    10
    I've noticed that, that's when I had (totAvg/records). I've tried (totNumQuiz/numOfQuiz) which keeps giving me 10. Am I not declaring the total number of quizzes?? I don't understand why (totNumQuiz/numOfQuiz) is not giving me the correct answer. I keep changing things around and it just keeps getting worse..

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You need totalScores / numberOfScores if you're looking to get 19.1 out of it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Feb 2013
    Posts
    10
    I've tried plugging in all int's and float's and I'm not getting it ...

  11. #11
    Registered User
    Join Date
    Feb 2013
    Posts
    10
    Ok, I have the average correct now, but it is throwing my average of quizzes with high ID #'s way off.. What am I doing wrong?? I get one thing fixed and now another problem happens.. sigh.. I don't understand why the low ID # scores are not affected, but the high ID #'s are..
    Here is the code I have now:

    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    
    using namespace std;
    
    int main()
    {
      ifstream inFile;
      ofstream outFile;
    
      inFile.open("in.data");
      outFile.open("out.data");
    
      if(inFile.fail()||outFile.fail())
      {
        cout << "Check your files" << endl;
        return 1;
      }
    
      outFile.setf(ios::fixed);
      outFile.precision(2);
    
      int studId;
      int numOfQuiz = 0;
      int examlow = 0;
      int quizScore = 0;
      int records = 0;
      int lowStudId;
      int highStudId;
      int totNumQuiz = 0;
      float totAvg = 0;
      float quizAvgHigh = 0;
      float quizAvgLow = 20;
      float allAvg = 0;
      float avg = 0;
      float quizTotAllStud;
      float quizTotSingStud;
    
      inFile >> studId >> numOfQuiz;
    
      outFile << "*~~< Stud Quiz Report >~~*" << endl << endl;
      outFile << setw(20) << left << "Stud Id" << left
                << setw(20) << left << "AVG of Quizzes" << endl;
      outFile << setw(20) << left << "-------" << left
                << setw(20) << left << "--------------" << endl;
    
      while(inFile)
      {
        for(int i = 0; i < numOfQuiz; i++)
        {
          inFile >> quizScore;
          totNumQuiz++;
          quizTotAllStud += quizScore;
          quizTotSingStud += quizScore;
        }
        avg = (quizTotAllStud/numOfQuiz);
        totAvg = totAvg + avg;
        outFile << setw(20) << studId << avg << endl;
        quizTotAllStud;
        if(avg >= quizAvgHigh)
        {
          quizAvgHigh = avg;
          highStudId = studId;
        }
    
        if(avg <= quizAvgLow)
        {
          quizAvgLow = avg;
          lowStudId = studId;
        }
        records++;
        quizTotSingStud;
    
        inFile >> studId >> numOfQuiz;
      }
    
      outFile << endl << ">>>Average of all quizzes: " << (quizTotAllStud/totNumQuiz) << endl << endl;
      outFile << ">>>Student with id " << highStudId << " has highest quiz average of " << quizAvgHigh << "" << endl;
      outFile << ">>>Student with id " << lowStudId << " has lowest quiz average of " << quizAvgLow << "" << endl << endl;
      outFile << ">>>There were " << records << " students & " << totNumQuiz << " quizzes in file." << endl;
    
      inFile.close();
      outFile.close();
    
      return 0;
    }
    Last edited by GoBlue13; 02-28-2013 at 05:42 AM.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
    $ g++ -Wall -Wextra foo.cpp
    foo.cpp: In function ‘int main()’:
    foo.cpp:60:19: warning: statement has no effect [-Wunused-value]
    foo.cpp:73:20: warning: statement has no effect [-Wunused-value]
    foo.cpp:26:7: warning: unused variable ‘examlow’ [-Wunused-variable]
    foo.cpp:35:9: warning: unused variable ‘allAvg’ [-Wunused-variable]
    Consider the first two warnings carefully.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Registered User
    Join Date
    Feb 2013
    Posts
    10
    Solved it. Thank you for your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested four loops
    By begginer in forum C Programming
    Replies: 12
    Last Post: 02-25-2011, 11:21 PM
  2. nested For loops
    By Chaplin27 in forum C++ Programming
    Replies: 1
    Last Post: 03-09-2005, 10:12 AM
  3. help with nested loops
    By geo_c in forum C Programming
    Replies: 15
    Last Post: 07-11-2004, 01:35 PM
  4. nested loops PLEASE HELP!!
    By scuba22 in forum C++ Programming
    Replies: 6
    Last Post: 10-08-2002, 09:31 AM
  5. nested loops
    By briand. in forum C Programming
    Replies: 6
    Last Post: 10-01-2002, 05:15 PM