"...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
Sorry for dragging this out painfully, but it's not always easy to explain something without just blurting out the solution, which would rob you of a learning experience. Thanks for being patient =)
" initialization, condition, and the iterative change"
Should i have the initialization at 2 since i'm using it as my initial value? What should my condition be to stop it when necessary?
"Sorry for dragging this out painfully, but it's not always easy to explain something without just blurting out the solution, which would rob you of a learning experience. Thanks for being patient =)"
No thank you! I need help and you're actually helping me.
"Can you write one that will compile without errors?"
The only thing i see that i messed up on was capitalizing "for".
It's probably better to use a separate variable just for the counter. Although there are a few different ways to approach this, I'd recommend starting the counter at zero (initialization) and counting up to 'n'. (Well, one less than 'n', since you'd be starting from zero and not one.)
This would require three variables: a counter for the loop, the maximum number of times to loop (based on the exponent), and the running result.
Look again.
For, While and Do While Loops in C - Cprogramming.com
Edit: In C, punctuation matters more than in English.
Tim S.
"...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 also separated the "for()" arguments with commas instead of semi-colons.The only thing i see that i messed up on was capitalizing "for".
how about this?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 = PWR; loop_counter++) { loop_counter*PWR; } system("pause"); return 0; }
If you have a good Compiler with warnings up high; you should get a message saying this line has no effect.Code:loop_counter*PWR;
I suggest comparing "*=" with "*" in what they do.
Also, compare "==" with "=".
Edit3: http://en.wikipedia.org/wiki/Operato..._C%2B%2B#Table
Tim S.
Last edited by stahta01; 04-26-2013 at 06:30 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
Well then... What am i doing wrong? All you're telling me is that i'm wrong and then not helping me. I'm not having you guys do my homework, i'm trying to problem solve.
In addition to what Tim mentioned:
- "scanf()" requires a pointer to a variable (i.e. "&PWR")
- The middle argument of the "for()" dictates when the loop ends - when that condition is no longer true, the loop terminates. You don't have a condition there, you have an assignment.
- I mentioned to keep your loop/counter variable separate from any variables used in calculations
"...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
Even with those changes i'm getting errors.
Post the updated code!