Sie sind auf Seite 1von 26

Repetition Control Structure

Intro. To Repetition (Looping)

Repetition refers to the repeated


execution of a given set of code. The
programmer must provide a way to halt
the looping, otherwise you have an
infinite loop.
There are several looping mechanisms.
One of the simplest is the for
statement.
2

Intro. To Looping Continued

An example of the structure of the for statement is:

for (i = 1; i <= 4; i += 1)
{
.
.
.
}

Statements in the
loop

The above construct will execute the statements within the


braces 4 times

Example the for statement

Recall an earlier example about computing


tax, health surcharge and NIS for an
employee.
Assume we now want to perform this
computation for 4 employees.
Basically, all we have to do is insert within
the braces the code which handles one
employee.
This code, when embodied inside the braces,
will execute 4 times. Here is the complete
program.
4

The Program
//
//
//
//
//

This simple program illustrates the use of the for statement.


For each of 4 employees, it reads in a salary and computes tax,
health surcharge and NIS. If the salary is less than or equal to $50,000,
then tax, health and NIS are computed at rates which are lower
than if the salary exceeds $50,000.

#include <stdio.h>
void main()
{
double salary, tax, health, NIS;
int i;
// looping variable in the for statement
for (i = 1; i <= 4; i += 1)
{
printf_s("Enter salary \n");
scanf_s("%lf", &salary);
if (salary > 50000)
{
tax = 0.35 * salary;
health = 0.05 * salary;
NIS = 0.02 * salary;
}
else
{
tax = 0.28 * salary;
health = 0.035 * salary;
NIS = 0.015 * salary;
}

printf_s("Tax = %8.2f \n", tax);


printf_s("Health insurance = %8.2f \n", health);
printf_s("NIS = %8.2f \n", NIS);
} // brace to end the for statement

Exercise
For each of three students, read in the
assignment, midterm and final exam grades
obtained in a course. Using a for loop,
determine the course grade for each student
using the formula:
Course Grade = 10% of assignment
+
30% of midterm
+
60% of final exam
6

Variations of the for statement


Note that the structure:
for (i = 1; i <= 4; i += 1)
{
.
.
.
}
may be replaced by
for (i = 5; i <= 8; i += 1)
{
.
.
.
}
or by
for (i = x; i <= y; i += 1)
{
.
.
.
}
where x and y are variables with integer values and y is 3 more than x.

Variations Continued
The following is an example of another
variation:
for (i = 1; i <= 20; i += 2)
{
.
.
.
}
8

Exercise

Use the last variation to find the sum of


all even integers from 1 to 30. Display
the result as follows:
sum of even integers from 1 to 30 = 240

Exercise

Write a program to determine and


display the sum of the first five terms of
the geometric progression
6 18

54

10

Exercise

This exercise is a generalization of the


previous one. Your program should
allow the user to enter the first term of
the GP, the common ratio and the
number of terms whose sum is
required. It displays the sum.

11

Exercise
This question deals with determining the total payable by a
customer at a grocery store. For each of eight items purchased by
the customer, your program should read in the number of units
purchased and the unit price. Your program should determine and
output total payable by the customer.

12

Application 1

13

The Program
#include <stdio.h>
#include <math.h>
void main()
{
int i;
double term, val;
double temp;
temp = -1;
i = 1;
val = 1;
for (i = 1; i <= 19; i += 1)
{
term = pow (temp, i) * (1.0 / (2 * i + 1)); // first parameter of
val = val + term;

}
val = val * 4;
printf_s("Value of Pi = %3.8f \n", val);

// pow must be a double


14

Why 1.0?

Practice Problem
The Taylor expansion for the sine of an angle x is given by:
sin(x) = x / 1! - x3 / 3! + x5 / 5! - x7 / 7! + . . .

x3
x5 x 7
x9
sin ( x) x

. . . for all x.
3! 5! 7! 9!
x2
x4
x6
x8
cos ( x) 1

. . . for all x.
2! 4! 6! 8!
where x is in radians.
Write a C program to input an angle in degrees, and to apply the above
series to 20 terms to determine the sine of the angle.

15

Application 2
This application deals with determining
the area bounded by a curve and the xaxis. Let us consider the graph of
y = 3x2 - 7x + 2
Here is a sketch of the graph.

16

17

Application 2
The exact area is determined by integration as follows:
2

Area =

(3x2 - 7x + 2) dx

x3 - 7x2/2 + 2x

(8 14 + 4) (0.037037 - 0.388888 + 0.666666)

=
=

| -2.314809 |
2.314809

18

Application 2

In order to sketch the graph, one usually finds

Point of intersection with the y-axis (by setting


x = 0)
Point(s) of intersection with the x-axis (perhaps
by factorization, e.g., y = (3x 1)(x 2)
Point of maximum / minimum (by using first
derivative)
Having sketched the graph, we may now carefully
pick some points on the x-axis along with their
19
corresponding y-values.
These points will define a set of trapezoids which
could be used to approximate the required area.

Application 2

20

Application 2
In order to approximate the required
area, we proceed as follows:

Consider the x-values 0.40, 0.45, 0.50,


0.55, 1.90, 1.95, 2.0 (33 points), and
their corresponding y-values.
Each consecutive pair of x and y values
define a trapezoid.
The sum of the areas of these trapezoids.
approximates the area between the curve
and the x-axis.
The following program determines the sum
of the trapezoids.

21

Application 2
#include <stdio.h>
#include <math.h>
void main()
{
double
double
double
double
double
int i;

x1, x2;
fx1, fx2;
increment;
width;
totArea, tempArea;

increment = 0.05;
x1 = 0.40;
x2 = x1 + increment;
width = x2 - x1;
totArea = 0;
for (i = 1; i <= 32; i += 1)
{
fx1 = 3 * pow(x1, 2) - 7 * x1 + 2;
fx2 = 3 * pow(x2, 2) - 7 * x2 + 2;
tempArea = 0.5 * (fx1 + fx2) * width;
totArea = totArea + tempArea;
//Go on to next trapezoid

x1 = x2;
x2 = x1 + increment;
}
totArea = abs(totArea);
printf_s("Area under curve = %2.8f \n", totArea);
}

22

Application 2

With the starting point and increment


used in the last program, we get an
area of 2.302. Compare this with the
area found by integration (2.314809).
Question
How can we get a better
approximation?
Answer
Use a smaller increment. Also, use a
starting point of .

23

Application 2

The next program uses a starting point


of . It also uses an increment of 0.01
With these figures, it is difficult to
determine the exact number of intervals
to be used; this, of course, is the
number of iterations in the for
statement.
Instead of using the for statement, we
will use the while construct.
Here is the program. . .

24

Application 2
#include <stdio.h>
#include <math.h>
void main()
{
double
double
double
double
double

x1, x2;
fx1, fx2;
increment;
width;
totArea, tempArea;

increment = 0.01;
x1 = 1.0 / 3;
x2 = x1 + increment;
width = x2 - x1;
totArea = 0;
while (x2 <= 2.0)
{
fx1 = 3 * pow(x1, 2) - 7 * x1 + 2;
fx2 = 3 * pow(x2, 2) - 7 * x2 + 2;
tempArea = 0.5 * (fx1 + fx2) * width;
totArea = totArea + tempArea;
//Go on to next trapezoid
x1 = x2;
x2 = x1 + increment;
}
totArea = abs(totArea);
printf_s("Area under curve = %2.8f \n", totArea);
}
This procedure produces an area of 2.314621

25

ASSIGNMENT
26

Das könnte Ihnen auch gefallen