Ok, so im working on a little program that I need to prompt the user to input integers, which are stored in an array. The data in the array is then passed into another function which adds all the array location together, and divides it by the number of numbers entered to give an average of the numbers input by the user.
Heres my code...
So the problem I'm having is the data is being stored properly in the function that gets the data from the user, but when I pass it to the average() function its not passing the array data correctly. What is causing this, and what do I need to do to fix it?
Thanks,
Val
Heres my code...
Code:
#include <stdio.h>
#define MAXSTUDENTS 25
int input(int numgrades[]);
float average(int numgrades[], int num_grades);
int main(void) {
float mean = 0.0;
int numgrades[MAXSTUDENTS];
int grades_entered = 0;
grades_entered = input(numgrades);
mean = average(numgrades, grades_entered);
}
//sub functions
int input(int numgrades[]) {
int i = 0;
printf("Enter the grades you would like curved.\nEnter -1 to finish.\n");
scanf("%d%*c", &numgrades[i]);
while (i < MAXSTUDENTS - 1 && numgrades[i] != -1) {
i++;
scanf("%d%*c", &numgrades[i]);
}
if (i == MAXSTUDENTS - 1) {
printf("Maximum number of grades allowed is 25.\nNo more grades allowed.");
return(i + 1);
} else {
return (i);
}
}
float average(int numgrades[], int num_grades) {
int i = 1;
int num = 0;
int sum = 0;
float average = 0.0;
for (i = 1; i <= num_grades; i++) {
numgrades[i] = num;
sum = sum + num;
}
average = sum / num_grades;
return (average);
}
So the problem I'm having is the data is being stored properly in the function that gets the data from the user, but when I pass it to the average() function its not passing the array data correctly. What is causing this, and what do I need to do to fix it?
Thanks,
Val