Loops can be used in many ways. They can perform the same calculations on many different starting values, as shown in the sine() examples above.
Loops are also commonly used to perform a series of calculations on a single starting value, to produce a single result. A common use for this sort of iteration is found in numerical analysis, which uses iterative calculations to solve many kinds of equations.
Integer powers can be computed using a simple loop.
/*************************************************************************** * Description: * * Arguments: * * Returns: * * History: * Date Name Modification * 2013-08-11 Jason Bacon Begin ***************************************************************************/ #include <stdio.h> #include <sysexits.h> int main(int argc,char *argv[]) { // Variable definitions double base, power; unsigned int exponent, i; // Statements printf("Enter base and exponent: "); scanf("%lf %u", &base, &exponent); power = 1.0; for (i = 1; i <= exponent; ++i) power *= base; printf("%f ** %u = %f\n", base, exponent, power); return EX_OK; }
!----------------------------------------------------------------------- ! Program description: ! Compute integer powers of real numbers !----------------------------------------------------------------------- !----------------------------------------------------------------------- ! Modification history: ! Date Name Modification ! 2011-03-10 Jason Bacon Begin !----------------------------------------------------------------------- ! Main program body program int_power use constants ! Constants defined above use ISO_FORTRAN_ENV ! INPUT_UNIT, OUTPUT_UNIT, ERROR_UNIT, etc. ! Disable implicit declarations (i-n rule) implicit none ! Variable definitions real(8) :: base, power integer :: exponent, i ! Statements print *, 'Enter base and exponent on one line:' read *, base, exponent power = 1.0d0 do i = 1, exponent power = power * base enddo print *, base, ' ** ', exponent, ' = ', power end program
Newton's method is a simple iterative approach for finding the roots of an equation of the form f(x) = 0.
How do we know when to stop iterating?
Note that Newton's method does not guarantee finding a root, even if one exists. Whether, and how fast it converges on a root depends on your initial guess.
#include <stdio.h> #include <sysexits.h> #define ABS(n) ((n) < 0 ? -(n) : (n)) int main(int argc,char *argv[]) { double x, y, slope, tolerance; printf("Enter initial guess for x: "); scanf("%lf", &x); printf("Enter the tolerance: "); scanf("%lf", &tolerance); // Compute first guess to prime the loop y = x * x - 1.0; while ( ABS(y) > tolerance ) { // Recompute x, y and slope slope = 2.0 * x; x = x - y / slope; y = x * x - 1.0; // Debug output printf("%f\n", y); } printf("%f\n", x); // This is the solution within tolerance return EX_OK; }
program newton ! Disable implicit declarations (i-n rule) implicit none real(8) :: x, y, slope, tolerance write (*,*) 'Enter initial guess for x:' read (*,*) x write (*,*) 'Enter the tolerance' read (*,*) tolerance ! Compute first guess to prime the loop y = x ** 2 - 1.0d0 do while ( abs(y) > tolerance ) ! Recompute x, y and slope slope = 2.0d0 * x x = x - y / slope y = x ** 2 - 1.0d0 ! Debug output write (*,*) y enddo write (*,*) x ! This is the solution within tolerance end program