Beruflich Dokumente
Kultur Dokumente
for (i = 1; i <= 4; i += 1)
{
.
.
.
}
Statements in the
loop
The Program
//
//
//
//
//
#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;
}
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 Continued
The following is an example of another
variation:
for (i = 1; i <= 20; i += 2)
{
.
.
.
}
8
Exercise
Exercise
54
10
Exercise
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);
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
=
=
| -2.314809 |
2.314809
18
Application 2
Application 2
20
Application 2
In order to approximate the required
area, we proceed as follows:
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
23
Application 2
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