Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the program set to print out the final iteration. I am just unsure on how to print out each iteration so that I can see it approaching the root.

C++
#include <stdio.h>
#include <math.h>

double f(double x)
{
return 3*(x+1)*(x-.5)*(x-1);  //change equation for each problem
}

int main()
{
    double a, b, p, Tol, FA, FP;
    int i=1;
    int No;

    printf("Enter first endpoint: ");
    scanf ("%lf", &a);

    printf("Enter second/last endpoint: ");
    scanf ("%lf", &b);

    printf("Desired Tolerance: ");
    scanf ("%lf", &Tol);

    printf("Maximum Iterations: ");
    scanf ("%d", &No);
    FA = f(a);

    while (i<=No)
    {
        p=a+(b-a)/2;
        FP = f(p);

        if ((FP==0)||((b-a)/2)< Tol)
        {
            printf("%lf\n",p);
            break;
        }
        i++;  //i=i+1

        if ((FA*FP)>0)
        {
            a=p;
            FA=FP;
        }
           else
           {
                b=p;
           }
        }
   if (i>No)
   {
   printf("Method Failed after %d", No);
   printf(" iterations");
   }
}


[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 3-Feb-14 6:19am
v2

1 solution

Just add another printf, or move the existing one
If you want to print each iteration with it's number and current value, then add a line at the bottom:
C++
printf("Iteration %d: Current value = %lf", i, p);
Otherwise, just move the existing printf outside the if condition:
C++
printf("%f\n",p);
if ((FP==0)||((b-a)/2)< Tol)
{
    break;
}


[edit]Changed output format from integer to float - OriginalGriff[/edit]
 
Share this answer
 
v2
Comments
MalDrHoop 3-Feb-14 12:37pm    
Thank you. I've been trying to learn some programming while in this course that requires programming and, advisor mess up, and so I appreciate your help with this. I'm figuring it out though I got the majority of this code with some guidance from my professor or by myself. I'll be prepared when I do take my programming class next semester!
MalDrHoop 3-Feb-14 12:48pm    
From the first method im just getting:

Enter first endpoint: -2
Enter second/last endpoint: 1.5
Desired Tolerance: .01
Maximum Iterations: 20
Iteration 1: Current value = 0Iteration 2: Current value = 0Iteration 3: Current
value = 0Iteration 4: Current value = 0Iteration 5: Current value = 0Iteration
6: Current value = 0Iteration 7: Current value = 0Iteration 8: Current value = 0
Iteration 9: Current value = 0
Process returned 20 (0x14) execution time : 8.780 s
Press any key to continue.

Why is it giving me zero everytime?

MalDrHoop 3-Feb-14 13:00pm    
I found the issue. I tried it before your edit. I did not realize you edited it before I came back to comment. But it was the %d instead of the %lf in correspondence with my p
OriginalGriff 3-Feb-14 14:34pm    
Yeah, I noticed just after I pressed the "send" button... :O
MalDrHoop 3-Feb-14 15:07pm    
It's fine lol. I should have noticed before I implemented it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900