# C Loop iterates but only prints once

Australia

Hey guys, just going to start off by saying I'm rather new to C. My problem is that I wrote this program for uni homework, but instead of getting eleven results printed out, I only get one. For instance:

a = 0.00; b = 1.00; c = 4.00; d = -6.00; e = 3.00; guess = 10.00

I was wondering whether anyone would be kind enough to give me advice on what I'm doing wrong?

P.S. Sorry, I tried to format it better, but I have no idea how to make my code look nice on this page.

# define TOLERANCE 0.000005

int main(void) {

``````size_t  counter=0;

double  valueA=0.0,
valueB=1.0,
valueC=4.0,
valueD=-6.0,
valueE=3,
initialGuess=0.0,
guess=0.0,
guessNext=0.0,
polynomial=0.0,
derivative=0.0;

//Initial Guess is set to -10.0 and will increment in steps of 2.0 each time the loop is iterated until
//the initial guess is set to 10.0 and eleven results have been produced
for (initialGuess = -10.0; initialGuess <= 10.0; initialGuess += 2.0); {

guess = initialGuess; //Guess is set to initialGuess so that it can be modified while still being able to display the value of initialGuess

//Calculate the 4th order polynomial
polynomial = (pow((valueA * guess), 4)) + (pow((valueB * guess), 3)) + (pow((valueC * guess), 2)) + (valueD * guess) + (valueE);

//If the polynomial calculation is within the range of 0 and 0.000005, then the initial guess
//has stumbled on a root of the polynomial.
if (polynomial >= 0.0 && polynomial <= TOLERANCE) {

printf("a = %.2f; b = %.2f; c = %.2f; d = %.2f; e = %.2f; guess = %.2f", valueA, valueB, valueC, valueD, valueE, initialGuess);
printf("\n");
printf("Gives F(x) = %f at x = %f ..... Root found after 0 iterations.", polynomial, initialGuess);
printf("\n");

} else {

//If the polynomial calculation is greater than 0.000005, calculate the derivative of the polynomial
derivative = (pow((4 * valueA * guess), 3)) + (pow((3 * valueB * guess), 2)) + (2 * valueC * guess) + (valueD);

//If the derivative is within the range of 0 and 0.000005, Newton's method will not converge
//as you can't divide by 0 and multiplying a number by a very small number will result in a very large number
if (derivative >= 0 && derivative <= TOLERANCE) {

printf("a = %.2f; b = %.2f; c = %.2f; d = %.2f; e = %.2f; guess = %.2f", valueA, valueB, valueC, valueD, valueE, initialGuess);
printf("\n");
printf("\n");

}

//Iterates Newton's method 50 times
for (counter = 0; counter < 50; counter++) {

//Calculate Newton's method to get the next guess and set the old guess to it.
guessNext = guess - (polynomial / derivative);
guess = guessNext;

//Calculate the new polynomial and derivative
polynomial = (pow((valueA * guess), 4)) + (pow((valueB * guess), 3)) + (pow((valueC * guess), 2)) + (valueD * guess) + (valueE);
derivative = (pow((4 * valueA * guess), 3)) + (pow((3 * valueB * guess), 2)) + (2 * valueC * guess) + (valueD);

//Check for the same conditions previously
if (polynomial >= 0.0 && polynomial <= TOLERANCE) {

printf("a = %.2f; b = %.2f; c = %.2f; d = %.2f; e = %.2f; guess = %.2f", valueA, valueB, valueC, valueD, valueE, initialGuess);
printf("\n");
printf("Gives F(x) = %f at x = %f ..... Root found after 0 iterations.", polynomial, initialGuess);
printf("\n");

} else if (derivative >= 0 && derivative <= TOLERANCE) {

printf("a = %.2f; b = %.2f; c = %.2f; d = %.2f; e = %.2f; guess = %.2f", valueA, valueB, valueC, valueD, valueE, initialGuess);
printf("\n");
printf("\n");

}

}

//If the loop has iterated 50 times, it is assumed that the next guess cycled between the same region
//and that the method did not converge
if (counter == 50) {
printf("a = %.2f; b = %.2f; c = %.2f; d = %.2f; e = %.2f; guess = %.2f", valueA, valueB, valueC, valueD, valueE, initialGuess);
printf("\n");
printf("\n");

}
}

}
``````

return 0;

}

• Australia

@tienkhoanguyen Ah thank you for responding My problem wasn't so much with the maths as it was with the for loop only printing one result instead of eleven. I've modified it from a for loop to a while loop and it seems to be functioning properly now.

• Australia

Sorry to double post, but I just realised that I had a semicolon after my for loop. I'm an idiot..

• Australia

Haha thanks. I've learnt java before. And I'm doing C++ as well this semester, so C feels familiar but at the same time rather strange and archaic.