Inaccurate and wrong output on C# training p2

Hamidreza Jorjani 0 Reputation points
2024-04-19T18:17:23.79+00:00

I am experiencing issues with the C# training p2 module, found at https://learn.microsoft.com/en-us/training/modules/guided-project-arrays-iteration-selection/5-exercise-implement-selection-statements. In this exercise, the variable sumAssignemtScores is set to int type, which causes inaccurate and wrong output when calculating scores that have a 10% influence on the final score. I think it should be type of decimal. Can someone help? This question is related to the following Learning Module

Azure Training
Azure Training
Azure: A cloud computing platform and infrastructure for building, deploying and managing applications and services through a worldwide network of Microsoft-managed datacenters.Training: Instruction to develop new skills.
926 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Pradeep M 400 Reputation points Microsoft Vendor
    2024-04-20T05:24:14.74+00:00

    Hi Hamidreza Jorjani,

    Thank you for reaching out to Microsoft Q & A forum.   

    Thank you for raising this concern. You're absolutely correct in noting that using the int data type for the variable sumAssignmentScores can lead to inaccuracies, especially when calculating scores that have a 10% influence on the final grade. This is because integer division truncates any decimal part, resulting in a loss of precision.

    While the current implementation may seem to produce correct output for the provided data, it's essential to consider its limitations, particularly regarding the calculation of extra credit scores. In scenarios where extra credit scores significantly impact the final grade, relying on integer division could indeed result in inaccurate grading.

    However, it's important to note that changing the data type of sumAssignmentScores to double might not be necessary in all cases. Using double introduces floating-point arithmetic, which could potentially introduce rounding errors or other issues.

    Instead, we can address this concern by ensuring that the calculation involving sumAssignmentScores maintains precision. This can be achieved by explicitly casting the integer division operation to a floating-point type before performing the calculation. For example:

    currentStudentGrade = (double)sumAssignmentScores / examAssignments;
    
    

    By making this adjustment, we preserve the accuracy of the calculation without necessarily changing the data type of sumAssignmentScores. This approach maintains the integrity of the existing code while addressing the issue of precision loss in the calculation of student grades.

    If you encounter any future issues, please feel free to contact us, and we will be pleased to assist you further.  

    If you have found the answer provided to be helpful, please click on the "Accept answer/Upvote" button so that it is useful for other members in the Microsoft Q&A community. 

    Thank you.