Cornflakes Posted March 18, 2011 Share Posted March 18, 2011 Sigh. I am in this pitiful little Computer Science 101 class, and there's one question that I am struggling with, but I refuse to let it beat me!! Complete the following program: // File Name: 1170_8_9b.cc // This program computes the average, the highest, and // the lowest scores of a test for a class. // All the scores are stored in a data file (one score per line) // called "cla8c.dat". #include <iostream> #include <fstream> using namespace std; // declare function "ProcessTests" here _____________________________________________________________ int main() { float highest, //the highest score in the file lowest, //the lowest score in the file average; //the average score in the file ifstream datafile; //an input file stream object //open the data file datafile.open("/var/local/atol-data/programs/dataFiles/cla8c.dat"); // fill in the activation statement for function "ProcessTests" here ___________________________________________________________________________ cout << "The highest score is " << highest << endl; cout << "The lowest score is " << lowest << endl; cout << "The average score is " << average << endl; datafile.close(); return 0; } // ProcessTests // This function finds the highest, lowest, and the average test score // When you pass an input file stream to a function, it needs to be a call-by-reference argument. // define the function here void ProcessTests( , , , ) { } so I need to fill in the blanks. I understand what a function is. Call-by-value and call-by-reference get me a little confused but I think I got it. And I know how to read in from an external data file. But I'm having trouble putting it altogether. Any ideas? :guiltysmiley: I would post what my guesses are but I don't want to embarrass myself further. And thanks :) Quote Link to comment Share on other sites More sharing options...
antiaircraft Posted March 18, 2011 Share Posted March 18, 2011 Well, first things first, people really should standardise on the .cpp extension for C++ source code. *grumbles* :P You really shouldn't be too worried about posting your guesses - I mean, this is programming, there are lots of different ways to do the same thing anyway. ;) That said, this seems pretty simple. I prefer C-style pointers rather than references personally, but I suppose that's just me wanting to do everything the hard way. xD I'll go with references and see how things turn out. Your function prototype (up at the top) should probably look something like this: void ProcessTests(ifstream& readThis, float& max, float& min, float& mean); So you pass the function the file stream which you can use to read your data file, and then references to the three variables where you store your results. The function can then be called from the main() function like so: ProcessTests(datafile, highest, lowest, average); This tells the function to read information from the datafile stream, and store the results in the highest, lowest, and average variables. Then your function definition should look something like this: void ProcessTests(ifstream& readThis, float& max, float& min, float& mean) { float current = -1.0; float sum = 0.0; unsigned int num_read = 0; // Initialise, assuming scores are between zero and one hundred - the question really should be more specific max = -0.1; // Initial maximum is just below the lowest possible score min = 100.1; // Initial minimum is just above the highest possible score // This way, the inital values will be immediately overwritten by sane input =) // If you see impossible scores, you know there's something wrong with your input // Yeah, my comments get really wordy sometimes while (readThis) // While there's still stuff to read { readThis >> current; num_read++; sum += current; if (current > max) { max = current; } if (current < min) { min = current; } } mean = sum / static_cast<float>(num_read); } I'm a little rusty in C++, so my code may need one or two corrections and some touching up. Hopefully it's helpful and not too confusing. :) Quote Link to comment Share on other sites More sharing options...
Cornflakes Posted March 18, 2011 Author Share Posted March 18, 2011 Thanks(!!) for responding, I'll try it when I get home later! And thanks for not being mean to me like most C++ers :) "people really should standardise on the .cpp extension for C++ source code" Er, I have no idea what that is, is that something that's my fault? Quote Link to comment Share on other sites More sharing options...
antiaircraft Posted March 18, 2011 Share Posted March 18, 2011 Nope, I'm just being obsessive compulsive. :P C++ source code files can be named something.cc, something.cpp, something.c++, something.cxx, and so on... .cpp is the de-facto standard and something I wish everyone would agree on. And it looks like you've fallen in with the wrong crowd of C++ers. xD But I'm glad I could be of service. Good luck! And feel free to ask about anything else you run into. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.