A couple of quick questions.
- Why does this code not return a decimal answer
int const limit = 5; int sum; double average; int marks[]={7,5,8,1,3}; double Avg(int marks[]) { for (int i = 0; i < limit; i++) { sum += marks[i]; } average = sum / limit; return average; } - Why does changing sum to a double matter?
int const limit = 5; double average, sum; int marks[]={7,5,8,1,3}; double Avg(int marks[]) { for (int i = 0; i < limit; i++) { sum += marks[i]; } average = sum / limit; return average; - Here is my final solution making the question irrelevant, but I would still like to understand the logic.
int const limit = 5; double average; int marks[limit]; double Avg(int marks[]) { for (int i = 0; i < limit; i++) { average += marks[i]; } average /= limit; return average; }
Any suggested reading on C++ math would be greatly appreciated. I just noticed that
cout << 15/4;returns 3 and not 3.75. That was unexpected too!!!
Also I am looking for documentation on code format etiquette and accepted C++ syntax. It is very hard to tell what is current when trying to learn from the web.
Thanks

New Topic/Question
Reply




MultiQuote










|