So I started learning arrays in C. I made a simple program.
Code:
#include <stdio.h>
int grades[9];
int main(void)
{
int i = 0;
int amount_subjects;
int total;
int ans;
printf("How many subjects do you have? ");
scanf("%d", &amount_subjects);
while (i != amount_subjects)
{
printf("Type in your grade: ");
scanf("%d", &grades[i]);
i++;
}
total = grades[0] + grades[1] + grades[2] + grades[3] + grades[4] + grades[5] + grades[6] + grades[7] + grades[8];
printf("\nADD = %d", total);
ans = total / amount_subjects;
printf("\n%d", ans);
}
I know it is not the best. I know I could of used a for statement. I know I should comment in my code.
So it is a small program that calculates the average of your grades.
Total amount of grades Divided by Amount of classes/Subjects
Simple.
See the program works fine with small numbers like 6, 11, 7
It adds them to 24 Then it divides by 3 and the answer is 8. It is correct
But for bigger numbers like 60,78,90,65,68,70,70,80,70,77 it does not add them correctly.
Program adds them to 651 That is wrong. it should be 728.
Did I make a mistake? What is it?
Oh I used Dev-C++ 5.5.3 to compile it.
Compiler is: TDM-GCC 4.7.1 64-Bit Release