"...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson
You edited it after i posted ;p
Go back to the math. If PWR == 0, then you want to generate 1, if PWR == 1, then you want to generate 1 · 2, if PWR == 2, then you want to generate 1 · 2 · 2 , and so on.
What would be the condition to stop it? loop_counter < PWR?Code:#include <stdio.h> #include <stdlib.h> int main() { int loop_counter = 2, PWR = 0; printf("enter an interger:\n "); scanf("%d", &PWR); for (loop_counter = 0; loop_counter <; loop_counter++) { loop_counter*=PWR; } system("pause"); return 0; }
Well, that would be the condition to keep going - but you have the right code, as the loop will stop when the condition becomes false.hat would be the condition to stop it? loop_counter < PWR?
Also, keep "loop_counter" separate - don't use it in the calculation. Put the result of the multiplication in a separate variable (perhaps called "result").
"Go back to the math. If PWR == 0, then you want to generate 1, if PWR == 1, then you want to generate 1 · 2, if PWR == 2, then you want to generate 1 · 2 · 2 , and so on."
PWR is a variable for the user inputted exponent. loop_counter is the variable i want to multiply.
So should i multiply result to PWR?
Code:#include <stdio.h> #include <stdlib.h> int main() { int loop_counter = 2, PWR = 0, result = 0; printf("enter an interger:\n "); scanf("%d", &PWR); for (loop_counter = 0; loop_counter < PWR; loop_counter++) { result*=PWR; } system("pause"); return 0; }
OOOOOOOOOOOOOOOOOH! I know what you mean by return now lol
How does this look?Code:#include <stdio.h> #include <stdlib.h> int main() { int loop_counter = 2, PWR = 0, result = 0, new_counter = 2; printf("enter an interger:\n "); scanf("%d", &PWR); for (loop_counter = 0; loop_counter < PWR; loop_counter++) { result = new_counter*=PWR; } printf("%d\n", result); system("pause"); return 0; }
I think you will find that 0 is NOT the best value to use.Code:(loop_counter = 0;
You changed the code enough to get to work with 0; but, the code is ugly to me.
Tim S.
Last edited by stahta01; 04-26-2013 at 07:27 PM.
"...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson
Whenever i input something though it's outputting wrong numbers.
I'm VERY new to coding. I just learned about arithmetic expressions and looping this week in class.
I would have thought that loop_counter++ would of added 1 until it reached the value of PWR. But it's going beyond that.
That is not the problem. Your for starting at loop counter at 0, and checking for < PWR is OK. You need to initialize result to 1 before starting the loop. Then looking at your loop, what it's currently doing is factorial, not powers of 2, your loop is doing 1 · 2 · 3 · 4 ... . So how can you fix your loop so it's doing 1 · 2 · 2 · 2 ... ?
Did your teacher cover bitwise operations too?Originally Posted by sourpatchkid
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)