-
April 10th, 2008, 02:31 PM
#1
Confusion++
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.
-
April 10th, 2008, 02:35 PM
#2
Re: Confusion++
Originally Posted by Gobias
I'm pretty new to C++.I have to write a program and I have absolutely no idea where to start.
Code:
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.
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.
-
April 10th, 2008, 02:42 PM
#3
Re: Confusion++
Start at the beginning. Step one. You need to read from a file. Figure out how to do that. Move on to step two.
-
April 10th, 2008, 03:17 PM
#4
Re: Confusion++
Can I test the program without actually having the file to read from, or will it crash unless I have that text file?
-
April 10th, 2008, 03:21 PM
#5
Re: Confusion++
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
-
April 10th, 2008, 03:29 PM
#6
Re: Confusion++
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.
-
April 10th, 2008, 03:36 PM
#7
Re: Confusion++
Originally Posted by STLDude
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.
-
April 10th, 2008, 05:08 PM
#8
Re: Confusion++
Originally Posted by GCDEF
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
Code:
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++;
}
-
April 22nd, 2008, 10:22 PM
#9
Re: Confusion++
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.
Code:
#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;
}
-
April 22nd, 2008, 10:51 PM
#10
Re: Confusion++
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?
-
April 23rd, 2008, 02:23 AM
#11
Re: Confusion++
7) When you read from a file, your read statement should be the while loop conditional:
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:
Code:
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.
Last edited by 7stud; April 23rd, 2008 at 02:28 AM.
-
April 23rd, 2008, 09:44 AM
#12
Re: Confusion++
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.
-
April 23rd, 2008, 09:53 AM
#13
Re: Confusion++
Originally Posted by STLDude
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.
-
April 24th, 2008, 05:34 AM
#14
Re: Confusion++
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.
-
April 26th, 2008, 12:40 AM
#15
Re: Confusion++
Originally Posted by GCDEF
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.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|