|
Hey all,
everyone who posted in my last blog about failing in computer science was really helpful. I ended up using the code and advice you all provided me with, and I was able to pull a 90 on my exam!
I was wondering if you could all help me out some more, as I had a few questions on my homework.
I think the general consensus on this set of problems is that I don't quite understand loops. We had a while loop question on the exam, which I nailed, but while loops are the easiest ones, and I also think I must have gotten lucky, because I can't make shit work now.
anyway, here are the questions I'm having trouble with:
Sum of Numbers *Write a program that asks the user for a positive integer. The program should contain a loop to get the sum of all of the numbers from 1 to the number the user entered. For example, if the user enters fifty, the program should get the sum of all numbers from 1 through 50. Do not accept negative numbers.
+ Show Spoiler [Code so Far] + #include <iostream> using namespace std;
int main ()
{ int num, counter, sum = 0; counter = 0;
cout << "Please enter a positive number"; cin >> num;
while (num < 0 ) { cout << "The number you entered was negative.\n"; cout << "Please enter a positive number"; cin >> num; }
while (counter <= num) { sum += counter++; } cout << sum << endl;
return 0; }
Fixed this one and now it is working fine.
Pennies for Pay *Write a program that calculates how much money a person would earn if they earned one penny their first day, and then doubled their income each following day. The program should ask the user to input a number of days, and then display a table, showing each day, and then the income for that day. All values should be displayed in dollar amounts.
+ Show Spoiler [Code so Far] + #include <iostream> using namespace std;
int main()
{ int days; int count = 0;
cout << "This program calculates how much a person would earn\n"; cout << "if their salary started at one penny a day, and\n"; cout << "and doubled every day\n"; cout << "Please enter the number of desired days:\n"; cin >> days;
cout << "Days Money (in Dollars)\n"; cout << "_____________________\n"; while (count <= days) { cout << count << "\t\t$" << (count*2) <<endl; count++; } return 0; }
This code is pretty close. I know I need to replace "(count*2)" with something that exponentiates the values by the number of days that the code has run. I tried using something along the lines of "pow(1,2)" so that each day, the value from the day before is squared, but I'm at a loss as to how to square the value from the day before. Also, everytime I've tried to use "pow," I always get an error that the variable was not included in the scope, even though I include cmath.
The Greatest and Least of These *Write a program with a loop that lets the user enter a series of integers. The user should enter -99 to signal the end of the series. After all of the numbers have been entered, the program should display the largest and smallest numbers entered.
+ Show Spoiler + #include <iostream>
using namespace std;
int main () { int num;
cout << "Please Enter a positive number"; cin >> num;
if (num < 0){ cout << "The number you entered is negative.\n"; cout << "Please enter a positive number"; cin >> num; }
else { do{ cin>>num; if(num < lowNum) {lowNum = num;} if(num > highNum){highNum = num;} }
while (num != -99); }
Howitzer was awesome in providing me with code for this. I'm getting error messages that Well that all well and good, I don't fully understand it. I can see how each of the lines works, but I don't think I could replicate this if i was given a similar problem. Furthermore, I'm getting error message that say that INT_Min/MAX are not declared in this scope. Help with that please?
|
First one is most likely because you didn't set counter to anything. Adding a simple counter =0; should fix it. Also, you need a sum = 0; at the beginning.
For the second one, pow requires double/float inputs not int so you can change the variable type. Also, the amount you have each day is given by 2^count-1 since the first day you would have 2^1-1=1, second day is 2^2-1=2 ect. So using pow and count as one of the inputs, you should be able to get it.
For the third one, do you need to keep track of all the numbers that the user inputs? If not, you can set a min and max to the first number the user inputs and then check each subsequent number to se if its smaller than the min or larger than the max and update it that way.
|
You never initialize counter or sum for sum of numbers, which in C means it can be whatever happens to be sitting in whatever chunk of memory happens to be assigned for that variable. Initializing sum and counter to 0 should fix that. You'll also need to preincrement rather than postincrement counter the way you have it written right now. Also, dunno if you've learned them yet, but that sort of problem really begs for a for loop, not a while loop.
Should probably reuse the code to exclude negative values for the second problem; the problem statement doesn't say to but negative days don't make sense there. There are a couple ways to deal with the doubling, the simplest being to just make another variable to keep track. Unless you're dealing with extremely tight memory considerations, there's no reason you should be afraid to create an int or two. Another note: if using a second variable to keep track of the doubling, you should probably make it an unsigned long, since int will run out of space to hold that very quickly.
I believe (without looking it up to be sure) that the constants you're referencing in 3 are called MAX_INT and MIN_INT, which would be why those are undefined errors. Were there any other problems? At a glance the solution looks fine, if inelegant. You actually don't need to create an array at all for that problem, nor more than one loop. Can you see how you would do it without those?
|
As for as pennies for pay is concerned, you can work out a formula to calculate the amount without using a loop.
For the last one, I am uncertain what is INT_MIN and INT_MAX. Frankly, you don't need those.
here's the pseudocode:
WHILE the user does not enter -99, DO: save the number into the next empty slot of your array; if this is the first number, let Max and Min equal to that number; LOOP i from 0 to the Counter, DO: if the current number is bigger than Max, make Max = current number; if the current number is smaller than Min, make Min = current number; END LOOP END WHILE
OUTPUT Max, Min, etc.
|
@Total_Dark and Macanenger: Thanks guys, I can't believe I missed such simple things. Oh well, thanks a lot.
I also noticed that in the original, I wrote while (counter < num), instread of (counter <= num). just more silly mistakes.
|
To solve the pennies problem you need to realize that the doubling of the salary is an exponential function where the salary for a particular day is equal to 2 raised to the power of the day -1. ie: P(d) = 2^(n-1). This would mean on the first day your pay is 2^0 or 1cent. The second day your pay is 2^1 or 2 cents. Third day: 2^2 or 4 cents. Fourth: 2^3 = 8. Nth: 2^(n-1). To do this: you use a simple for loop. for (int i=1; i<=days; i++) { cout<<"Day: "<<i<<"\t"<<"Salary: $"<<pow(2, i-1)/100<<\n"; } also, for this you need to make sure you #include <math.h> This should be the only tricky part of this problem. Also I didn't test it, so if you get an error lemme know.
For the second problem you will use a do while loop. The condition of this loop will be in pseudo code: "while the user input does not have the value of -99". You want a do while loop because you want it to run at least once. To do this you would do:
do{ cin>>usrInput; if(usrInput<lowNum) {lowNum = usrInput;} if(usrInput>highNum){highNum = usrInput;} } while (usrInput != -99);
don't forget the ending semicolon.
|
On October 12 2010 12:05 KiLL_ORdeR wrote: I also noticed that in the original, I wrote while (counter < num), instread of (counter <= num). just more silly mistakes. Well, it can still work that way (that's why I said pre instead of postincrement), but yeah, changing it to <= is fine too.
|
u will need to #include <climits> for your last program to get the definition of INT_MIN/MAX
|
On October 12 2010 12:06 Macavenger wrote:Show nested quote +On October 12 2010 12:05 KiLL_ORdeR wrote: I also noticed that in the original, I wrote while (counter < num), instread of (counter <= num). just more silly mistakes. Well, it can still work that way (that's why I said pre instead of postincrement), but yeah, changing it to <= is fine too. Ya, the program still runs, just not with the correct output. For example, if I wanted to do all of the numbers up to 3, if I had (counter < num) it will only add 1 and 2. But ya, just minor details, not very important for the entire problem.
On October 12 2010 12:05 TyPsi5 wrote: To solve the pennies problem you need to realize that the doubling of the salary is an exponential function where the salary for a particular day is equal to 2 raised to the power of the day -1. ie: P(d) = 2^(n-1). This would mean on the first day your pay is 2^0 or 1cent. The second day your pay is 2^1 or 2 cents. Third day: 2^2 or 4 cents. Fourth: 2^3 = 8. Nth: 2^(n-1). To do this: you use a simple for loop. for (int i=1; i<=days; i++) { cout<<"Day: "<<i<<"\t"<<"Salary: $"<<pow(2, i-1)/100<<\n"; } also, for this you need to make sure you #include <math.h> This should be the only tricky part of this problem. Also I didn't test it, so if you get an error lemme know.
For the second problem you will use a do while loop. The condition of this loop will be in pseudo code: "while the user input does not have the value of -99". You want a do while loop because you want it to run at least once. To do this you would do:
do{ cin>>usrInput; if(usrInput<lowNum) {lowNum = usrInput;} if(usrInput>highNum){highNum = usrInput;} } while (usrInput != -99);
don't forget the ending semicolon.
I thought this at first, but I was looking at the equation, and if I use an exponential equation, I'm one value off every day.
What i'm saying is, on day 0, she should have 0 pennies, because she hasn't started working yet. on day 1, she has 1 penny, which doubles to 2 on day 2, which becomes 4 on day 3, 8 on day 4, and so on. Am I right or am I over-thinking this?
|
+ Show Spoiler +On October 12 2010 12:26 KiLL_ORdeR wrote: I thought this at first, but I was looking at the equation, and if I use an exponential equation, I'm one value off every day.
What i'm saying is, on day 0, she should have 0 pennies, because she hasn't started working yet. on day 1, she has 1 penny, which doubles to 2 on day 2, which becomes 4 on day 3, 8 on day 4, and so on. Am I right or am I over-thinking this?
Wow holy shit disregard that, I didn't read carefully enough obviously.
|
No you are correct. Day.....Salary......equation 1 1 2^0 2 2 2^1 3 4 2^2 4 8 2^3 n 2^(n-1) you can see the equation for the salary for a specific day is equal to 2 raised to power of the day minus 1.
|
On October 12 2010 12:05 TyPsi5 wrote: To solve the pennies problem you need to realize that the doubling of the salary is an exponential function where the salary for a particular day is equal to 2 raised to the power of the day -1. ie: P(d) = 2^(n-1). This would mean on the first day your pay is 2^0 or 1cent. The second day your pay is 2^1 or 2 cents. Third day: 2^2 or 4 cents. Fourth: 2^3 = 8. Nth: 2^(n-1). To do this: you use a simple for loop. for (int i=1; i<=days; i++) { cout<<"Day: "<<i<<"\t"<<"Salary: $"<<pow(2, i-1)/100<<\n"; } also, for this you need to make sure you #include <math.h> This should be the only tricky part of this problem. Also I didn't test it, so if you get an error lemme know.
For the second problem you will use a do while loop. The condition of this loop will be in pseudo code: "while the user input does not have the value of -99". You want a do while loop because you want it to run at least once. To do this you would do:
do{ cin>>usrInput; if(usrInput<lowNum) {lowNum = usrInput;} if(usrInput>highNum){highNum = usrInput;} } while (usrInput != -99);
don't forget the ending semicolon.
So the pennies one now works perfectly. That's crazy man. I don't even think I have to reformat it to make a table. And now I also know why the "pow" function wqasn't working, I needed to use <math.h> instead of <cmath>.
|
On October 12 2010 12:26 KiLL_ORdeR wrote: Ya, the program still runs, just not with the correct output. For example, if I wanted to do all of the numbers up to 3, if I had (counter < num) it will only add 1 and 2. But ya, just minor details, not very important for the entire problem. Right, but what I'm saying is that + Show Spoiler +while (counter < num) { sum += ++counter; } produces the same answer as + Show Spoiler +while (counter <= num) { sum += counter++; } because counter is incremented after the test before the addition, instead of after the addition.
On a related note, it's also worth noting that using pre or postincrement in the middle of a statement as you're doing here, while perfectly correct and frequently useful, can also lead to code that is very difficult to read and thus often discouraged. May be the source of the misunderstanding here.
|
On October 12 2010 12:05 TyPsi5 wrote: do{ cin>>usrInput; if(usrInput<lowNum) {lowNum = usrInput;} if(usrInput>highNum){highNum = usrInput;} } while (usrInput != -99);
don't forget the ending semicolon.
How do I properly assign lowNum and highNum to be the lowest and highest numbers?
|
Canada8028 Posts
do{ cin>>usrInput; if(usrInput<lowNum) {lowNum = usrInput;} if(usrInput>highNum){highNum = usrInput;} } while (usrInput != -99);
don't forget the ending semicolon.
Does this code work? It looks like it'd take the -99 to be an actual input since usrInput is evaluated at the end of the loop.
|
On October 12 2010 12:42 Macavenger wrote:Show nested quote +On October 12 2010 12:26 KiLL_ORdeR wrote: Ya, the program still runs, just not with the correct output. For example, if I wanted to do all of the numbers up to 3, if I had (counter < num) it will only add 1 and 2. But ya, just minor details, not very important for the entire problem. Right, but what I'm saying is that + Show Spoiler +while (counter < num) { sum += ++counter; } produces the same answer as + Show Spoiler +while (counter <= num) { sum += counter++; } because counter is incremented after the test before the addition, instead of after the addition. On a related note, it's also worth noting that using pre or postincrement in the middle of a statement as you're doing here, while perfectly correct and frequently useful, can also lead to code that is very difficult to read and thus often discouraged. May be the source of the misunderstanding here. Oh ok I understand what you're sayiong. Thanks for clarifying and a big thanks for the help.
|
On October 12 2010 12:42 KiLL_ORdeR wrote:Show nested quote +On October 12 2010 12:05 TyPsi5 wrote: do{ cin>>usrInput; if(usrInput<lowNum) {lowNum = usrInput;} if(usrInput>highNum){highNum = usrInput;} } while (usrInput != -99);
don't forget the ending semicolon.
How do I properly assign lowNum and highNum to be the lowest and highest numbers?
It was my understanding that the lowNum and highNum were to be the lowest and highest numbers entered by the user resepectively.
In which case, it need not be changed, as long as lowNum and highNum are initialized to proper values. ie a really high number for the lowNum and a really low number for highNum.
|
On October 12 2010 12:47 Spazer wrote:do{ cin>>usrInput; if(usrInput<lowNum) {lowNum = usrInput;} if(usrInput>highNum){highNum = usrInput;} } while (usrInput != -99);
don't forget the ending semicolon. Does this code work? It looks like it'd take the -99 to be an actual input since usrInput is evaluated at the end of the loop.
I haven't tested it, but its a fairly straightforward do-while loop. The main difference between a do-while and a while loop is that in a do-while loop the "do" code is run at least once before the condition is checked. In this particular loop, it prompts the user for input, and does 2 conditional statements, before finally checking to see if the value stored in usrInput does not have a value of -99.
In fact, the code should probably be this:
do{ cin>>usrInput; if(usrInput == -99){break;} if(usrInput<lowNum) {lowNum = usrInput;} if(usrInput>highNum){highNum = usrInput;} } while (usrInput != -99);
|
I'm including the code I have for last problem both here and in the OP.
When I try to compile the code, it stops compiling when it hits the usrInput lines.
also not I changed usrInput to num just to make it more concise.
+ Show Spoiler + #include <iostream>
using namespace std;
int main () { int num;
cout << "Please Enter a positive number"; cin >> num;
if (num < 0){ cout << "The number you entered is negative.\n"; cout << "Please enter a positive number"; cin >> num; }
else { do{ cin>>num; if(num < lowNum) {lowNum = num;} if(num > highNum){highNum = num;} }
while (num != -99); }
|
Canada8028 Posts
Wait, does one number need to be positive? Or do they all need to be positive? Because
if (num < 0) is only running once. I don't think you need it in there since it's not in the question description. =/
Also, if you're gonna use the do-while loop, add the break statement mentioned in one of the posts above.
|
|
|
|