Reputation: 45
So I have this array, I need to find the Min, Max, and Average. I have figured out how to get the Max, my code looks right for the minimum but it does not work so I must be missing something. I also need to find the average of the numbers entered. Everything I can find on this only works when you have the size of the array set. I have a max size set for the array but it does not get filled by user input. I have been staring and testing on this for days and still cant figure it out.
I know for the average I need something like (with added declartions at the top), but I cannot figure out how to get it to work:
for (i = 0; i < numOfGrades; i++) {
sum += grades[i];
}
avg = (double) sum / numOfGrades;
Here is the rest of my code:
#include <stdio.h>
int main(){
int grades [100];
int i = 0;
int small, big, input;
printf("Enter a series of grades. When done, enter any number above 100.\n\n");
while (i <= 100) { //while loop to set maximum for array
printf("Enter grade:");
if (scanf("%d", &input) == 1) {
if (input >= 0 && input <=100) {
grades[i] = input; //if good, add to array
i++;
}
else {
printf("\n\nExiting entry.\n");
printf("\n\nGrades entered:\n\n");
i = i - 1;
break; //exiting loop
}
}
}
int x, y;
for (x = 0; x <= i; x++) {
printf("Grade: %d\n", grades[x]); //print array
}
big = small = grades[0];
for (y = 0; y < i; y++) {
if (grades[y] > big) {
big = grades[y];
}
else if (grades[y] < small) {
small = grades[y];
}
}
printf("Highest number : %d\n", big);
printf("Smallest number: %d\n", small);
return 0;
}
Upvotes: 0
Views: 99
Reputation: 559
Actually you do have the number of grades
if (scanf("%d", &input) == 1) {
if (input >= 0 && input <=100) {
grades[i] = input; //if good, add to array
i++;
}
else {
numOfGrades=i;
printf("\n\nExiting entry.\n");
printf("\n\nGrades entered:\n\n");
i = i - 1;
break; //exiting loop
}
As for the minimum, your logic should be like this:
for (y = 0; y < numOfEdges; y++) {
if (grades[y] > big) {
big = grades[y];
}
if (grades[y] < small) {
small = grades[y];
}
}
The else statement I removed should do the trick. Also I would always use
for(int var = 0; var < numOfEdges; var++)
in any of the loop constructs. It's easyer to follow the logic this way: You have counted the number of edges (numOfEdges), but your loop goes only to numOfEdges-1 since you start with index 0.
Upvotes: 1
Reputation: 33934
Here you do the right thing:
for (x = 0; x <= i; x++) {
Because x <= i
will take you from 0 to the value of i
. Then in your min/max
loop you do this:
for (y = 0; y < i; y++) {
So you are going from 0 to to i-1
. You have 2 options:
y <= i
.i
in the while
loop (Dont do this i = i - 1;
) That way, i
represents the number of numbers entered, instead of number of numbers entered -1 (you will need to fix the x <= i
to x < i
).Here is a live example. http://ideone.com/ZEaDue
To calculate the average, you are doing the right thing, you can re-use i
though:
int sum = 0, avg = 0;
for (y = 0; y < i; y++) {
sum += grades[y];
}
avg = (double) sum / i;
printf("Avg: %d\n", avg);
Upvotes: 1