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().
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.Rhombic wrote:A bit of a change of scene now that I feel very comfortable with Python...now my issues involve C.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?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; }
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;
}