CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: Confusion++

  1. #1
    Join Date
    Apr 2008
    Posts
    3

    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.

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Confusion++

    Quote 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.

    - 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.

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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.

  4. #4
    Join Date
    Apr 2008
    Posts
    3

    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?

  5. #5
    Join Date
    Feb 2002
    Posts
    4,640

    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

  6. #6
    Join Date
    May 2007
    Posts
    811

    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.

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Confusion++

    Quote 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.

  8. #8
    Join Date
    Apr 2008
    Posts
    26

    Re: Confusion++

    Quote 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++;
    }

  9. #9
    Join Date
    Apr 2008
    Posts
    3

    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;
    }

  10. #10
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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?

  11. #11
    Join Date
    Oct 2004
    Posts
    296

    Re: Confusion++

    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:
    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.

  12. #12
    Join Date
    May 2007
    Posts
    811

    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.

  13. #13
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Confusion++

    Quote 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.

  14. #14
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    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.
    My hobby projects:
    www.rclsoftware.org.uk

  15. #15
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: Confusion++

    Quote 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.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured