|
Editted, I suck at sorting anyone care to help? I get an error that tells me I can't convert parameter 1 of 's_sort' from 'int' to 'int []'
If anything if you can help, point out what I'm doing wrong  + Show Spoiler + #include <iostream> #include <fstream> #include <string> using namespace std;
struct Student_Statistics { int student_id; string name; double quiz1, quiz2, quiz3, quiz4, quiz5; double midterm1, midterm2, final; double average; char letter_grade; };
void s_sort( int x[],string y[],double a[],double b[], int n); double find_lowest(Student_Statistics student); int main() { Student_Statistics Student[30]; ifstream in_file; char grade_file[80]; int i = 0; int j; int k; double all_averagesA = 0; double all_averages;
cout << "Enter an input file: "; cin >> grade_file; in_file.open(grade_file);
cout << "---------------------------------------------------" << endl; cout << " Individual Course Report" << endl; cout << "---------------------------------------------------" << endl;
while(!in_file.eof()) { in_file >> Student[i].student_id >> Student[i].name >> Student[i].quiz1 >> Student[i].quiz2 >> Student[i].quiz3 >> Student[i].quiz4 >> Student[i].quiz5 >> Student[i].midterm1 >> Student[i].midterm2 >> Student[i].final;
cout.setf(ios::showpoint); cout.setf(ios::fixed); cout.precision(2); if(find_lowest(Student[i]) >= 90) { Student[i].letter_grade = 'A'; } if((find_lowest(Student[i]) < 90) && (find_lowest(Student[i]) >= 80)) { Student[i].letter_grade = 'B'; } if((find_lowest(Student[i]) < 80) && (find_lowest(Student[i]) >= 70)) { Student[i].letter_grade = 'C'; } if((find_lowest(Student[i]) < 70) && (find_lowest(Student[i]) >= 60)) { Student[i].letter_grade = 'D'; } if(find_lowest(Student[i]) < 60) { Student[i].letter_grade = 'F'; } s_sort(Student[i].student_id, Student[i].name, find_lowest(Student[i]), Student[i].letter_grade, i); cout << Student[i].student_id << ' ' << Student[i].name << " - " << find_lowest(Student[i]) <<" (" << Student[i].letter_grade << ')' << endl; i++; } cout << endl; cout << "---------------------------------------------------" << endl; cout << " Statistics" << endl; cout << "---------------------------------------------------" << endl; cout << "Number of Students: " << i << endl; for(j=0;j<i;j++) { all_averagesA+=find_lowest(Student[j]); all_averages=all_averagesA/i;
} cout.setf(ios::showpoint); cout.setf(ios::fixed); cout.precision(1); cout << "Average of all students: " << all_averages << endl; cout << "Grade Distribution (histrogram)" << endl;
cout << " A: "; for(k=0;k<i;k++) { if(Student[k].letter_grade == 'A') { cout << '*'; } } cout << endl;
cout << " B: "; for(k=0;k<i;k++) { if(Student[k].letter_grade == 'B') { cout << '*'; } } cout << endl;
cout << " C: "; for(k=0;k<i;k++) { if(Student[k].letter_grade == 'D') { cout << '*'; } } cout << endl;
cout << " D: "; for(k=0;k<i;k++) { if(Student[k].letter_grade == 'D') { cout << '*'; } } cout << endl;
cout << " F: "; for(k=0;k<i;k++) { if(Student[k].letter_grade == 'F') { cout << '*'; } } cout << endl;
return 0; }
double find_lowest(Student_Statistics student) { if((student.quiz1 < student.quiz2) && (student.quiz1 < student.quiz3) && (student.quiz1 < student.quiz4) && (student.quiz1 < student.quiz5)) { student.average = ((student.quiz2*(.2) + student.quiz3*(.2) + student.quiz4*(.2) + student.quiz5*(.2) + student.midterm1*(.4) + student.midterm2*(.4) + student.final*(.4))/128)*100; } else if((student.quiz2 < student.quiz1) && (student.quiz2 < student.quiz3) && (student.quiz2 < student.quiz4) && (student.quiz2 < student.quiz5)) { student.average = ((student.quiz1*(.2) + student.quiz3*(.2) + student.quiz4*(.2) + student.quiz5*(.2) + student.midterm1*(.4) + student.midterm2*(.4) + student.final*(.4))/128)*100; } else if((student.quiz3 < student.quiz1) && (student.quiz3 < student.quiz2) && (student.quiz3 < student.quiz4) && (student.quiz3 < student.quiz5)) { student.average = ((student.quiz1*(.2) + student.quiz2*(.2) + student.quiz4*(.2) + student.quiz5*(.2) + student.midterm1*(.4) + student.midterm2*(.4) + student.final*(.4))/128)*100; } else if((student.quiz4 < student.quiz1) && (student.quiz4 < student.quiz2) && (student.quiz4 < student.quiz3) && (student.quiz4 < student.quiz5)) { student.average = ((student.quiz1*(.2) + student.quiz2*(.2) + student.quiz3*(.2) + student.quiz5*(.2) + student.midterm1*(.4) + student.midterm2*(.4) + student.final*(.4))/128)*100; } else if((student.quiz5 < student.quiz1) && (student.quiz5 < student.quiz2) && (student.quiz5 < student.quiz3) && (student.quiz5 < student.quiz4)) { student.average = ((student.quiz1*(.2) + student.quiz2*(.2) + student.quiz3*(.2) + student.quiz4*(.2) + student.midterm1*(.4) + student.midterm2*(.4) + student.final*(.4))/128)*100; }
return student.average; } void s_sort(double x[],string y[],double a[],double b[], int n) { int m; // keep the index of current smallest value double hold; string hold2; double hold3; double hold4;
for (int k=0; k<=n-2; k++) { m = k; for (int j=k+1; j <= n-1; j++) { if (x[j] > x[m]) m = j; }
hold = x[m]; hold2 = y[m]; hold3 = a[m]; hold4 = b[m]; x[m] = x[k]; y[m] = y[k]; a[m] = a[k]; b[m] = b[k]; x[k] = hold; y[k] = hold2; a[k] = hold3; b[k] = hold4;
}
return; }
   
|
Why is find_lowest declared as void, even though it returns something?
And why does it have a parameter, even though when you call it, you pass nothing?
|
In addition to what huameng said you also are using variable 'i' inside of find_lowest, but 'i' isn't in that scope.
|
Man, there's nothing more annoying than trying to find out why your code won't compile.
|
Oh also, looks like you have some capitalized Student[i] when you probably mean lowercase student[i] near the end of find_lowest
|
omggggggggg so much to fix, lol...
|
From the code it looks like find_lowest should take in a single 'Student_Statistics' variable. so declared as such double find_lowest(Student_Statistics student)
so you would call it like so; find_lowest(Student[i])
then inside of find_lowest get rid of all the references to 'student[i]' and just use 'student' instead
|
You seem to have all kinds of scope problems. It's like you're treating every variable like it was global or something, like "i" being accessed in find_lowest(). As has already been mentioned you designed find_lowest() to take a parameter but you give it no parameters when you use it. Lots of problems here.
|
looks like the main thing my compiler is saying is:
'student' is an undeclared identifier -- i dont even know where to begin to fix that.. 'find lowest' illegal use of the type 'void
also, updated the code.
|
just rewrite. It takes like 1/12 the time it took to write originally and it ends up being better code
|
On December 10 2009 11:53 Dave[9] wrote: looks like the main thing my compiler is saying is:
'student' is an undeclared identifier -- i dont even know where to begin to fix that.. 'find lowest' illegal use of the type 'void
also, updated the code. You are using something that returns void in your cout. So cout is looking for something to print and it finds a function that returns void and it's confused.
Take a look at my suggestions and see if they help.
|
Katowice25012 Posts
this would be the easiest thing in sas you should make that part of your program imo
|
On December 10 2009 11:53 Dave[9] wrote: looks like the main thing my compiler is saying is:
'student' is an undeclared identifier -- i dont even know where to begin to fix that.. 'find lowest' illegal use of the type 'void
also, updated the code. When you declare the function of type "void" you are saying that the function will return nothing. Your function returns Student.average which is a variable of type "double".
|
this is messy lol
try something like this, only minor changes but now it's compiling. I can't test it since I don't have your input files but you can take it form here lol: + Show Spoiler + #include <iostream> #include <fstream> #include <string> using namespace std;
struct Student_Statistics { int student_id; string name; double quiz1, quiz2, quiz3, quiz4, quiz5; double midterm1, midterm2, final; double average; };
double find_lowest(Student_Statistics student);
int main() { Student_Statistics Student[30]; ifstream in_file; char grade_file[80]; int i = 0;
cout << "Enter an input file: "; cin >> grade_file; in_file.open(grade_file);
cout << "---------------------------------------------------" << endl; cout << " Individual Course Report" << endl; cout << "---------------------------------------------------" << endl;
while(!in_file.eof()) { in_file >> Student[i].student_id >> Student[i].name >> Student[i].quiz1 >> Student[i].quiz2 >> Student[i].quiz3 >> Student[i].quiz4 >> Student[i].quiz5 >> Student[i].midterm1 >> Student[i].midterm2 >> Student[i].final;
cout << Student[i].student_id << Student[i].name << find_lowest(Student[i]) << endl; i++; } return 0; }
double find_lowest(Student_Statistics student) { if((student.quiz1 < student.quiz2) && (student.quiz1 < student.quiz3) && (student.quiz1 < student.quiz4) && (student.quiz1 < student.quiz5)) { student.average = (student.quiz2 + student.quiz3 + student.quiz4 + student.quiz5)/4; } else if((student.quiz2 < student.quiz1) && (student.quiz2 < student.quiz3) && (student.quiz2 < student.quiz4) && (student.quiz2 < student.quiz5)) { student.average = (student.quiz1 + student.quiz3 + student.quiz4 + student.quiz5)/4; } else if((student.quiz3 < student.quiz1) && (student.quiz3 < student.quiz2) && (student.quiz3 < student.quiz4) && (student.quiz3 < student.quiz5)) { student.average = (student.quiz1 + student.quiz2 + student.quiz4 + student.quiz5)/4; } else if((student.quiz4 < student.quiz1) && (student.quiz4 < student.quiz2) && (student.quiz4 < student.quiz3) && (student.quiz4 < student.quiz5)) { student.average = (student.quiz1 + student.quiz2 + student.quiz3 + student.quiz5)/4; } else if((student.quiz5 < student.quiz1) && (student.quiz5 < student.quiz2) && (student.quiz5 < student.quiz3) && (student.quiz5 < student.quiz4)) { student.average = (student.quiz1 + student.quiz2 + student.quiz3 + student.quiz4)/4; }
return student.average; }
|
On December 10 2009 11:57 Eti307 wrote:this is messy lol try something like this, only minor changes but now it's compiling. I can't test it since I don't have your input files but you can take it form here lol: + Show Spoiler + #include <iostream> #include <fstream> #include <string> using namespace std;
struct Student_Statistics { int student_id; string name; double quiz1, quiz2, quiz3, quiz4, quiz5; double midterm1, midterm2, final; double average; };
double find_lowest(Student_Statistics student);
int main() { Student_Statistics Student[30]; ifstream in_file; char grade_file[80]; int i = 0;
cout << "Enter an input file: "; cin >> grade_file; in_file.open(grade_file);
cout << "---------------------------------------------------" << endl; cout << " Individual Course Report" << endl; cout << "---------------------------------------------------" << endl;
while(!in_file.eof()) { in_file >> Student[i].student_id >> Student[i].name >> Student[i].quiz1 >> Student[i].quiz2 >> Student[i].quiz3 >> Student[i].quiz4 >> Student[i].quiz5 >> Student[i].midterm1 >> Student[i].midterm2 >> Student[i].final;
cout << Student[i].student_id << Student[i].name << find_lowest(Student[i] << endl; i++; } return 0; }
double find_lowest(Student_Statistics student) { if((student.quiz1 < student.quiz2) && (student.quiz1 < student.quiz3) && (student.quiz1 < student.quiz4) && (student.quiz1 < student.quiz5)) { student.average = (student.quiz2 + student.quiz3 + student.quiz4 + student.quiz5)/4; } else if((student.quiz2 < student.quiz1) && (student.quiz2 < student.quiz3) && (student.quiz2 < student.quiz4) && (student.quiz2 < student.quiz5)) { student.average = (student.quiz1 + student.quiz3 + student.quiz4 + student.quiz5)/4; } else if((student.quiz3 < student.quiz1) && (student.quiz3 < student.quiz2) && (student.quiz3 < student.quiz4) && (student.quiz3 < student.quiz5)) { student.average = (student.quiz1 + student.quiz2 + student.quiz4 + student.quiz5)/4; } else if((student.quiz4 < student.quiz1) && (student.quiz4 < student.quiz2) && (student.quiz4 < student.quiz3) && (student.quiz4 < student.quiz5)) { student.average = (student.quiz1 + student.quiz2 + student.quiz3 + student.quiz5)/4; } else if((student.quiz5 < student.quiz1) && (student.quiz5 < student.quiz2) && (student.quiz5 < student.quiz3) && (student.quiz5 < student.quiz4)) { student.average = (student.quiz1 + student.quiz2 + student.quiz3 + student.quiz4)/4; }
return student.average; }
Nice. That's basically exactly what I was suggesting. I was just working on putting it into pastebin but now that you have it, I don't need to. 
|
Alright, you guys gave me enough hints that I arrived at Eti's code. thank you guys sooooooo much.
|
|
I didn't want to completely change his code so I just modified it so it would work.
Just so you know Dave, there are way better ways to do this. But you're just starting from what I see. We've all been there
|
In addition to the above comments, the find_lowest is broken: 1) The algorithm for finding the average of the four highest scores is wrong. Suppose I gave it a student with scores {1., 1., 2., 3., 4.}, where index i corresponds to student.quiz$i. You're likely to get a garbage response because the "average" field isn't explicitly initialized. 2) Assigning to student.average doesn't do anything, and is rather misleading (it's not incorrect for this program, but should you extend it in the future...). Either pass in student via reference/pointer, or create a local double variable to hold the result.
|
If you're going to be programming and making spelling mistakes, make sure you learn how to use find/replace or regex, it fixes little problems like that very fast.
|
lol this is awesome. i haven't done anything with C++ since 9th grade, but i like how everyone comes in to help
|
Wow... Java is almost exactly the same. I never noticed.
edit: Not sure but Java is very sensitive with everything, but seems you randomly capitalized "student" in some of the lines that arent related to the name of the program.
I could be wrong, only beginner Java here. :/
|
okay, I got way further in my code, and now I'm stuck on the sorting part of it, can more of you have a look?
Love you guys
|
The reason it doesn't work right now is you're passing s_sort a single students grades, when the function is expecting arrays containing all of the students grades. Also, you're calling s_sort every time you're reading in an individual students info, when it should only need to be called once.
I would advise changing the s_sort function to accept your array of Student_Statistic structures, instead of a bunch of arrays that represent the fields in those structures. This way, after all of the students have been read in from the file, you can then pass s_sort your array of students, and it will come back sorted.
so instead of:
void s_sort( int x[],string y[],double a[],double b[], int n);
you'd have something like:
void s_sort(Student_Statistics[] students);
Inside the function, you'd have to change x[whatever] to students[whatever].student_id (assuming the x array was representing student_id).
|
|
On December 10 2009 14:17 Disregard wrote: Wow... Java is almost exactly the same. I never noticed.
edit: Not sure but Java is very sensitive with everything, but seems you randomly capitalized "student" in some of the lines that arent related to the name of the program.
I could be wrong, only beginner Java here. :/
java's pretty similar just a lot more fun to program in god i hate C so much programming in C is like visiting a dentist who hates you >.<
btw it seems ur current problem is that you're passing .studentID which is an int but your method's expecting an array of ints because that's how you declared it (although in your actual method you're expecting an array of doubles) stay consistent ;o
also, better variable names never hurt anyone =) arbitrary letters are almost never the way to go aside from for loops or something, and even then .. :p
|
This is your definition of s_sort
void s_sort( int x[],string y[],double a[],double b[], int n);
This is what you are passing in
s_sort(Student[i].student_id, Student[i].name, find_lowest(Student[i]), Student[i].letter_grade, i);
All but your last input parameters are incorrectly typed.
|
simple case of mismatching function arguments.
don't write your code like that. it's messy and when shits go wrong you will kill yourself.
writing any kinda of algorithm in C/C++ is a pain but type checking is very important, you need to get yourself around it.
|
you can save yourself some headaches if you use container classes like std::vector<> instead of the built-in [] arrays.
|
On December 15 2009 17:20 yh8c4 wrote: you can save yourself some headaches if you use container classes like std::vector<> instead of the built-in [] arrays. a crutch wont teach you how to walk.
|
|
|
|