Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 03-27-2004, 09:00 PM   #1
Mondsreitersmann
 
Nuclear Krusader's Avatar
 
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.
Nuclear Krusader is offline   Reply With Quote
Old 03-28-2004, 01:02 PM   #2
aym
Registered User
 
aym's Avatar
 
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.
aym is offline   Reply With Quote
Old 03-28-2004, 01:15 PM   #3
Mondsreitersmann
 
Nuclear Krusader's Avatar
 
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
Nuclear Krusader is offline   Reply With Quote
Old 03-28-2004, 01:28 PM   #4
aym
Registered User
 
aym's Avatar
 
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[])
aym is offline   Reply With Quote
Old 03-28-2004, 01:35 PM   #5
aym
Registered User
 
aym's Avatar
 
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.
aym is offline   Reply With Quote
Old 03-28-2004, 01:36 PM   #6
Mondsreitersmann
 
Nuclear Krusader's Avatar
 
Join Date: Jul 1999
Location: Skingrad
Posts: 8,781


If I don't use the type casting it keeps complaining about it.
Nuclear Krusader is offline   Reply With Quote
Old 03-28-2004, 01:37 PM   #7
aym
Registered User
 
aym's Avatar
 
Join Date: Nov 2001
Posts: 1,965
I think you mean .25 and .5 in the formula, right? Then see my prev post
aym is offline   Reply With Quote
Old 03-28-2004, 01:52 PM   #8
Mondsreitersmann
 
Nuclear Krusader's Avatar
 
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

Nuclear Krusader is offline   Reply With Quote
Old 03-28-2004, 05:12 PM   #9
aym
Registered User
 
aym's Avatar
 
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.
aym is offline   Reply With Quote
Old 03-28-2004, 07:16 PM   #10
Mondsreitersmann
 
Nuclear Krusader's Avatar
 
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.
Nuclear Krusader is offline   Reply With Quote
Old 03-29-2004, 11:39 AM   #11
aym
Registered User
 
aym's Avatar
 
Join Date: Nov 2001
Posts: 1,965
The link gives 404, problem solved I think?
aym is offline   Reply With Quote
Old 03-29-2004, 02:58 PM   #12
Mondsreitersmann
 
Nuclear Krusader's Avatar
 
Join Date: Jul 1999
Location: Skingrad
Posts: 8,781
Nope. Not solved.

Strange, the link works for me in two different computers.
Nuclear Krusader is offline   Reply With Quote
Old 03-29-2004, 03:40 PM   #13
aym
Registered User
 
aym's Avatar
 
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]);
     }
In main, pass grade to compute_mark:
Code:
compute_mark(index, mark1, mark2, final, grade);
I've tried it, works fine
aym is offline   Reply With Quote
Old 03-29-2004, 08:43 PM   #14
Mondsreitersmann
 
Nuclear Krusader's Avatar
 
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.
Nuclear Krusader is offline   Reply With Quote
Old 03-30-2004, 05:59 AM   #15
aym
Registered User
 
aym's Avatar
 
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
}
Overload swap to take float and string params:
Code:
void swap(float& x, float& y) {
    // ...
}
void swap(string& x, string& y) {
    // ...
}
Try to implement this, if you have any problems just post

Good luck.
aym is offline   Reply With Quote
Old 03-30-2004, 12:17 PM   #16
Mondsreitersmann
 
Nuclear Krusader's Avatar
 
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)?
Nuclear Krusader is offline   Reply With Quote
Old 03-30-2004, 01:20 PM   #17
aym
Registered User
 
aym's Avatar
 
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.
aym is offline   Reply With Quote
Old 03-30-2004, 02:02 PM   #18
Mondsreitersmann
 
Nuclear Krusader's Avatar
 
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
Nuclear Krusader is offline   Reply With Quote
Old 03-30-2004, 04:15 PM   #19
aym
Registered User
 
aym's Avatar
 
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.
Attached Files
File Type: txt cis4c.txt (14.9 KB, 58 views)
aym is offline   Reply With Quote
Old 03-30-2004, 09:38 PM   #20
Mondsreitersmann
 
Nuclear Krusader's Avatar
 
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.
Nuclear Krusader is offline   Reply With Quote
Old 03-31-2004, 04:47 AM   #21
aym
Registered User
 
aym's Avatar
 
Join Date: Nov 2001
Posts: 1,965
no problem, glad to help
aym is offline   Reply With Quote
Old 04-08-2004, 02:04 AM   #22
Mondsreitersmann
 
Nuclear Krusader's Avatar
 
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.
Nuclear Krusader is offline   Reply With Quote
Old 04-08-2004, 02:09 AM   #23
CMT
Member (7 bit)
 
CMT's Avatar
 
Join Date: Feb 2003
Location: Pittsfield,ME
Posts: 123
Send a message via Yahoo to CMT
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
CMT is offline   Reply With Quote
Old 04-08-2004, 09:00 AM   #24
aym
Registered User
 
aym's Avatar
 
Join Date: Nov 2001
Posts: 1,965
Quote:
Originally posted by Nuclear Krusader
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.
You're welcome, hope you get a good mark
aym is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 12:50 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2