943,967 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 7670
  • C++ RSS
Oct 13th, 2007
0

Help with counter-controlled loop to find average

Expand Post »
Hey fellow programmers.

I am just starting to work on C++ and languages in computer programming. So far its not bad yet, but having an issue with this program I am trying to write. The program I am writing is to compute the sum of the minutes exercised per week, as well as the average. The user is asked to input the minutes of exercise per day, and from there am required to use a counter-controlled while loop to figure out the sum and average. I am having issues with my code. I have it so that it displays the sum and average minutes, but it also needs to kick out the number of days exercised and if a 0 is entered, it still gives me seven for days exercised. I am looking for help and ideas how to get this to work right. Thanks for help. My code is below.

C++ Syntax (Toggle Plain Text)
  1. //Program to calculate the average number of minutes
  2. //excersized per week by an individual.
  3.  
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8.  
  9. int main()
  10.  
  11. {
  12.  
  13. int counter;
  14. int number;
  15. int sum;
  16. int limit;
  17.  
  18. limit = 7;
  19.  
  20.  
  21.  
  22.  
  23.  
  24. sum = 0;
  25. counter = 0;
  26.  
  27.  
  28.  
  29.  
  30. while (counter < limit)
  31. {
  32. cout << "Enter number of minutes excercised per day. Press enter after each entry." << endl;
  33. cout << "The first day of the week is Monday." << endl;
  34.  
  35. cin >> number;
  36.  
  37. sum = sum + number;
  38. counter = counter + 1;
  39. }
  40.  
  41. cout << "The total minutes excersized is " << sum << endl;
  42.  
  43. if (counter != 0)
  44. cout << "The average minutes excersized per day during this week is "
  45. << sum / counter << endl;
  46.  
  47. else
  48. cout << "Zero minutes entered for the week." << endl;
  49.  
  50.  
  51. if (counter = limit)
  52. cout << "The number of days excercised is " << counter << endl;
  53.  
  54. return 0;
  55. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
Koldsoul is offline Offline
39 posts
since Oct 2007
Oct 13th, 2007
0

Re: Help with counter-controlled loop to find average

You're incrementing 'counter' each time you loop. You need to add a condition:
    while (counter < limit)
    {
        cout << "Enter number of minutes excercised per day.  Press enter after each entry." << endl;
        cout << "The first day of the week is Monday." << endl;
        
        cin >> number;
        
        sum = sum + number;
        if (number == 0) {
            counter = counter + 1;
        }
    }

And get rid of this:
if (counter = limit)
	cout << "The number of days excercised is " << counter << endl;
(Not to mention that you used the assignment operator, not the comparison operator (==), another reason why it was outputting the wrong number.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Oct 14th, 2007
0

Re: Help with counter-controlled loop to find average

As to the final bit always stating 7 days exercised, it's because you've made one of those typo errors we all tend to make from time to time.
C++ Syntax (Toggle Plain Text)
  1. if (counter = limit)
  2. cout << "The number of days excercised is " << counter << endl;
You are assigning to counter the value stored in limit, and then evaluating the result of that assignment. It's always going to be 7. You really meant to type if( counter == limit) didn't you?

You seem to have a couple logical problems as well. Your loop is limited by counter being less than limit, and counter gets incremented every iteration. You are forcing the user to enter 7 values. So counter will always be 7 (or whatever you set limit to) after the loop.

Counter has no relationship to how many minutes the user may enter - thus this line really doesn't tell a correct story:
C++ Syntax (Toggle Plain Text)
  1. else
  2. cout << "Zero minutes entered for the week." << endl;
To know that, you need to examine the sum variable.

Val
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Oct 14th, 2007
0

Re: Help with counter-controlled loop to find average

in addition, this expression sum / counter will do integer division; ie. if sum ==23 and counter==7, the result would be 3, not 3.28. you could either make sum a double or write double(sum) / counter to get the fractional part.

it is also a good idea to get into the habit of initializing a variable at the point of definition; this will protect you from silly errors (using uninitialized variables) and later, when you deal with more complex types, help you write more efficient code. ie. not
C++ Syntax (Toggle Plain Text)
  1. int counter;
  2. int sum;
  3. int limit;
  4. limit = 7;
  5. sum = 0;
  6. counter = 0;
but
C++ Syntax (Toggle Plain Text)
  1. int counter = 0 ;
  2. int sum = 0 ;
  3. int limit = 7 ;

also, since limit is a constant, declare it as such eg const int limit = 7 ;
or enum { limit = 7 } ;

how you write code later is going to be determined to a large extent by the habits you form in the early days; forming good habits now (some people call this programming hygiene) would serve you well in the future,
Last edited by vijayan121; Oct 14th, 2007 at 12:45 am.
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Oct 14th, 2007
0

Re: Help with counter-controlled loop to find average

I have tried those suggestions, cleaned up the program, and am now trying to get it to run without asking for each number to be input as the instructor in my class requested. The user should be asked for the minutes, leaving a space between each number and now I cannot get any of it to run right. This is what I currently have. I put in the conditions as joeprogrammer suggested and tried various things to get this to work. I feel like a moron because it is probably something so simple that I am just not getting it to sink in. Anyways, here is my new code for this program. I tried to clean it up a bit so its not so cluttered looking. Thanks in advance for advice on this.


C++ Syntax (Toggle Plain Text)
  1. //Program to calculate the average number of minutes
  2. //excersized per week by an individual.
  3.  
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8.  
  9. int main()
  10.  
  11. {
  12.  
  13. int counter;
  14. int number;
  15. int sum;
  16. int limit;
  17.  
  18. limit = 7;
  19.  
  20.  
  21. cout << "Enter number of minutes excercised per day." << endl;
  22. cout << "Leave a space between each number of minutes." << endl;
  23. cout << "Do not enter a value for zero minutes." << endl;
  24. cout << "The first day of the week is Monday." << endl; \
  25. cin >> number;
  26.  
  27. sum = 0;
  28. counter = 0;
  29.  
  30.  
  31. while (counter <= 7)
  32. {
  33. if (number > 0)
  34. {
  35. sum = sum + number;
  36. counter = counter + 1;
  37. }
  38. if (number = 0)
  39. counter = counter;
  40. }
  41.  
  42. cout << "The total minutes excersized is " << sum << endl;
  43.  
  44. if (counter != 0)
  45. {
  46. cout << "The average minutes excersized per day during this week is "
  47. << sum / counter << endl;
  48. }
  49. cout << "The number of days excercised is " << counter << endl;
  50.  
  51. return 0;
  52. }
Reputation Points: 10
Solved Threads: 0
Light Poster
Koldsoul is offline Offline
39 posts
since Oct 2007
Oct 14th, 2007
0

Re: Help with counter-controlled loop to find average

you need to accept seven integers from the user; so you need to move
cin >> number; to inside the while loop that executes seven times.

counter is initially zero; so to execute the loop seven times, you should write
while (counter < 7) // not <=
Last edited by vijayan121; Oct 14th, 2007 at 12:48 am.
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Oct 14th, 2007
0

Re: Help with counter-controlled loop to find average

I moved cin >> number; inside the loop and now it never stops and runs the rest of the program. I need to only ask for input once, the user leaves spaces between the days, and then calculates it all. With this inside the loop, when I run the program, enter number of minutes, it just goes to the next line on the screen and doesn't seem to run the loop or the rest of the program.
Reputation Points: 10
Solved Threads: 0
Light Poster
Koldsoul is offline Offline
39 posts
since Oct 2007
Oct 14th, 2007
0

Re: Help with counter-controlled loop to find average

c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. int counter = 0 ;
  7. int sum = 0 ;
  8. const int limit = 7 ;
  9.  
  10. int num_days = 0 ;
  11. while( counter < limit )
  12. {
  13. int number ;
  14. cin >> number ;
  15. if (number > 0)
  16. {
  17. sum = sum + number;
  18. num_days = num_days + 1;
  19. }
  20. counter = counter + 1 ;
  21. }
  22.  
  23. cout << "The total minutes excersized is " << sum << endl;
  24. if( num_days > 0 )
  25. {
  26. cout << "The average minutes excersized per day during this week is "
  27. << double(sum) / counter << endl;
  28. cout << "The number of days excercised is " << num_days << endl;
  29. }
  30. }
  31. /**
  32. >c++ exersize.cc && ./a.out
  33. 1 0 2 3 4 0 5
  34. The total minutes excersized is 15
  35. The average minutes excersized per day during this week is 2.14286
  36. The number of days excercised is 5
  37. */
Last edited by vijayan121; Oct 14th, 2007 at 1:05 am.
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Oct 14th, 2007
0

Re: Help with counter-controlled loop to find average

Oh, duh, like I said, I am a moron. I see exactly what you were saying now and understand what it is all doing. Thank you so much.
Reputation Points: 10
Solved Threads: 0
Light Poster
Koldsoul is offline Offline
39 posts
since Oct 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Recursively Reversing a String
Next Thread in C++ Forum Timeline: Creating Variables on the fly





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC