In: Computer Science
OBJECTIVE-C
For this program a teacher needs to be able to calculate an average of test scores for students in their course. Your program must ask the Professor ho many students and how many tests will be averaged per student. Your program should then allow the Professor to enter scores for each student one at a time and then average those scores together. For example, if a student has 5 scores to be entered and the scores are 80, 60, 65,98, and 78 the average should be 76.2%
Here is what I got.
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
int students, numOfScores, scores, sum;
float average;
NSLog(@"Enter number of students");
scanf("%i", &students);
NSLog(@"Enterh number of scores");
scanf("%i", &numOfScores);
int n;
int s;
for (n = 1; n <= students; n++){
for (s = 1; s <= numOfScores; s++)
{
NSLog(@"Enter the score");
scanf("%i", &scores);
sum = sum + scores;
}
}
average = (float) sum / numOfScores;
NSLog(@"An average score is %f", &average);
return 0;
}
Please find your solution below and if any doubt or need change comment and do upvote.
CODE:
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
int students, numOfScores, scores, sum;
float average;
//take user input
NSLog(@"Enter number of students");
scanf("%d", &students);
NSLog(@"Enterh number of scores");
scanf("%d", &numOfScores);
int n;
int s;
//using loop sum the enter values
for (n = 1; n <= students; n++)
{
sum=0;//reset to 0 for each next student
for (s = 1; s <= numOfScores; s++)
{
//add thte scores
NSLog(@"Enter the score");
scanf("%d", &scores);
sum = sum + scores;
}
//find the average and print it
average=(float)sum/numOfScores;
NSLog(@"An average score is %.1f", average);
}
return 0;
}
OUTPUT: