Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Help with array problems (http://www.go4expert.com/forums/help-array-t28278/)

cscstudent1991 27Apr2012 04:23

Help with array problems
 
I am extremely new to programming and I am having a ton of trouble! I made this program to read in a external file called grades....

3313 90 42 58 64 70 75 100
5688 88 48 79 70 79 70 94
4700 50 44 89 73 70 73 100
9561 88 69 88 87 84 63 98
3199 96 69 100 90 88 67 100
3768 78 57 80 59 57 15 60
8291 72 56 70 82 74 9 83
7754 76 62 93 100 78 41 58
8146 94 68 99 94 93 9 54
2106 98 47 96 94 70 27 100

And then give a ID number, average grade, and corresponding grade from with,above, or below 10 points of the class average and asign "satisfactroy, outstanding, or unsatisfactory". This is way above my programming skills.
However, I am sure that I have two main problems. How do I call each column from the grades.txt and assign it to a function? Also, how do I give the corresponding grade? I know there is a problem with "if (mean < mean + 10)" and the other 2 alike, but what should take its place?


Code:

// File: stugrades.cpp
// Computes a external file and compiles it into a table with 3 columns of ID numbers, scores, and corresponding scores.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cmath>
using namespace std;
void readStuData
(ifstream grades,
float score[],
int id[],
int count,
bool tooMany);
int main()
{
ifstream myfile ("Grades.txt"); // Input from external file
int id[0]; // the ID of students
float score[1, 2, 3, 4, 5, 6];
float mean; // average of individuals scores
string grade;
int count;
int tooMany;
 
 
if (myfile.is_open())
{
while ( myfile.good() )
{
if (mean > mean + 10)
{
grade = "outstanding";
}
else if (mean < mean - 10)
{
grade = "unsatisfactory";
}
else
{
grade = "satisfactory";
}
mean = (score++ / count);
void printTable(int score[1, 2, 3, 4, 5, 6], int id[0], int count);
myfile >> id >> score >> endl;
cout << id << " " << mean << " " << grade << " " << endl;
 

}
myfile.close();
}
else cout << "Unable to open file";
return 0;

}


xpi0t0s 28Apr2012 15:17

Re: Help with array problems
 
You don't appear to have read anything in from the file. I'm not even sure you've opened it (I don't know ifstream too well). Can I suggest rather than trying to write the program all in one go you take it a step at a time?

First open the file and read a number from it, display it to screen, close and exit. Build that program and get it working before moving on to the next step.

Then modify that to read multiple numbers from the file. The format appears fixed: first a 4-digit number, then 7 numbers below 101. Display each of those numbers, and optionally detect a line break (by the fact that you've got a large number), and when that works the output should match the input.

As you haven't explained the file structure I can only guess. Does the first line mean student ID=3313 has scores 90 42 58 64 70 75 100? (It's not obvious, because another possibility is that 3313 is a test reference and student 1, represented by the second column, got scores 50, 88, 96 etc). How to proceed will depend critically on this.

If it's the first then you won't need any storage. You can just read the student ID, then initialise a variable X to zero, then read the next 7 numbers in and add them to X. X/7 will then give you the mean score for that student.

You haven't explained how the three grades (outstanding, satisfactory and unsatisfactory) are determined. Does "mean < mean - 10" mean "this student's mean is less than 10 points below the overall mean for the class"? If so then you are going to need some storage: at least the 10 scores for the students, then you have some way of determining where each student's mean lies in respect to the other students. But again take it a step at a time. Just do one student as shown above, and compile and run the program and make sure it works. Then create an array and read in and store all 10 students, and display the 10 results (compile, run, make sure it works, debug before proceeding).

The final step will then be easy: calculate the overall mean, then for each student work out if their mean is >overall+10, or <overall-10, or neither, and display the grading accordingly.

Taking it a step at a time means you won't be swamped by 1000s of errors. Honestly, this is how professionals write code. Nobody in the business to my knowledge writes more than a few lines of code in one go before running a compile/test cycle. This keeps the scope of debugging down to the minimum: if the first step (open, read 1 number, display it, close the file and exit) doesn't work, then you only have a limited number of reasons why: plus, if that doesn't work, then you have *no* chance of doing the whole program.

xpi0t0s 28Apr2012 15:22

Re: Help with array problems
 
You could do it without any array storage but you would need some way of reading through the file twice; if you can "rewind" the file, or if not you can close and reopen it. First pass will add up all numbers less than 101 and divide the result by the number of numbers*, which will be the overall mean. Second pass will work a line at a time: read in a student's scores, calculate the mean and compare it with the overall.

*and if you can do it that way in general, then you're safe if the instructor then gives you a bigger file.


All times are GMT +5.5. The time now is 03:34.