|
-
February 6th, 2011, 12:32 PM
#1
Output from a for loop is incorrect due to wrong calculations.
Hey guys,
I finished the looping chapter today and have been able to solve most exercises. Now there is one exercise I need some help with.
 Originally Posted by Exercise
Write a program that calculates how much a person would earn over a period of time if his or her salary is one penny the first day and two pennies the second day, and continues to double each day. The program should ask the user for the number of days.
Display a table showing how much the salary was for each day, and then show the total pay at the end of the period. The output should be displayed in a dollar amount, not the number of pennies. Input Validation: Do not accept a number less than 1 for the number of days worked.
The issue I have is that my output with 5 days is 2 - 4 - 6 - 8 - 10 while it should be 1 - 2 - 4- 8- 10. Now I know why I am getting the first result, but I have no idea how to get the 2nd result.
Even though I tried many combinations at the part where it goes wrong, I am still not getting a correct result. I could implant several if/else if statements to counter it, but am I wrong to think it could be solved without it as well? Again, not looking for solutions, but rather a hint.
This is my code:
Code:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
// Variable declaration.
int totalDays;
float total = 0;
// Input.
cout << "Pennies for Pay.\n" << endl;
cout << "Enter the number of days: ";
cin >> totalDays;
if (totalDays < 1)
{
cout << "\nError: days cannot be smaller than 0.\n" << endl;
return 0;
}
else
{
// Table creation.
cout << "\nDay(s)\tPennies" << endl;
cout << "----------------" << endl;
// Looping.
for (int day = 1; day <= totalDays; day++)
{
cout << day << "\t";
float temp = day * 2; // Calculation is wrong!
cout << temp << endl;
total += temp;
}
}
// Formatting.
cout << fixed << showpoint << setprecision(2);
// Show total.
cout << "\nTotal $: " << (total / 100) << "\n" << endl;
// End of program.
return 0;
}
lunaaaa
-
February 6th, 2011, 12:37 PM
#2
Re: Output from a for loop is incorrect due to wrong calculations.
You should be multiplying the total by two, not the day. There's no need for a temp variable at all.
FWIW, in C and C++, it's traditional and in most cases more practical to start your loop counter at 0 and keep looping while the counter is less than the value that should terminate the loop.
-
February 6th, 2011, 12:51 PM
#3
Re: Output from a for loop is incorrect due to wrong calculations.
 Originally Posted by GCDEF
You should be multiplying the total by two, not the day. There's no need for a temp variable at all.
The total variable in my code is an accumulator. In the first iteration it is 0 so multiplying it by 2 doesn't change that. Unless I am over thinking it - I am confused.
 Originally Posted by GCDEF
FWIW, in C and C++, it's traditional and in most cases more practical to start your loop counter at 0 and keep looping while the counter is less than the value that should terminate the loop.
Alright, noted. I started at 1 because the exercise wanted me to start at Day 1, rather than Day 0.
lunaaaa
-
February 6th, 2011, 01:03 PM
#4
Re: Output from a for loop is incorrect due to wrong calculations.
The number of the day is irrelevant, and it's good form and a good habit to start your loop counters at zero. All that matters is the loop runs the correct number of times. When you start dealing with arrays, you'll see why zero makes much more sense.
You need to keep track of two things. The current rate, which you'd initialize to 1 and double each time through the loop. The other thing is the total earnings so far. You'd initialize that to zero and add the current rate to it each time through the loop.
-
February 6th, 2011, 01:24 PM
#5
Re: Output from a for loop is incorrect due to wrong calculations.
I made those modifications, well as far as I could understand them. Changed the expression names in the for loop (like you said, they are irrelevant) and are now starting at 0. Created a variable currentRate and initialized it with 1. In each iteration it is doing * 2 on it.
However it is still showing me the wrong output.
Day 1 should be starting with 1 Penny and not 2. Day 2 should then be 2 and not 4. That is the issue I am having. If the person started with 2 Pennies then it is alright, but "it" did not.
Code:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
// Variable declaration.
int totalDays;
int currentRate = 1;
float total = 0;
// Input.
cout << "Pennies for Pay.\n" << endl;
cout << "Enter the number of days: ";
cin >> totalDays;
if (totalDays < 1)
{
cout << "\nError: days cannot be smaller than 0.\n" << endl;
return 0;
}
else
{
// Table creation.
cout << "\nDay(s)\tPennies" << endl;
cout << "----------------" << endl;
// Looping.
for (int i = 0; i < totalDays; i++)
{
cout << (1 + i) << "\t";
currentRate *= 2;
cout << currentRate << endl;
total += currentRate;
}
}
// Formatting.
cout << fixed << showpoint << setprecision(2);
// Show total.
cout << "\nTotal $: " << (total / 100) << "\n" << endl;
// End of program.
return 0;
}
lunaaaa
-
February 6th, 2011, 02:44 PM
#6
Re: Output from a for loop is incorrect due to wrong calculations.
Code:
for (int i = 0; i < totalDays; i++)
{
cout << (1 + i) << "\t";
currentRate *= 2; //this is in the wrong spot
cout << currentRate << endl;
total += currentRate;
}
The sequence of events inside your loop is wrong. This is the line that is giving you trouble.
-
February 6th, 2011, 03:02 PM
#7
Re: Output from a for loop is incorrect due to wrong calculations.
I am pretty much clueless right now.
I will give it another spin tomorrow, now it is just pissing me off.
lunaaaa
-
February 6th, 2011, 03:22 PM
#8
Re: Output from a for loop is incorrect due to wrong calculations.
 Originally Posted by lunaaaa
I am pretty much clueless right now.
I will give it another spin tomorrow, now it is just pissing me off.
lunaaaa
Everything is correct except the order of operations inside your loop.
Think about the state of your variables at the start of your loop. You have currentRate initialized to 1(which is correct, because this is where the pay starts) but you are modifying that value before you use it in the first iteration of your loop. Effectively giving your worker an early pay raise
-
February 6th, 2011, 03:30 PM
#9
Re: Output from a for loop is incorrect due to wrong calculations.
Hmm, lmao.
You were right.
Code:
for (int i = 0; i < totalDays; i++)
{
cout << (1 + i) << "\t";
cout << currentRate << endl;
total += currentRate;
currentRate *= 2;
}
That fixed it. I should read more about loops before I move on tho. I know what they do, just following them is still a bit, awkward.
lunaaaa
-
February 6th, 2011, 03:43 PM
#10
Re: Output from a for loop is incorrect due to wrong calculations.
Also, as it is, this program will fail if the user inputs anything > 31(not because you messed up). I have a feeling your instructor would be very impressed if you found out why this error occurred and how to handle it.
HINT: I am on a 32-bit machine so it fails for me...if you are using a 64-bit machine it probably won't fail for you at 32 days but it will fail at some point.
-
February 6th, 2011, 03:49 PM
#11
Re: Output from a for loop is incorrect due to wrong calculations.
 Originally Posted by lunaaaa
I should read more about loops before I move on tho. I know what they do, just following them is still a bit, awkward.
Loops are tough at first. I find that commenting each step of the loop with what is happening helps me visualize better.
Code:
for (int i = 0; i < totalDays; i++)
{
cout << (1 + i) << "\t"; //display the day number
cout << currentRate << endl; //display the current pay rate
total += currentRate; //pay the worker by adding the current rate to his total
currentRate *= 2; //raise the pay rate
}
-
February 6th, 2011, 04:10 PM
#12
Re: Output from a for loop is incorrect due to wrong calculations.
 Originally Posted by Austin.Soucy
Also, as it is, this program will fail if the user inputs anything > 31(not because you messed up). I have a feeling your instructor would be very impressed if you found out why this error occurred and how to handle it.
Wish I had an instructor. In probably 2-3 years I will have one, but I hope to have enough knowledge by then to sit back and relax at those classes.
 Originally Posted by Austin.Soucy
HINT: I am on a 32-bit machine so it fails for me...if you are using a 64-bit machine it probably won't fail for you at 32 days but it will fail at some point.
Most likely related to the integers I am using, being 32 bits by default (a sizeof shows they are 4 bytes on my system which is equal to 32 bits (4*8). Anything bigger and it will rollover. At least that is my guess.
 Originally Posted by Austin.Soucy
Loops are tough at first. I find that commenting each step of the loop with what is happening helps me visualize better.
Comments could help, but I have issues when using nested loops. I know that the inner loops will iterate more than the outside loops, but the book I am using only used one example that did not properly explain the concept.
Yet another annoyance that I have caught (the first one being using char arrays instead of strings) but it is of no concern. Sites like this help me find out what to use and what to avoid, which is a good thing.
lunaaaa
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|