is this right nowCode:#include <stdio.h> int main () { int x; int y; int i =1; int power; scanf(" %d", &x); scanf(" %d", &y); i= power * x; while (i<=y) { i++; } return 0; ++i; printf("int power is i"); }
is this right nowCode:#include <stdio.h> int main () { int x; int y; int i =1; int power; scanf(" %d", &x); scanf(" %d", &y); i= power * x; while (i<=y) { i++; } return 0; ++i; printf("int power is i"); }
Hi, the while loop needs to have:
statement in as well. Otherwise, you'll just find x^1.Code:power = power * x;
The specs seem a little out of order.
a. input integer variable x with scanf
b. input integer variable y with scanf
c. initialise integer variable i to 1
d. initialise integer variable power to 1
e. while i <= y
f. multiply power by x and assign the result to power
g. increment i by 1
h. output the integer variable power with printf.
I guess that's the point of the assignment though, to think about how to do the problem.Code:while (i <= y) { power = power * x; i++; }
Be aware that the program is finished when you reach a "return" statement in main().
The printf documentation may seem confusing if you have never done any programming before. Anyway, you cannot include the variable in quotes (with C); it will just literally print "i".
The green part is the "template". The underlined bits are "conversion specifiers". For each conversion specifier in the template, you need a matching argument. In this example, there are two (in blue), one is a "string" (a char pointer) and the other one (important to you) is a "digit", which would be an integer.Code:#include <stdio.h> /* needed for printf */ int main() { char example[]="hello world"; int x=5; printf("%s, x is %d\n", example, x); return 0; }
\n is a newline, try it without that to see the difference.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
is this right nowCode:#include <stdio.h> int main () { int x; int y; int i =1; //#c int power; //change to int power = 1; as per #d of assignment scanf(" %d", &x); //#a scanf(" %d", &y); //#b i= power * x; //put this inside while loop, before the ++i; while (i<=y) //#f { i++; //#g } return 0; //move this line to the very end of the program ++i; //remove this line printf("int power is i"); //#h I'm not sure if this should go inside the while loop or not // I think it should go inside, however. }
Let's see where we are:
a. input integer variable x with scanf
b. input integer variable y with scanf
c. initialise integer variable i to 1
d. initialise integer variable power to 1
e. while i <= y
f. multiply power by x and assign the result to power
g. increment i by 1
h. output the integer variable power with printf.
So, a few things to be fixed, yet.
Edit:
The console window your program is running in will close immediately after running, in many compilers. It's the default action of a
console window in Windows.
To keep the wiindow open long enough to see it properly, you might need to add this line of code:
getchar();
Note that each getchar() call will only remove *one* char from the keyboard buffer. If you have more than one, it will need some further help:
Code:while((c = getchar()) != '\n'); //removes all chars in the keyboard buffer including the first newline char. getchar(); //now getchar() by itself will wait for the next "enter" key before proceeding return 0;
Last edited by Adak; 03-15-2009 at 09:00 PM.