Click to See Complete Forum and Search --> : Confusion++


Gobias
April 10th, 2008, 02:31 PM
I'm pretty new to C++.I have to write a program and I have absolutely no idea where to start. I've sat here and stared at the screen for awhile, but I'm still not sure what to do. I was wondering if anyone could give me any advice, code, pseudocode...whatever.
Heres what I have to do.
Assuming the maximum number of students in a class is 50, I have to write a program that reads student's names followed by their test score from a file and outputs:
- class average
- names of all students whose test scores are below the class average
- highest test score and the names of all the students having the highest score

I hope someone can help out!
Thanks in advance.

Lindley
April 10th, 2008, 02:35 PM
I'm pretty new to C++.I have to write a program and I have absolutely no idea where to start.



int main()
{

return 0;
}


Always a good place to start.

Assuming the maximum number of students in a class is 50

This means you can use an array of size 50. Probably two, actually, one for names and one for grades.


- class average


One loop through the grade array will allow you to calculate the average.


- names of all students whose test scores are below the class average


Once you've got the average, another loop can do this easily.


- highest test score and the names of all the students having the highest score


Okay, so find the maximum at the same time you find the average; then this should just be a third loop.

GCDEF
April 10th, 2008, 02:42 PM
Start at the beginning. Step one. You need to read from a file. Figure out how to do that. Move on to step two.

Gobias
April 10th, 2008, 03:17 PM
Can I test the program without actually having the file to read from, or will it crash unless I have that text file?

MrViggy
April 10th, 2008, 03:21 PM
That depends on what you write. If you don't check that you have a valid file handle, it may crash. If you do implement a check, I would expect you to print an error message.

Viggy

STLDude
April 10th, 2008, 03:29 PM
I would slightly disagree with GCDEF about first writing code to read the file in for input. I would concentrate on getting algorithm and code right for output you need:
- class average
- names of all students whose test scores are below the class average
- highest test score and the names of all the students having the highest score

and for input I would just hardcode some test values. After I'm satisfied with output, then and only then I would write the code to read file in and feed those values into my already tested code.

GCDEF
April 10th, 2008, 03:36 PM
I would slightly disagree with GCDEF about first writing code to read the file in for input. I would concentrate on getting algorithm and code right for output you need:

and for input I would just hardcode some test values. After I'm satisfied with output, then and only then I would write the code to read file in and feed those values into my already tested code.

Honestly, I don't think it makes much difference. The drawback with getting the algorithm done first is he has to have a mechanism for testing it with dummy data which will eventually just get thrown away. The requirements are that he read from a file, so getting that requirement done first will provide him with the data he needs to feed his algorithms.

At this stage, it's all new to him. While dummying up some data may be trivial to an experienced coder, seems like it's just one more thing for a novice to have to worry about.

Either way, the point is not to be overwhelmed by a big project, but to learn how to take what appears to be a large task and break it into its component pieces and solve them one at a time.

chiragrr
April 10th, 2008, 05:08 PM
Honestly, I don't think it makes much difference. The drawback with getting the algorithm done first is he has to have a mechanism for testing it with dummy data which will eventually just get thrown away. The requirements are that he read from a file, so getting that requirement done first will provide him with the data he needs to feed his algorithms.

At this stage, it's all new to him. While dummying up some data may be trivial to an experienced coder, seems like it's just one more thing for a novice to have to worry about.

Either way, the point is not to be overwhelmed by a big project, but to learn how to take what appears to be a large task and break it into its component pieces and solve them one at a time.

Just use



ifstream infile ("Filename");
string name[10];
int marks[10],i;
if (! infile.is_open())
{
perror("Error opening file");

}
i=0;
while (!infile.eof())// Read the file and parse it
{
infile>>name[i]>>marks[i];
i++;
}

Gobias
April 22nd, 2008, 10:22 PM
Well I know that this probably isn't right (or complete) at all, but here is what I have gotten so far. If anyone could help anymore that'd be great.
Also, thanks for all your advice so far.


#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

const int ARRAY_SIZE = 50;

void findAvg(ifstream& infile, int grades[]);
void belowAvg(ifstream&infile, int grades[], char studentname[]);

int main()
{
int index, maxIndex, highestScore, sum, i;
double average;
char studentname[ARRAY_SIZE];
int grades[ARRAY_SIZE];
ifstream infile;

infile.open("FileName");

if (!infile)
{
cout << "Cannot open the input file."
<< endl;
return 1;
}
i=0;
while (!infile.eof())
{
infile >> studentName[i] >> grades[i];
i++;
}

findAvg(infile, grades);

belowAvg(infile, grades, studentName);

return 0;
}

void findAvg(ifstream&infile, int grades[])
{
sum = 0;
maxIndex = 0;
for (index = 0; index < 50; index++)
if (grades[maxIndex] < grades[index])
maxIndex = index;
highestScore = grades[maxIndex];
sum = sum + grades[index];
average = sum / 50;

cout << "Class average is: "
<< average << endl;

cout << "Highest Test Score is: "
<< highestScore << endl;

return 0;
}

void belowAvg(ifstream&infile, int grades[], char studentname[]);
{
for (index = 0; index < 50; index++)
if (grades[index] < average)
cout << studentName[index] << " ";

return 0;
}

Lindley
April 22nd, 2008, 10:51 PM
Offhand, I see.....

1) In main(), you're doing i++. I suspect you want i to start at 0, but you never set this----i could be anything to begin with!
2) Your functions lack variable declarations. How is the machine supposed to know what type maxIndex, sum or average should be?
3) You're missing {} in some places. Indenting isn't enough. You can only skip the {} if a block contains only a single line.
4) Since grades are ints, presumably sum will be an int to. However, you probably don't want average to be an int-----so you shouldn't be dividing an int by an int to get it, because that will give you an int! Cast 50 to double in that division.
5) Speaking of 50, you're using it hard-coded in several places even though you have ARRAY_SIZE defined to that value. If you're going to bother defining such a const variable, use it!
6) What purpose is served by passing the ifstream to the functions?

7stud
April 23rd, 2008, 02:23 AM
7) When you read from a file, your read statement should be the while loop conditional:



while(infile>>x>>y)
{


}
infile>> will return something that will be considered true if the read was succesfull. If the read was unsuccessful for any reason, including hitting the end of the file, then infile>> will return something tha is considered false, which will terminate the loop.

On the other hand, if you do this:

while (!infile.eof())
{
infile >> studentName[i] >> grades[i];
i++;
}
if an error occurs while reading from the file, you will get stuck in an infinite loop. You won't be at the end of the file when the error occurs, and after the error occurs, you won't be able to read from the file anymore, so you will never get to the end of the file, and the while loop will never terminate.

STLDude
April 23rd, 2008, 09:44 AM
Again, seeing all this issues with just reading file data correctly (adding that from some posters who posted wrong samples) underscores my point than many beginners spend large amount of time on this (file reading) and neglect the main part of the assignment and end up with bugs in all places, where as I stated before, concentrate on your student problem, feed hard coded data, solve it and then and only then code your file reading routine. This way you are assured that you logic to process grades, scores, etc is working with any data you want to test.

GCDEF
April 23rd, 2008, 09:53 AM
Again, seeing all this issues with just reading file data correctly (adding that from some posters who posted wrong samples) underscores my point than many beginners spend large amount of time on this (file reading) and neglect the main part of the assignment and end up with bugs in all places, where as I stated before, concentrate on your student problem, feed hard coded data, solve it and then and only then code your file reading routine. This way you are assured that you logic to process grades, scores, etc is working with any data you want to test.

But both parts of the program have to be there for it to be complete according to the spec. He can get the algorithm done but that won't change the amount of time he's spending on the file handling.

Zaccheus
April 24th, 2008, 05:34 AM
I agree that the first step should be to read in the data. When the data can be read in (and the array(s) can be correctly dumped to cout), THEN the next step can be attempeted.

exterminator
April 26th, 2008, 12:40 AM
But both parts of the program have to be there for it to be complete according to the spec. He can get the algorithm done but that won't change the amount of time he's spending on the file handling.I agree. Both parts, reading a file and working with the data are orthogonal to each other, independent of each other. And any of those two can be tackled in isolation. There's a third component wherein the data from file is to be sent into the business logic of the code. This third one can be tackled later (but dummy data would be needed to test the program logic).

If one is doing test driven developement (or even if one is writing unit tests for their code and functionality) in a more general case, the above is the automatic choice of the way to go. This is my view, in a more general scenario and not just limited to the OP's case.

Which one you do before becomes immaterial and both are important aspects of the actual functional program. You cannot do without either of them and none of them has as much of an overlap with the other so as to cause any issues about which one should be taken up first.

STLDude
April 26th, 2008, 01:57 AM
If this is work, they yes, both steps need to be completed and in principal it does not matter which one first.

My main point here was that; I see this quite often coming from students where they have home work assignment as in this case who are fairly new to programming, not having much or any experience and just make harder on themselves by mixing file input and the logic of the assignment, with end results being that it took far longer to finish.

If you read over this thread you will see that even seemingly simple think like reading from the file was given wrong advice. The end result of the assignment is to calculate something, etc, thus do that first (using stable hard coded data) and just in case you run out of time before your due data you at least got it done in spirit, and as teacher I would far more impressed with that piece, rather then student showing me just reading data from the file.

Again, please do not mix what I'm saying here with professional work environment, this is mostly geared towards student.

exterminator
April 26th, 2008, 02:16 AM
If this is work, they yes, both steps need to be completed and in principal it does not matter which one first.

My main point here was that; I see this quite often coming from students where they have home work assignment as in this case who are fairly new to programming, not having much or any experience and just make harder on themselves by mixing file input and the logic of the assignment, with end results being that it took far longer to finish.

If you read over this thread you will see that even seemingly simple think like reading from the file was given wrong advice. The end result of the assignment is to calculate something, etc, thus do that first (using stable hard coded data) and just in case you run out of time before your due data you at least got it done in spirit, and as teacher I would far more impressed with that piece, rather then student showing me just reading data from the file.

Again, please do not mix what I'm saying here with professional work environment, this is mostly geared towards student.Your views make sense as well, STLdude. I did not disagree to it. And what you say is not just limited to beginners/students. Most of the times you will see applications where 2 layers/components which are completely independent of each other, are highly cluttered/coupled together making it very hard to change to adapt to changing requirements/maintain and even understand the code.

The basic thing that we all seem to agree to is that independent parts of the program should be taken up in isolation and tested before adding the "glue" layer that makes them interact with each other. That is the essence of your suggestion as well as GCDEF's. So, basically, both viewpoints are non-conflicting.

Preparing dummy/test data is also helpful in a lot ways. You get to test your application for both good as well as bad data. You become more aware of the data format. It makes the data format contract more explicit and documented in lack of documentation (this is all in general scenarios and not restricted here but are helpful things to note for a beginner as well). And may be in more ways as a result of which, one gets more robust code, well-designed isolated parts which are easy to maintain/enhance and adapt to changes which are all very important aspects of software development in general but pretty hard to get right.