### for loop

the program is to calculate the average time each student spent over a long weekend . I am trying to make the inner loop iterate n times where n is a positive integer that is input by the user

//This program finds the average time spent programming by a student each day over a 3 day period.
#include <iostream>
using namespace std;
 ``12345678910111213141516171819202122232425262728293031323334`` ``````int main() { int numStudents , student; float numHours, total , average; int day ,n; // these are the counters for the loops cout << " This program will find the average number of hours a day " << "that a student spent programming over a long weekend\n\n" ; cout << "How many students are there ? " << endl << endl; cin >> numStudents; cout << "Enter the number of days in the long weekend" << endl; cin >> n; for ( student = 1 ; student <= numStudents ; student++) { total = 0 ; for (day = 1 ; day <= n; day++) { cout << "Please enter the number of hours worked by student " << student << " on day " << day << "." << endl; cin >> numHours; total = total + numHours; } average = total / 3; cout << endl; cout << "The average number of hours per day spent programming " << " by student " << student << " is " << average << endl << endl << endl; } return 0; }``````

its giving wrong output , tried initializing n = 0 ; but that was even worse , please tell me what i'm missing ,
Last edited on
1) Please use code tags when posting code, to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/

2) "wrong output" isn't very helpful. Telling us what the output is, and how it differs from what you expect, is much more helpful. Help us to help you.
ok will at the link so that i will know how to use the code tag.

if i input 2 students , 2 days of the weekend , hours worked on day 1 = 4 and day2 = 6 average by student 1 must be 5 but its giving me 3.3333
i thought maybe i had to initialise n to 0 but its making the output worse
in the sense that where its supposed to ask the user to enter hours worked on day1 and day2 it will now just say day0 .

Last edited on
You ask for the number of days in the long weekend, and then divide by 3 to get your average.
ok here is a sample run

How many students are there
2
Enter the number of days in the long weekend
2
Please enter the number of hours worked by student1 on day1
4
please enter the number of hours worked by student1 on day2
6
The average number of hours spent programming by student 1 is 3.3333

but the answer should be 5 so how can i fix my code so as to obtain the correct answer
Fix line 25. It should be `average = total / n;`
thank you , was just staring at the answer but hey
Topic archived. No new replies allowed.