|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
#1 |
|
Mondsreitersmann
Join Date: Jul 1999
Location: Skingrad
Posts: 8,781
|
Non-compiling program
I am trying to implement a grading function in this program but I am stuck again with the compute_grade function.
Is that formula I am using right, or is there a problem with it? Any ideas anyone? TIA
__________________
Darum still, füg' ich mich, wie Gott es will. Nun, so will ich wacker streiten, und sollt' ich den Tod erleiden, stirbt ein braver Reitersmann. |
|
|
|
|
|
#2 |
|
Registered User
Join Date: Nov 2001
Posts: 1,965
|
looks fine to me, are you having any problems?
I didn't try to compile or run the program, if you have any specific problems, post to make things easier for me. The formula looks fine. Last edited by aym; 03-28-2004 at 01:08 PM. |
|
|
|
|
|
#3 |
|
Mondsreitersmann
Join Date: Jul 1999
Location: Skingrad
Posts: 8,781
|
This is what the compiler complains about:
CIS4.cpp: In function `void compute_mark(int, int*, int*, int)': CIS4.cpp:223: invalid types `int[int]' for array subscript Execution terminated |
|
|
|
|
|
#4 |
|
Registered User
Join Date: Nov 2001
Posts: 1,965
|
It complaining about (float)final[k], final is declared as int in the parameter list.
I think you mean: Code:
void compute_mark(int index, int mark1[], int mark2[], int final[]) |
|
|
|
|
|
#5 |
|
Registered User
Join Date: Nov 2001
Posts: 1,965
|
As for the grade formula, to get rid of the warning, use .25f instead of just .25, .25 is double while .25f is float.
One final thing, you use compute_mark in the main loop without any params, so you'll get a linker error, you need to fix this by passing arguments to the function. |
|
|
|
|
|
#6 |
|
Mondsreitersmann
Join Date: Jul 1999
Location: Skingrad
Posts: 8,781
|
![]() If I don't use the type casting it keeps complaining about it. |
|
|
|
|
|
#7 |
|
Registered User
Join Date: Nov 2001
Posts: 1,965
|
I think you mean .25 and .5 in the formula, right? Then see my prev post
|
|
|
|
|
|
#8 |
|
Mondsreitersmann
Join Date: Jul 1999
Location: Skingrad
Posts: 8,781
|
Yep. Regardless of that, I am getting some strange results when I run the program.
See, the first mark would be the easiest as this student has: mark1 = 100 mark2 = 100 final = 100 So, the result would be grade = 100, right? Alas, it's not. Result is 0.00 Results of this formula for the others are even worse. For example, student 2 has: mark1 = 90 mark2 = 87 final = 69 grade = 10193237897557078000000000000000000.00
|
|
|
|
|
|
#9 |
|
Registered User
Join Date: Nov 2001
Posts: 1,965
|
function compute_mark computes the grade and stores it in the local variable grade, when the function exits, grade goes out of the scope.
You need to create another array for the grades in the main function, like those for mark1, mark2, and final, and store the results there. And I couldn't find any way to print the grade in the program, I used the debugger, and for mark1 = mark2 = final = 100, the computed grade is 100, you just need to store it. I think you added something like: "cout << grade[k];" after the for loop, correct? this explain the results you got, when the loop exits, k's value isn't valid, and you get some random result. |
|
|
|
|
|
#10 |
|
Mondsreitersmann
Join Date: Jul 1999
Location: Skingrad
Posts: 8,781
|
I updated the link with the changes made. I am trying to output the grade using the display_record() function.
|
|
|
|
|
|
#11 |
|
Registered User
Join Date: Nov 2001
Posts: 1,965
|
The link gives 404, problem solved I think?
|
|
|
|
|
|
#12 |
|
Mondsreitersmann
Join Date: Jul 1999
Location: Skingrad
Posts: 8,781
|
Nope. Not solved.
Strange, the link works for me in two different computers. |
|
|
|
|
|
#13 |
|
Registered User
Join Date: Nov 2001
Posts: 1,965
|
Found the problem.
In function compute_mark, you declare a local array called grade, and store the compued grade there, when the function returns, grade goes out of the scope. When calling display_record, you pass the array grade declared in main (which is different from grade in compute_mark), because you didn't assign anything to main's grade, it contains some random number. To solve the problem, you need to pass main's grade to compute_mark, so the computed mark is kept in memory for display_record to print it. Something like this: Code:
void compute_mark(int index, int mark1[], int mark2[], int final[], float grade[])
{
int k;
for (k=0; k<index; k++)
grade[k] = (.25 * (float)mark1[k]) + (.25 * (float)mark2[k]) +
(.5 * (float)final[k]);
}
Code:
compute_mark(index, mark1, mark2, final, grade);
|
|
|
|
|
|
#14 |
|
Mondsreitersmann
Join Date: Jul 1999
Location: Skingrad
Posts: 8,781
|
Thanks, aym, worked like a charm.
One last question: I am trying to implement a sort and search function to that program. You may have noticed the code in blue at the bottom, those are the functions for that. Any ideas how can I get it done? Sorry to bother you too much man, but I have been squeezing my brains with this program for too long and I am suffering from programmer's block now. Thank you. |
|
|
|
|
|
#15 |
|
Registered User
Join Date: Nov 2001
Posts: 1,965
|
The sort function sorts correctly, you only need to call it to sort the arrays.
Your program calls sort_record to sort the records, so you need to pass the arrays to it, and then call the sort function on the grade array to sort by grade, or call it on name array to sort by name. There is one small problem here, if you call sort on the grade array, the function will swap grades only, and won't swap names, you can solve the problem with something like this: Code:
if (grade[i] < grade[j]) {
swap(grade[i], grade[j]);
swap(name[i], name[j]);
// same for mark1, mark2, and so on
}
Code:
void swap(float& x, float& y) {
// ...
}
void swap(string& x, string& y) {
// ...
}
![]() Good luck. |
|
|
|
|
|
#16 |
|
Mondsreitersmann
Join Date: Jul 1999
Location: Skingrad
Posts: 8,781
|
Overload swap? Do I have to write a swap function for all types (string, float, int)?
|
|
|
|
|
|
#17 |
|
Registered User
Join Date: Nov 2001
Posts: 1,965
|
correct, since you have a string array (name), a float array (grade) and integer arrays (mark1, mark2, final).
When swapping 2 items in one array, you should swap them in the remaining arrays as well. |
|
|
|
|
|
#18 |
|
Mondsreitersmann
Join Date: Jul 1999
Location: Skingrad
Posts: 8,781
|
OK.
Here is the new version of the program with the added swap functions. Yet the compiler is complaining again: CIS4c.cpp: In function `void sort_record()': CIS4c.cpp:250: parse error before `,' token CIS4c.cpp:254: parse error before `,' token Execution terminated Also, did I implement well the sort function? TIA |
|
|
|
|
|
#19 |
|
Registered User
Join Date: Nov 2001
Posts: 1,965
|
Yes, the sort function is correct.
As for the errors: 1) Lines 250 and 254: When calling a function, you don't need to add param types, remove string, int and float. 2) Line 233: You need to pass student_name, student_number, mark1, mark2, final, grade, and no_of_elements to function sort_record. 3) Line 31: Don't forget to fix sort_record's declaration. 4) Line 85: Pass params to sort_record. 5) A cool feature in C++ is that you can use the same name for more than one function, and the compiler will select the proper function according to the params you pass. I renamed all swap_* functions to swap. I've attached the working version. If you know about structures, I suggest you put main's arrays in one structure, it makes things easier. |
|
|
|
|
|
#20 |
|
Mondsreitersmann
Join Date: Jul 1999
Location: Skingrad
Posts: 8,781
|
Worked like a charm again, aym.
![]() The program is far from over tho. I still have to implement a function to change names and marks, as well as to incorporate that stats program from the other thread in it. So it's a sure bet I'll keep bothering ya for another week. ![]() Thank you very much. |
|
|
|
|
|
#21 |
|
Registered User
Join Date: Nov 2001
Posts: 1,965
|
no problem, glad to help
|
|
|
|
|
|
#22 |
|
Mondsreitersmann
Join Date: Jul 1999
Location: Skingrad
Posts: 8,781
|
Just wanted to thank you for all your help, Aym. I finished the program and submitted it. Now I am done with C programming. Whoo-hoo!!
![]() Thanks again. |
|
|
|
|
|
#23 |
|
Member (7 bit)
|
Sorry to interrupt you Guys post but coming from a newbie if you two keep using this language i'll report you to someone who knows what the hell ya talking about.....Just admiring how deep some people think .....keep it up
|
|
|
|
|
|
#24 | |
|
Registered User
Join Date: Nov 2001
Posts: 1,965
|
Quote:
|
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|