| KennethQ (6) | |||||||
In my computer science class, we were to take the following .txt file and arrange it so that it takes in the first two names in two separate strings and the numbers in an array with 12 elements.
After that, we were supposed to print a report that shows for each student the student's name, their average for the 12 grades, their high grade, their low grade, their median grade and finally a class average grade (going vertically down each grade column). The code below compiles and runs with only one error(as far as I know), which is the segfault at the end. I have no idea why it's doing this since I'm still new to c++. Can anyone help me? My code so far:
What I get:
| |||||||
|
|
|||||||
| Catfish3 (279) | |
|
A segmentation fault usually happens when you try to use memory that you shouldn't. In your case the culprit is... for (int i = 1 ;i <= iSize ;i++)... because in C++ counting starts from 0, not 1. So the first element in the grades array is grades[0], not grades[1].Last element is grades[iSize - 1], not grades[iSize].Corrected for() loop: for (int i = 0 ;i < iSize ;i++) | |
|
|
|
| KennethQ (6) | |||
I realized that and tried it too but the results turned out to be worse than before, unfortunately :/. However when i change it to what you put as the corrected for loop, it outputted:
Instead. | |||
|
|
|||
| Catfish3 (279) | |
|
Hmm. In your findGrades() function, you don't have a limit for index, nor do you reset it between checks. So you may get out of bounds there, too. | |
|
|
|
| KennethQ (6) | |
| So i should set index into each for loop as the reset between each loop? Also i would have thought that the error occurs before it even reaches the first function call. | |
|
|
|
| Catfish3 (279) | |||
Well all you need to do is change the start to: for (index = 0;.Even then, index could possibly go outside range, because you don't check it against a limit (which is the array's size).
You could add cerr << "I'm in function x()" << endl; in the functions to be sure. | |||
|
|
|||
| KennethQ (6) | |||
Ok, i'll try that. Also I think instead of having to even deal with the unlimited index, I could just change every for statement into f statements like so:
And then put a do/while that says to do that while index < 12. Could that possibly work? | |||
|
|
|||
| Catfish3 (279) | |
That doesn't work because you're not checking the minimum value against iMin.So if you have an iArray of {3, 4, 5, 6, 100, 200}, at the end iMin would be 100 instead of 3.And then, what's the purpose of doing this, if you sort the grades array? If it's sorted by sortArray(), just grab its first and last elements for iMin and iMax respectively.I'd like to go a bit off topic. How free are you, to use the C++ language at its fullest? C++ already has your functions in its library, which can sort for you, swap two values for you, and find the minimum and maximum values in an array for you. http://cplusplus.com/reference/algorithm/sort/ http://cplusplus.com/reference/algorithm/swap/ http://cplusplus.com/reference/algorithm/min_element/ http://cplusplus.com/reference/algorithm/max_element/ If you're totally new to C++, they may be tricky to use at first. But your program will end up being cleaner, and simpler. | |
|
|
|
| KennethQ (6) | |||
I can use any means necessary to get the job done. I also forgot to change my old program function to this one:
Showing what I was actually going to do. Actually i hadn't even thought of taking those values from the sorted array, until you mentioned it i'll change that too. | |||
|
|
|||
| Catfish3 (279) | |||
|
Here's how I would do it, with some minor differences. This code isn't meant to intimidate, but to inspire. Whatever questions you have, I'll be around to answer them. From what I tested, it runs on your input data, and I didn't see obvious bugs. (If anyone does, please point them out.)
| |||
|
|
|||
| Chervil (1206) | |
Function float medianGrade() is returning the average (or arithmetic mean), not the median.
| |
|
|
|
| Catfish3 (279) | |
|
@ Chervil: thanks, could you point me to the formula for that? | |
|
|
|
| Chervil (1206) | |
| @ Catfish3 To be frank, it's years since I had to use this, so I'm dependent on what Google has come up with: http://www.mathsisfun.com/median.html | |
|
|
|
| KennethQ (6) | |
| I see... Well, thank you for the program, I'll add some parts of that program to mine and take out what I feel could be unnecessary or not as well made. After I understand it all of course. If I have any questions then I'll bring them up here. Thanks again!! | |
|
|
|
| Catfish3 (279) | |||
|
Thanks Chervil. Here's the updated version. Posted in full because some things changed outside the Student struct as well.
| |||
|
|
|||
| Catfish3 (279) | |||
|
@ KennethQ: there is a lot of room left for improvement. For instance it doesn't even check if the file was successfully opened. Happy bug hunting! Edit: minor bugfix: If the input file doesn't end with a newline, getline() will hang (for me). I don't know why it does, but this seems to fix it.
| |||
|
Last edited on
|
|||