1CMIS 102 Week 5 Exercise 1 1.Code modification to calculate Average of 4 students with 6 exams. 1.C Code: #include <stdio.h> int main () { /* variable definition: */ char StudentName[100]; float ExamValue, Sum, Avg; int students,exams; // Loop through 4 Students for (students=0; students <4 ; students++) { // reset Sum to 0 Sum =0.0; printf("Enter Student Name \n"); scanf("%s", StudentName); // Nested Loop for Exams for (exams=0; exams < 6; exams++) {
2CMIS 102 printf ("Enter exam grade: \n"); scanf("%f", &ExamValue); Sum += ExamValue; } //End loop - exams //Calculate average Avg = Sum/6.0; //Display average printf( "Average for %s is %0.3f\n",StudentName,Avg); } //End loop - students return 0; }
3CMIS 102 2.Test Case for above mentioned modification: Test CaseInputExpected Output 1Studentname=Chris Examvalue1=80.0 Examvalue2=90.0 Examvalue3=100.0 Examvalue4=84.0 Examvalue5=70.0 Examvalue6=89.0 Studentname=John Examvalue1=70.0 Examvalue2=90.0 Examvalue3=80.0 Examvalue4=84.0 Examvalue5=70.0 Examvalue6=89.0 Studentname=Sally Examvalue1=100.0 Examvalue2=100.0 Examvalue3=100.0 Examvalue4=84.0 Examvalue5=70.0 Examvalue6=89.0 Studentname=Pat Examvalue1=50.0 Eexamvalue2=70.0 Examvalue3=60.0 Examvalue4=90.0 Examvalue5=95.0 Examvalue6=100.0 AverageforChrisis 85.500 AverageforJohnis 80.500 AverageforSallyis 90.500 Average for Pat is 77.500
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
4CMIS 102 Screenshot of the above mentioned exercise: Screenshot 1Screenshot 2
5CMIS 102 Screenshot: 3
6CMIS 102 3.In the code, if sum=0.0 is removed, then the program gives an unexpected result because the value of sum is not initialised before and after for loop. If sum= 0.0 is removed from the code, then it contains junk values and produces unexpected output. Output Screenshot is attached below for justifying the above statement.
Secure Best Marks with AI Grader
Need help grading? Try our AI Grader for instant feedback on your assignments.
7CMIS 102 If the sum variable is initialised during variable declaration then whenever sum variable is used for adding exam grade then it add the new exam grade with existing student grade and generate a wrong output. An output screenshot is attached below for justifying the above statement.
8CMIS 102 4.Modification of the original code to be able to input any number of exams and any number of students. C code: #include <stdio.h> int main () { /* variable definition: */ char StudentName[100]; float ExamValue, Sum, Avg; int students,exams; int Totalstudent,totalExam; //variable for storing numbers of student and exam //Asking User for numbersof Students and exams printf("Enter numbers of Students: \n"); scanf("%d", &Totalstudent); printf("Enter total numbers of exams: \n"); scanf("%d", &totalExam); // Loop through 5 Students
9CMIS 102 for (students=0; students <Totalstudent ; students++) { // reset Sum to 0 Sum =0.0; printf("Enter Student Name \n"); scanf("%s", StudentName); // Nested Loop for Exams for (exams=0; exams < totalExam; exams++) { printf ("Enter exam grade: \n"); scanf("%f", &ExamValue); //If Condition for exam value validation if(ExamValue>0 && ExamValue<=110){ Sum += ExamValue; } else { printf ("Enter a valid exam grade\n"); exams--;
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
10CMIS 102 } } //End loop - exams //Calculate average Avg = Sum/totalExam; //Display average printf( "Average for %s is %0.3f\n",StudentName,Avg); } //End loop - students return 0; }
11CMIS 102 Test Case table Test CaseInputExpected Output 1Number of student: 2 Numbers of Exam: 2 Studentname=Smith Examvalue1=80.0 Examvalue2=90.0 Studentname=Chris Examvalue1=70.0 Examvalue2=85.0 Average for Smith is 85.00 Average for Chris is 77.50 2Number of student: 3 Numbers of Exam: 3 Studentname=John Examvalue1=75.0 Examvalue2=94.0 Examvalue3=88.0 Studentname=Peter Examvalue1=68.0 Examvalue2=57.0 Examvalue3=74.0 Studentname=Lilly Examvalue1=87.0 Examvalue2=98.0 Examvalue3=83.0 Average for John is 85.66 Average for Peter is 66.33 Average for Lilly is 89.33 3Number of student: 2 Numbers of Exam: 2 Studentname=Chris Examvalue1=78.0 Examvalue2=91.0 Studentname=John Examvalue1=75.0 Examvalue2=87.0 Average for Chris is 84.50 Average for John is 81.00