The C code below is for Exam 2 Program 1 . It now runs perfectly. Help me with E
ID: 3713359 • Letter: T
Question
The C code below is for Exam 2 Program 1. It now runs perfectly. Help me with Exam 3 Program 2 and Exam 4 Program 1 from the picture above
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main()
{
int i;
char name[15];
char grade;
int homework1, homework2, homework3, exam1, exam1total, exam2, exam2total, totalhomework, totalexam, a, b, c, d, f;
float percentage;
exam1total = 0;
exam2total = 0;
a = b = c = d = f = 0;
for(i = 0; i < 5; i++)
{
printf("enter a name ");
gets_s(name, 15); //no placeholder
printf("enter homework grades and exam scores ");
scanf_s("%d%d%d%d%d", &homework1, &homework2, &homework3, &exam1, &exam2);
exam1total += exam1; //examtotal = exam1total + exam1;
exam2total += exam2;
totalexam = exam1 + exam2;
totalhomework = homework1 + homework2 + homework3;
percentage = totalhomework / (float) 1.875 + totalexam / (float) 3.333;
if (percentage < 0 || percentage > 100)
{
printf("invalid");
}
else if (percentage >= 90)
{
grade = 'A';
a++;
}
else if (percentage >= 80)
{
grade = 'B';
b++;
}
else if (percentage >= 70)
{
grade = 'C';
c++;
}
else if (percentage >= 60)
{
grade = 'D';
d++;
}
else
{
grade = 'F';
f++;
}
printf(" %s Total Homework Points : %d Total Exam Points : %d Class Percentage: %0.2f Grade : %c ", //string - collection of chars.
name, totalhomework, totalexam, percentage, grade); // \\ %d = int/unsigned int \\ %c char
getchar(); //?????
}
printf(" average exam 1 score : %0.2f", exam1total / (float)5); //5 average scores a,b,c,d,f? or 5 other "things" **********
printf(" average exam 2 score : %0.2f", exam2total / (float)5);
printf(" Number of A's : %d Number of B's : %d Number of C's : %d Number of D's : %d Number of F's : %d ", a, b, c, d, f);
system("PAUSE");
}
Explanation / Answer
The below C code uses two functions getMarks() and showResult().It takes as input the parameters that are required to be manipulted ahead like name,exam1total,exam2total,totalexam,totalhomework and percentage.The getMarks function is used to read the marks from the user and then calcultes the total for the exams and homeworks.It then passes these values again to the showResult function and it calculates the percentage and displays the result accordingly.
I am using pointers to pass data from main to these functions as the changes made into the values of these variables has to be persistent.
CODE:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void getMarks(char *name,int *e1total,int *e2total,int *totalexam,int *totalhomework,float *percentage)
{
int homework1, homework2, homework3, exam1, exam2;
printf("Enter a name ");
gets(name);
printf("Enter homework grades and exam scores ");
scanf("%d%d%d%d%d", &homework1, &homework2, &homework3, &exam1, &exam2);
*e1total += exam1;
*e2total += exam2;
*totalexam = exam1 + exam2;
*totalhomework = homework1 + homework2 + homework3;
}
void showResult(char *name,int *e1total,int *e2total,int *totalexam,int *totalhomework,float *percentage)
{
char grade;
*percentage = *totalhomework / (float) 1.875 + *totalexam / (float) 3.333;
int a , b, c, d, f ;
a = b = c = d = f = 0;
if (*percentage < 0 || *percentage > 100)
{
printf("invalid");
}
else if (*percentage >= 90)
{
grade = 'A';
a++;
}
else if (*percentage >= 80)
{
grade = 'B';
b++;
}
else if (*percentage >= 70)
{
grade = 'C';
c++;
}
else if (*percentage >= 60)
{
grade = 'D';
d++;
}
else
{
grade = 'F';
f++;
}
printf(" %s Total Homework Points : %d Total Exam Points : %d Class Percentage: Grade : %c ",name, *totalhomework, *totalexam, *percentage, grade);
getchar();
printf(" average exam 1 score : %0.2f", *e1total / (float)5); //5 average scores a,b,c,d,f? or 5 other "things" **********
printf(" average exam 2 score : %0.2f", *e2total / (float)5);
printf(" Number of A's : %d Number of B's : %d Number of C's : %d Number of D's : %d Number of F's : %d ", a, b, c, d, f);
system("PAUSE");
}
void main()
{
int i;
char name[15];
char grade;
int homework1, homework2, homework3, exam1,
exam1total=0, exam2, exam2total=0, totalhomework=0, totalexam=0,
a, b, c, d, f;
float percentage=0;
exam1total = 0;
exam2total = 0;
a = b = c = d = f = 0;
getMarks(name,&exam1total, &exam2total,&totalexam,&totalhomework,&percentage);
showResult(name,&exam1total, &exam2total,&totalexam,&totalhomework,&percentage);
}
OUTPUT:
For the exam4 program1,that uses loops to function itself has already been written by you.It doesn't need any modifications as far as the requirements are concerned.It assumes that the user will input the marks for a fixed number of students and hence it runs 5 times.So,the above code is already fine.The for loop is an entry control loop as it checks the condition that is to be tested before entering the statements to be executing inside it.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.