Page 2 of 2

### Re: Help with my scripts

Posted: December 1st, 2017, 9:47 am
You seem to be resetting the variable bB within the loop in int power().

### Re: Help with my scripts

Posted: December 1st, 2017, 9:58 am
Rhombic wrote:A bit of a change of scene now that I feel very comfortable with Python...now my issues involve C.

Code: Select all

``````#include <stdio.h>

int factorial(int n){
int nN; int i;
for(i=0;i<n;i++){
nN=1;
nN*=(n-i+1);
}
return nN;
}
int power(int b, int exp){
int bB; int i;
for(i=0;i<exp;i++){
bB=1;
bB*=b;
}
return bB;
}
int main(){
printf("Showing the minimum n for which A^n > A! \n A \t n");
int i;
for(i=1;factorial(i)<1000000000;i++){
int c; c=1;
while (c>0){
if (power(i,c)>factorial(i)){
printf("%d \t %d",i,c);
break;
}
c++;
}
}
return 0;
}
``````
The script is fairly simple. For integer A, it gives the smallest integer n so that A^n > A!. It doesn't work and, unlike Python, C doesn't really help you much with the debugging. How can I find the issue?
In this case the problem is not so much with C syntax but a logic error - a Python version of this program would have the same behaviour. The main issue is that in both functions you use an intermediate value to help calculate the result (nN and bB) but inside the loop you set it to 1 on every iteration. You need to move the initialisation of these variables outside of the loop.
A minor point: for each value i of A, you are calculating factorial(i) as many times as you call power(), plus one extra in the main loop test. This is very inefficient so you'll want to store that result until the next i.
I don't have a compiler handy, so I'm assuming that the code compiles. If not then you really should state what the error messages are when asking for help. And in any case it's better to be descriptive rather than vaguely say "it doesn't work".

### Re: Help with my scripts

Posted: December 1st, 2017, 10:44 am
I can't believe I missed that! It compiles fine, but it still does not print any numbers (so it's either stuck in a loop or "not finding anything"; the latter should actually be impossible).
The variables bB and nN are now set before the for loop, so even though that clearly was a problem there has to be something else. The program runs fine, just does not print anything.
Here it is.

Code: Select all

``````#include <stdio.h>

int factorial(int n){
int nN; int i; nN=1;
for(i=0;i<n;i++){
nN*=(n-i+1);
}
return nN;
}
int power(int b, int exp){
int bB; int i; bB=1;
for(i=0;i<exp;i++){
bB*=b;
}
return bB;
}
int main(){
printf("Showing the minimum n for which A^n > A! \n A \t n");
int i; int F;
for(i=1;F=factorial(i)<1000000000;i++){
int c; c=1;
while (c>0){
if (power(i,c)>F){
printf("%d \t %d",i,c);
break;
}
c++;
}
}
return 0;
}
``````
power(i,c) does return i^c, right? and factorial(i) is hopefully working OK too?

### Re: Help with my scripts

Posted: December 1st, 2017, 11:04 am
I suggest you try "running" the code with pencil and paper, just to help you identify what it's actually doing. Start with i=1 in the main loop and step through line by line, keeping track of all the variables. You will find problems with the factorial function and the while loop (hint: for what value of C will the loop stop for i=1?).

Edit: alternatively you can do the same thing with a debugger, but if you're using gcc in mingw then gdb is the only option I'm aware of and it's not very learner friendly.

### Re: Help with my scripts

Posted: December 1st, 2017, 7:50 pm
Thank you, found the problem (starting at 1...).