Standard C does not allow definition a function within another. Move definitions of functions like
int ask()
outside ofmain()
.The
if ( n == 0 ){ n++; }
favors the return value of 1- a bias. Alternative:// return [-level...-1, 1...level] int nonZeroCoefficient(void) { int n = rand()%(2*level); if (n % 2) { return n/2 + 1; } return -(n/2 + 1); }
Likewise for
nonZeroRand()
. In this case, code could simply re-try to avoid the1
bias.int nonZeroRand(void) { int n; do { int n=(rand()%randMax - randMax); } while (n == 0); return n; }
stdout
may not be buffered as expected. Usefflush()
to insure output is seen before asking for input.printf("\nEnter f:"); fflush(stdoout); // add scanf("%d", &inputF);
Robust code checks the return value of user input.
// scanf("%d", &inputF); if (scanf("%d", &inputF) != 1) Handle_Error();
Curious that code did not define
f,g,h,k
in alphabetically order.int f = nonZeroCoefficient(); int h = nonZeroCoefficient(); int k = nonZeroRand(); int g = nonZeroRand(); // why last?
Formating to the width of the review presentation is appreciated.
}else{ printf("\nSorry! your answer is not currect!\nthe currect answer is:\nf:%d\ng:%d\nh:%d\nk:%d\n \ntry again",f, g, h, k);
... versus (also check spelling)
}else{ printf("\nSorry! your answer is not correct!\n" "the correct answer is:\nf:%d\ng:%d\nh:%d\nk:%d\n \n" "try again",f, g, h, k);
Consider declaring/defining variables when needed.
// int a, b, c, level=1, randMax=level*5; // .... many lines later // a = f*h; // b = (f*k + g*h); // c = g*k; int a = f*h; int b = (f*k + g*h); int c = g*k;
Important tip for future coder efficiency (your efficiency). Do not manually format code as the below code implies. Use your coding environment's auto formatter (or get one that does that) that meets your group's coding style. Life is too short for such manual picayune tasks - like white-space concerns
// OP's scanf("%d", &inputK); if (inputF*inputH == a && ... // Auto formated scanf("%d", &inputK); if (inputF * inputH == a && ....