Thread: Problem getting correct numbers

  1. #1
    Registered User
    Join Date
    May 2014
    Posts
    3

    Problem getting correct numbers

    I am a beginner at C++, the output is pretty self explanatory. I'm trying to make a number sorting program with other features, but the numbers are all wrong.


    Code:
    #include <iostream> 
    #include <iomanip>
    #include <fstream>
    
    
    using namespace std;
     
    void readData(int list[], int size);
    
    
    
    
    int main()
    {
        int size = 50;
        int scores[50] = {0};
        int range (0);
        int rangeA(0);
        int rangeB(0);
        int rangeC(0);
        int rangeD(0);
        int a;
        int b;
        double avg;
        int c;
        int d;
        double avgA;
        int e;
        int f;
        double avgB;
        int g;
        int h;
        double avgC;
        int i;
        int j;
        double avgD;
        int aAvg (0);
        int aAvgA(0);
        int aAvgB(0);
        int aAvgC(0);
        int aAvgD(0);
        int paa, paaA, paaB, paaC, paaD;
        
        readData(scores, size);               //  call the function to load the array
        
        sort(scores, scores + 50);
        for ( int n=0 ; n<50 ; ++n )
        {
             if(scores[n] <=599&&scores[n] >= 300)
             {
                 rangeA = rangeA + 1;
                 c = scores[n];
                 d = scores[n] + c;
             }
             if(scores[n] <=699&&scores[n] >= 600)
             {
                 rangeB = rangeB + 1;
                 e = scores[n];
                 f = scores[n] + e;
             }
             if(scores[n] <=799&&scores[n] >= 700)
             {
                 rangeC = rangeC + 1;
                 g = scores[n];
                 h = scores[n] + g;
             }
             if(scores[n] <=850&&scores[n] >= 800)
             {
                 rangeD = rangeD + 1;
                 i = scores[n];
                 j = scores[n] + i;
             }
             range = range + 1;
             a = scores[n];
             b = scores[n] + a;
    
    
                       
        }
        int highA, highB, highC, highD;
        int lowA, lowB, lowC, lowD;
             avg = b/range;
             avgA = d/rangeA;
             avgB = f/rangeA;
             avgC = h/rangeA;
             avgD = j/rangeA;
             lowA = scores[1];
             highA = scores[rangeA];
             lowB = scores[rangeA + 1];
             highB = scores[rangeB];
             lowC = scores[rangeB +1];
             highC = scores[rangeC];
             lowD = scores[rangeC + 1];
             highD = scores[rangeD];
             for ( int n=0 ; n<50 ; ++n )
             {
                 if(scores[n] > avg)
                 {
                     aAvg = aAvg + 1;
                 }
                 
                 if(scores[n] > avgA && scores[n] <= 599)
                 {
                      aAvgA = aAvgA + 1;
                 }
                 if(scores[n] > avgB && scores[n] <= 699)
                 {
                      aAvgB = aAvgB + 1;
                 }
                 if(scores[n] > avgC && scores[n] <= 799)
                 {
                      aAvgC = aAvgC + 1;
                 }
                 if(scores[n] > avgD && scores[n] <= 850)
                 {
                      aAvgD = aAvgD + 1;
                 } 
             }
             paa = (aAvg/range)*100;
             paaA = (aAvgA/rangeA)*100;
             paaB = (aAvgB/rangeB)*100;
             paaC = (aAvgC/rangeC)*100;
             paaD = (aAvgD/rangeD)*100;
             
             cout << "Score Range     No.     Average     Lowest     Highest     No. of scores above average     Percent above average" << endl;
             cout << "300-599         " << rangeA << "       " << avgA << "      " << lowA << "       " << highA << "       " << aAvgA << "                             " << paaA << "%" << endl;
             cout << "600-699         " << rangeB << "       " << avgB << "      " << lowB << "       " << highB << "       " << aAvgB << "                             " << paaB << "%" << endl;
             cout << "700-799         " << rangeC << "       " << avgC << "      " << lowC << "       " << highC << "       " << aAvgC << "                             " << paaC << "%" << endl;
             cout << "800-850         " << rangeD << "       " << avgD << "      " << lowD << "       " << highD << "       " << aAvgD << "                             " << paaD << "%" << endl;
             
             cout << "Average: " << avg << endl;
             cout << "No. of scores above average: " << aAvg << endl;
             cout << "Percent above average: " << paa << "%" << endl;
             
        system("PAUSE");
        return 0;
    }
    
    
    void readData(int list[], int size)      //  This function loads an array from an input file 
    {
        ifstream infile;
    
    
        infile.open("Input.DAT");
    
    
        if (!infile)
        {
            cout << "Cannot open the input file. Program terminates!" 
                 << endl;
            return ;
        }
        int score;
        int index = 0;
    
    
        infile >> list[index];
    
    
        while (index < size - 1)
        {
            index = index + 1;
            infile >> list[index];
        }
            infile.close();
    
    
    }

    Input.DAT
    Code:
    376 389 450 735 600 576 612 700 450 628 778 389 667 500 475 550 687 791 829 344
    549 476 400 587 535 657 789 583 340 764 422 826 566 436 565 834 533 423 837 701
    847 521 746 356 582 465 493 593 425 421

    I can't for the life of me figure out why the numbers are all messed up.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Is suggest posting the output the program is giving and say how that output is NOT correct.

    Tim S.

    PS: I deleted my prior post because I realized you were using std::sort.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    May 2014
    Posts
    3
    Code:
    Score Range     No.     Average     Lowest     Highest     No. of scores above a
    verage     Percent above average
    300-599         31       38      344       600       31
        100%
    600-699         6       44      612       400       37
       600%
    700-799         8       51      421       422       45
       500%
    800-850         5       54      423       389       50
       1000%
    Average: 33
    No. of scores above average: 50
    Percent above average: 100%
    Press any key to continue . . .
    That's the output.
    It's supposed to come out something like this:
    Code:
                 Score  Range       no.            Average         Lowest        Highest         No. of scores above average        Percent above average
                  300 - 599            31             476.45             340            593                            14                                             45 %
                  600 - 699             6              641.83              600            687                             3                                              50 %                           
                  700 - 799             8              750.50              700            791                             4                                              50 %  
                  800 - 850             5              834.60              826            847                             2                                              40 % 
    
                     Average :  575.96    
                    No. of scores above average : 24
                     Percent above average  48 %
    

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I would suggest you investigate the std::setw() function along with some of the other functions contained in the iomanip header file instead of trying to use spaces to space your values. The wrapping is probably being caused by your terminal which may be set to something like an 80 character max width.

    Jim

  5. #5
    Registered User
    Join Date
    May 2014
    Posts
    3
    Thanks for the reply. The setw function was going to be the last thing. I wanted to make sure it wasn't reading dumb things like 1000% above average though. Really not sure why that's happening.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Possibly more significant than your output formatting, are incorrect calculations.

    One obvious problem is that division of integers gives an integer result. So statements like "avg = b/range" where b and range are integers and avg is a double computes b/range as an integer, and then converts that integer to floating point.

    So avg = 3/2 will give avg a value of 1.0, not 1.5.

    You're making that type of mistake in multiple places in your code.


    Given that you have a bunch of similarly named variables (avg, avgA, avgB, ... and a,b,c,d,e,f, ....) and you're doing similar operations with them them, you need to consider using more arrays.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to correct the problem?
    By vsanandan in forum C Programming
    Replies: 2
    Last Post: 03-26-2008, 02:02 AM
  2. My program works but the numbers are not correct.
    By romeoz in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2003, 10:02 PM
  3. Replies: 3
    Last Post: 07-24-2002, 08:46 AM
  4. What is wrong here, just won't print the correct numbers
    By Unregistered in forum C++ Programming
    Replies: 16
    Last Post: 05-10-2002, 03:31 PM
  5. does anyone know how to correct this problem
    By bryankorb in forum C Programming
    Replies: 2
    Last Post: 12-07-2001, 09:34 PM