CMIS 102 Assignment: C Code Modification for Student Exam Averages

Verified

Added on  2022/07/28

|13
|771
|61
Homework Assignment
AI Summary
This assignment presents a C code solution for a CMIS 102 homework assignment focused on calculating student exam averages. The original code calculates the average of 6 exams for 4 students. The solution includes a detailed analysis, explaining the code's structure, including the use of loops and variable initialization. It also provides test cases with expected outputs to validate the code's functionality. The assignment further explores code modifications to handle a variable number of students and exams, incorporating user input for flexibility and input validation. The document includes code snippets, screenshots of outputs, and explanations of the modifications made to enhance the program's adaptability and robustness. The provided test cases cover various scenarios to ensure the code's accuracy and reliability. This solution is designed to help students understand C programming concepts, particularly loops, user input, and output, while addressing practical programming challenges.
tabler-icon-diamond-filled.svg

Contribute Materials

Your contribution can guide someone’s learning journey. Share your documents today.
Document Page
Running Head: CMIS 102
CMIS 102
Name of the Student
Name of the University
tabler-icon-diamond-filled.svg

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
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++)
{
Document Page
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;
}
Document Page
3CMIS 102
2. Test Case for above mentioned modification:
Test Case Input Expected Output
1 Studentname=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
Average for Chris is
85.500
Average for John is
80.500
Average for Sally is
90.500
Average for Pat is 77.500
tabler-icon-diamond-filled.svg

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
4CMIS 102
Screenshot of the above mentioned exercise:
Screenshot 1 Screenshot 2
Document Page
5CMIS 102
Screenshot: 3
Document Page
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.
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
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.
Document Page
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
Document Page
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--;
tabler-icon-diamond-filled.svg

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
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;
}
Document Page
11CMIS 102
Test Case table
Test Case Input Expected Output
1 Number 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
2 Number 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
3 Number 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
Document Page
12CMIS 102
Output:
chevron_up_icon
1 out of 13
circle_padding
hide_on_mobile
zoom_out_icon
logo.png

Your All-in-One AI-Powered Toolkit for Academic Success.

Available 24*7 on WhatsApp / Email

[object Object]