CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Some Funky Math

  1. #1
    Join Date
    Feb 2009
    Posts
    135

    Some Funky Math

    Grader v. 1.01b.exe

    Grader v. 1.01 BETA Source.zip

    So, I have an odd dilemma that I'm not entirely certain I can pin point through debugging. To me, it seems more like an issue of not understanding how the system interprets a certain math equation than anything else.

    To preface, my program is a basic grader program that I am building as a part of falling the Accelerated C++ book for learning C++. If you look at the code, you'll likely find inefficient uses of things, etc... however, the extent to what I know is what will be found in there, and suggesting that I replace "such and such" with a class will most likely confuse me at this point. Beyond that, if you know Accelerated C++ and know what I should know after reading Chapter 4, feel free to comment on the code and help as you will.

    However, my issue lies in the calculation of final grades. At the moment, I have not incorporated a NULL state to my variables (I'm likely going to use the -1 method until I learn classes). Since I haven't, whenever you create a new student, it sets all of their basic attributes to 0, which you can then edit. However, whenever all of their attributes are 0, if I go to list to see their final grade, I do not see 0... I get odd numbers like "-1,#QHN", what the heck is that garbage?

    The equation for what my program is doing to find the final grade is here:
    Code:
    double Do_Calc_Homework(Student_Record& student)
    {
    	sz_t Max_Homework = student.homework.size();
    	double Total_Homework = 0;
    
    	for (sz_t Curr_Homework = 0; Curr_Homework != Max_Homework; 
    		++Curr_Homework)
    	{
    		Total_Homework += student.homework[Curr_Homework];
    	}
    
    	return Total_Homework / Max_Homework;
    }
    
    double Do_Calc_Quiz(Student_Record& student)
    {
    	sz_t Max_Quizzes = student.quizzes.size();
    	double Total_Quizzes = 0;
    
    	for (sz_t Curr_Quizzes = 0; Curr_Quizzes != Max_Quizzes; ++Curr_Quizzes)
    	{
    		Total_Quizzes += student.quizzes[Curr_Quizzes];
    	}
    
    	return Total_Quizzes / Max_Quizzes;
    }
    
    double Do_Calc_Test(Student_Record& student)
    {
    	sz_t Max_Tests = student.tests.size();
    	double Total_Tests = 0;
    
    	for (sz_t Curr_Tests = 0; Curr_Tests != Max_Tests; ++Curr_Tests)
    	{
    		Total_Tests += student.tests[Curr_Tests];
    	}
    
    	return Total_Tests / Max_Tests;
    }
    
    double Do_Calc_Project(Student_Record& student)
    {
    	sz_t Max_Projects = student.projects.size();
    	double Total_Projects = 0;
    
    	for (sz_t Curr_Projects = 0; Curr_Projects != Max_Projects;
    		++Curr_Projects)
    	{
    		Total_Projects += student.projects[Curr_Projects];
    	}
    
    	return Total_Projects / Max_Projects;
    }
    
    double Do_Calc_Final(Student_Record& student, Weight& weight)
    {
    	return (weight.midterm * student.midterm)
    		+ (weight.final * student.final)
    		+ (weight.homework * student.homework_avg)
    		+ (weight.tests * student.test_avg)
    		+ (weight.quizzes * student.quiz_avg)
    		+ (weight.projects * student.project_avg);
    }
    
    double Get_Average(const vector<double>& Number_List)
    {
    	double sum = 0;
    	for (sz_t count = 0; count != Number_List.size(); ++count)
    	{
    		sum += Number_List[count];
    	}
    
    	double sum_size = Number_List.size();
    
    	return sum / sum_size;
    }
    Any of the numbers divided by 0 should be 0. When the weights are multiple by 0, they should remains 0, the final grade should be 0, but it is not. Why? I'm pretty confused.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Some Funky Math

    Quote Originally Posted by BleaS
    Any of the numbers divided by 0 should be 0.
    But you should not actually try to divide by zero. Rather, you should check if a division by zero will occur, and if so, return 0 (or throw an exception in other circumstances).
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Some Funky Math

    Any of the numbers divided by 0 should be 0.
    Any division by zero results in not-a-number (QNAN for double). So don't do that.

    Second, you need an if statement in each of those loops ensuring you're finding the average of the positive values only----and that includes counting how many positive values there are rather than relying on .size()!

    Of course, since you're using containers to store the various grades anyway, one wonders why you need a "no grade here" marker at all. Why not just expand the container only when you get valid grades?

  4. #4
    Join Date
    Feb 2009
    Posts
    135

    Re: Some Funky Math

    Quote Originally Posted by Lindley View Post
    Any division by zero results in not-a-number (QNAN for double). So don't do that.

    Second, you need an if statement in each of those loops ensuring you're finding the average of the positive values only----and that includes counting how many positive values there are rather than relying on .size()!

    Of course, since you're using containers to store the various grades anyway, one wonders why you need a "no grade here" marker at all. Why not just expand the container only when you get valid grades?
    It's impossible for a negative number to be entered into the value due to some other checks in the program, thus it's a bit easier since we don't have to check for negative numbers. Download the program and break it, I'm sure you can and it will teach me about what else I need to safeguard for.

    I initialized values because of how it works when you add a student. Since the only way I found I could do push_back was to copy the entire container over, I needed to make a container in the function, and then push it back to the actual student_info container. I was under the assumption that in copying a structure container that I could not copy over uninitialized values, or, at the very least, that it would be bad form.

    Thank you for the advice, I will relook at the calculating functions.

  5. #5
    Join Date
    May 2007
    Posts
    811

    Re: Some Funky Math

    Quote Originally Posted by BleaS View Post
    Any of the numbers divided by 0 should be 0. When the weights are multiple by 0, they should remains 0, the final grade should be 0, but it is not. Why? I'm pretty confused.
    I'm confused how could you even think like this. Dividing by 0 in math is invalid, so how could that be valid for computers?

  6. #6
    Join Date
    Feb 2009
    Posts
    135

    Re: Some Funky Math

    Quote Originally Posted by STLDude View Post
    I'm confused how could you even think like this. Dividing by 0 in math is invalid, so how could that be valid for computers?
    Because I was wrong.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured