You are on page 1of 3

Lesson 10: Program Iterations - C Tutorial Todayâ s lecture is also examples mostly, but this time Iâ ll introduce you to iterations

and you will see why these program lines are common in every programmerâ s code. Just donâ t be confused when you see i= i++ (this really bugged me when I was first introduced to world of programming). How the heck can i be equal to i+1 ? You have to think outside of the box this time, and forget regular math.

Program Iterations Program iterations are used to process certain programâ s block, multiple number of times. Number of repeats (iterations) can be, or donâ t have to be previously known. We have following iterations that: test condition before starting iteration (while, for) test condition at the end (do-while) Iterations that tests iteration condition at the beginning Example: Write your own program that will read given positive-whole numbers, until number zero is given. Then it will print which given number was the smallest. The program must ignore given negative numbers (given = keyboard inputted). Warning: While searching for smallest (biggest) filed member or flow, we use following algorithm: first member in field is declared smallest and itâ s value is assigned to temp variable, which presents current minimum. we search through other field members and if one of them is smaller then our current minimum, we update our temp variable to show new current minimum. after we processed the whole field, temp variable now stores the real minimum member of field. Example: flow: 5, 6, 3, 9, 4, 7, 2, -1, 5.

#include <stdio.h>void main() { int min, x = 0; while(x <= 0){ printf ("Input number : "); scanf("%d", &x); min = x; // w e assume first given number is the smallest } while (x != 0) {printf("Input

number : "); scanf("%d", &x); if(x > 0 && x < min ) printf("Smallest given number is: %d\n", min);} Alternative approach (to avoid double storing)

min = x;

}

#include <stdio.h>void main() { int min=0, x=1; // in first iteration x=1 wh ile (x != 0) { printf("Input number : "); scanf("%d", &x); //after f irst iteration x becomes != 1 if (x > 0) { if ((min==0) || (x < m in)) min = x; } } printf("Smallest given number is: %d\n", m in);}

Example: Program reads positive-whole numbers until their sum reaches value bigger than approved range short int allows. Program also prints last valid sum. #include <stdio.h>void main() { short int x, sum = 0, end = 0; while (!end ) { printf("Input number : "); scanf("%d", &x); if ((signed sho rt)(sum+x) >= sum) { //why cast? sum += x; } else { end = 1; } } printf("Sum: %d\n", sum);} Example of programs testing: Input number : 30000 Input number : 10 Input number : 10000 Sum: 30010 Is the number of iterations in this program block previously known?

Example: Program prints first 100 numbers that are dividable with 7: #include <stdio.h>void main() { int i=0, n=0; i % 7 == 0) { printf ("%d\n", i); while (n < 100) { n++; } i++; if ( }}

Example: Program reads positive-whole number and calculates range of following numbers in this way: if the given number is even, then divides it with 2 if the given number is odd, then multiplies it with 3 and adds 1 The procedure is repeated until given number becomes 1. Program prints current value in every step. Program also prints number of calculations above processed number. #include <stdio.h>void main() { int number, nrSteps = 0; printf("Give positi

ve number:"); scanf("%d", &number); while (number > 1) { ++ nrSteps; if (number % 2){ number = number * 3 + 1; } else { number /= 2; } printf ("In %d. step, number = %d \n", nrSteps, number); } printf ("All together %d steps. \n", nrSteps); }

Give positive number: 9 In 1. step, number = 28 In 2. step, number = 14 In 3. step, number = 7 In 4. step, number = 22 In 5. step, number = 11 In 6. step, number = 34 In 7. step, number = 17 In 8. step, number = 52 In 9. step, number = 26 In 10. step, number = 13 In 11. step, number = 40 In 12. step, number = 20 In 13. step, number = 10 In 14. step, number = 5 In 15. step, number = 16 In 16. step, number = 8 In 17. step, number = 4 In 18. step, number = 2 In 19. step, number = 1 All together 19 steps.