开发者

This program runs but not correctly; the numbers aren't right

开发者 https://www.devze.com 2022-12-28 15:09 出处:网络
this program runs but not correctly numbers arent right, i read numbers from a file and then when i am using them in the program they are not right.:brief decription of what i am trying to do can some

this program runs but not correctly numbers arent right, i read numbers from a file and then when i am using them in the program they are not right.:brief decription of what i am trying to do can someone tell me if something doesnt look right.

this is what i have to do:

write a program that determines the grade dispersal for 100 students

You are to read the exam scores into three arrays, one array for each exam. You must then calculate how many students scored A’s (90 or above), B’s (80 or above), C’s (70 or above), D’s (60 or above), and F’s (less than 60). Do this for each exam and write the distribution to the screen.

    // basic file operations
    #include <iostream>
    #include <fstream>
    using namespace std;
    int read_file_in_array(double exam[100][3]);
    double calculate_total(double exam1[], double exam2[], double exam3[]); // function that calcualates grades to see how many 90,80,70,60
    //void display_totals();
    double exam[100][3];
    int main()
    {
        double go,go2,go3;
        double exam[100][3],exam1[100],exam2[100],exam3[100];
        go=read_file_in_array(exam);
        go2=calculate_total(exam1,exam2,exam3);
        //go3=display_totals();
        cout << go,go2,go3;
        return 0;
    }
    /*
    int display_totals()
    {

        int grade_total;
        grade_total=calculate_total(exam1,exam2,exam3);
        return 0;
    }   */
    double calculate_total(double exam1[],double exam2[],double exam3[])
    {
        int calc_tot,above90=0, above80=0, above70=0, above60=0,i,j, fail=0;
        double exam[100][3];
        calc_tot=read_file_in_array(exam);
        for(i=0;i<100;i++)
            {
            for (j=0; j<3; j++)
                {
                exam1[i]=exam[100][0];
                exam2[i]=exam[100][1];
                exam3[i]=exam[100][2];
                if(exam[i][j] <=90 && exam[i][j] >=100)
                    {
                    above90++;
                        {
                        if(exam[i][j] <=80 && exam[i][j] >=89)
                            {
                            above80++;
                                {
                                if(exam[i][j] <=70 && exam[i][j] >=79)
                                    {
                                    above70++;
                                        {
                                        if(exam[i][j] <=60 && exam[i][j] >=69)
                                            {
                                            above60++;
                                                {
                                                if(exam[i][j] >=59)
                                                    {
                                                    fail++;
                                                }
                                                }
                                        }
                                        }
                                }
                                }
                        }
                        }
                }
            }
        }
        return 0;
    }

    int read_file_in_array(double exam[100][3])
    {
        ifstream infile;  
        int exam1[100];
        int exam2[100];
        int exam3[100];
      infile.open("grades.txt");// file containing numbers in 3 columns
        if(infile.fail()) // checks to see if file opended
            {
            cout << "error" << endl;
            }
        int num, i=0,j=0;
        while(!infile.eof()) // reads file to end of line
          {
              for(i=0;i<100;i++) // array numbers les开发者_如何学Cs than 100
              {
                for(j=0;j<3;j++) // while reading get 1st array or element
                infile >> exam[i][j];
                infile >> exam[i][j];
                infile >> exam[i][j];
                cout << exam[i][j] << endl;
                    {
                        if (! (infile >> exam[i][j]) )
                        cout << exam[i][j] << endl;
                    }
              exam[i][j]=exam1[i];
              exam[i][j]=exam2[i];
              exam[i][j]=exam3[i];
              }     
        infile.close();
      }
    return 0;
    }


It's a bit hard to read your code, but when you're checking the score, you should be using chained else if statements and not nested ifs.

Something along the lines of:

if(exam[i][j] >=90 && exam[i][j] <=100)
{
     above90++;
}
else if(exam[i][j] >=80 && exam[i][j] <=89)
{
     above80++;
}
else if(...)
{...}

Also, you might want to pick a single syntax and be consistent with it throughout your code, it will make a HUGE amount of difference when trying to solve any problems, or even figuring out what on earth you were doing! It doesn't matter what way you go with as much as being consistent with it.


If statements allow you to do something if the conditional (the expression inside the parens) is true, and also if it's false.

if( /* statement that evaluates to true */ )
{
     /* insert the commands you want to run when it's true here */
}
else
{
     /* insert the commands you want to run when it's false here */
}

You can use the 'else' section to chain If statements together, this is useful if you have more than two possibilities. Such as this:

int number = 2; // Can be a value between 0 and 2 (0, 1, or 2)
if(number == 0)
{
     cout << "The number is zero";
}
else if(number == 1)
{
     cout << "The number is one";
}
else if(number == 2)
{
     cout << "The number is two";
}
else
{
     cout << "The value wasn't an allowed value!";
}

If number is between 0 and 2, it'll print out what number it is, and if it's not in that range, it'll print out a message saying it's not.


In addition to what others have stated, you need to look at your logic.

Example:

if(exam[i][j] <=90 && exam[i][j] >=100) {
...
}

Think about that for a minute. How can a number be <= 90 and simultaneously be >= 100?


Since you know how to write loops and functions, I suggest you change your design.

Create functions that operate on an Exam.
Create three separate Exam variables.
Call the functions on each Exam. In other words, only one exam will be passed as a parameter to a function.

Some example function names:
Exam_Calculate_Distribution
Output_Distribution

According to the minimal information you presented, the data is organized by rows of students and columns of scores. This would be the exception to the theme, since 3 exams would be easier to load.

In this assignment, the distribution may be easier to calculate using an array of 10 sections, each represent a range of 10. The calculation is simplified:

grade_index = grade / 10; distribution[grade_index]++;

With this method, there is no need for using if-else statements. The cost is a few more memory cells for the extra slots in the array. In this case, I believe simplicity out weighs the extra used space.

One more step is required: after the distribution is calculated, sum up the regions 0 through 5. This represents the failures (F grades).

You will have to figure out how to handle the special case of a perfect score.

Remember, if your program is too complex for you to understand, you probably need to review the design.

Programs should be a simple as possible but no simpler. -- paraphrasing Albert Einstein.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号