Eckirion Posted October 16, 2015 Share Posted October 16, 2015 Does anyone know how to program with C language? If yes, can you help me with this cause the struct element stude[].grade always returns with result 0. case 3: write( "\nThese are the student's grade." ); for ( d = 0; d < a; d++ ) { if ( stude[d].math_point == 0 ) { write( "\nStudent #%d : BLANK \n Grade: NOT AVAILABLE", g ); g++; } else { stude[d].grade = (( 0.3 * ( stude[d].math_point / stude[d].math_total )) + ( 0.3 * ( stude[d].sci_point / stude[d].sci_total )) + ( 0.2 * ( stude[d].eng_point / stude[d].eng_total )) + ( 0.2 * ( stude[d].att_point / stude[d].att_total ))) * 100; write( "\nStudent #%d : %s \n Grade: %.2lf", g, stude[d].name, stude[d].grade ); g++; } } g = 1; break; Thanks in advance! Quote Link to comment Share on other sites More sharing options...
Kriemhild Posted October 16, 2015 Share Posted October 16, 2015 C++tan~ <3 General advice when asking for help about coding problems: try to give a brief description of what you want your code snippet to do, rather than force the reader to decipher everything. Anyway, I'm not really sure what values you're testing with, but since you pointed out that stude[d].grade is always 0 (which I assume is NOT supposed to be the case), I'm going to guess that the problem is because the math_point, math_total, sci_point, etc are all defined as integers. When you divide an integer by an integer, the result becomes an integer. And since math_point is likely to be less than math_total (and so on), the result of each of those divisions yields 0. If this is the problem, my suggestion would be to generate a float/double in either the numerator or the denominator of each division. You can do this by casting an int into a float/double or performing an operation involving a float/double first e.g. (0.3 * integer / integer). I developed a habit of multiplying by 1.0 whenever I feel like generating floats/doubles so you can try that too I don't really see any other problems in your code, but then again, I'm not entirely sure exactly whether what I think the program is doing is what you want it to be doing. I should probably point out a minor nitpick though, in that you're multiplying the grades with weights less than 1 (0.3 and 0.2) and then multiplying everything by 100. It would be simpler to just use weights normalized under 100 (i.e. 30 and 20) and drop the multiplication by 100. Hope that helps! Quote Link to comment Share on other sites More sharing options...
Eckirion Posted October 17, 2015 Author Share Posted October 17, 2015 (edited) It's not actaully C++ since this uses printf which I defined as write. I see, so I need to change the data type of the x_point and x_total ( x are the subjects ) to double or float. Ahh... I did change stude[].grade to double, guess I will have to define those as float. Thank you! Will try to see if it works. EDIT NOTE: It really did work... Thank you very much, you have been a big help. Edited October 17, 2015 by Tecki Quote Link to comment Share on other sites More sharing options...
Kriemhild Posted October 17, 2015 Share Posted October 17, 2015 It's not actaully C++ since this uses printf which I defined as write.Yeah, I noticed. But everything in C is part of C++tan too. Even printf, but we just prefer using cout instead. I see, so I need to change the data type of the x_point and x_total ( x are the subjects ) to double or float. Ahh... I did change stude[].grade to double, guess I will have to define those as float. Thank you! Will try to see if it works.This would, indeed, work... but I won't really recommend changing them to double/float if they are supposed to represent integers only. I would still insist on either casting to float/double or multiplying with a float/double before dividing. But yeah, as long as you took care of this issue, it's fine. EDIT NOTE: It really did work... Thank you very much, you have been a big help.You're welcome! Don't hesitate if you need more help with coding later~ Quote Link to comment Share on other sites More sharing options...
Eckirion Posted October 17, 2015 Author Share Posted October 17, 2015 Yeah, I noticed. But everything in C is part of C++tan too. Even printf, but we just prefer using cout instead. Yeah, that is because C is the mother of C++...tan. You're welcome! Don't hesitate if you need more help with coding later~ Will do! ^_^ 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.