Week 3 FOR
Week 3 FOR
The following table provides the details of standard floating-point types with storage sizes and
value ranges and their precision:
It’s preferable to use double type because nowadays computers have math coprocessor.
All operations with floating-point type are executed in math coprocessor.
By default %lf format prints 6 decimal digits.
int %<n>d
double %<n>.<m>lf
8604. Sum and product of real numbers
Three real numbers are given. Find their sum and product.
Output. Print in one line the sum and the product of three numbers with 4 decimal digits
s = a + b + c;
p = a * b * c;
Find the value of the variable y for a given real value of the variable x.
3 5x2 3
y x 9x 1
7 x
Input. Value of the variable x.
Output. Print the value of the variable y up to thousandths.
scanf("%lf", &x);
y = x * x * x - 5 * x * x / 7 + 9 * x - 3 / x + 1;
printf("%.3lf\n", y);
Math functions
To work with algebraic and trigonometric functions, include the library <math.h> to the program:
#include <math.h>
Algebraic functions:
Math functions
To work with algebraic and trigonometric functions, include the library <math.h> to the program:
#include <math.h>
#include <stdio.h>
#include <math.h>
int main()
{
printf("%lf\n", sqrt(2)); // 1.4142
printf("%lf\n", pow(2, 10)); // 1024.000
printf("%lf\n", log(32) / log(2)); // log2 (32) = 5, 2 ^ 5 = 32
printf("%d %lf\n", abs(-34), fabs(-34.67)); // 34 34.670000
return 0;
}
Math functions
To work with algebraic and trigonometric functions, include the library <math.h> to the program:
#include <math.h>
Trigonometric functions:
Constant π is not defined in the library <math.h>. It can be obtained by calculating arccos (-1):
const double PI = acos(-1.0);
Math functions
Rounding functions:
#include <stdio.h>
#include <math.h>
int main()
{
double x = 2.678;
printf("%lf %lf\n", floor(x), ceil(x)); // 2.000 3.000
return 0;
}
Math functions
To compute the powers of numbers, it is sometimes convenient to use the relation
double x = 81;
double y = pow(x, 1.0 / 4); // x ^ (1/4)
printf("%lf\n", y);
scanf("%lf", &n);
if (floor(n) == n) puts("Ok");
else puts("No");
8877. Perfect square
Positive integer n is given. If n is a perfect square of some positive integer m, then print m.
Otherwise print No.
condition- FALSE
expression
TRUE
statement
end-expression
For loop
for (init-statement; condition-expression; end-expression)
statement;
init-statement
Evaluation of for statements
A for statement is evaluated in 3 parts:
statement
2) The condition-expression is evaluated. If this evaluates to false,
the loop terminates immediately. If this evaluates to true, the
statement is executed.
end-expression
Initialization i = 1.
Iteration while condition i ≤ 10 is true, execute the body of the for loop:
printf("%d ",i);
i++;
The loop stops when i = 11. For this value of i the condition i ≤ 10 becomes false.
8931. All OK
Positive integer n is given. Count and print the message OK as shown in the sample.
The race participants were provided with numbers from a to b and entered this information into
computer in reverse order. Print the numbers of athletes.
i s i % 2 == 1
yes
init 6 3 no s=s+i
4 iteration 18 27 s = s + i = 12 + 15 = 27
Although you do not see it very often, it is worth noting that the
following example produces an infinite loop:
for (;;)
statement;
For loop – sequence of numbers
5328. Find minimum
Alarm in Summer Computer School! Minimum is lost. You must find it.
Input. The first line contains number n (1 ≤ n ≤ 1000). In the second line n integers are given, each of
them is no more than 105 by absolute value.
Output. Print the minimum among the n given numbers.
Sample input Sample output
4 -4
5 8 -4 6 scanf("%d", &n);
min = 100000;
Assign variable min to infinity. for(i = 0; i < n; i++)
Iterate over the given numbers – if the next {
number is less than min, update the final scanf("%d", &val);
minimum. if (val < min) min = val;
}
printf("%d\n", min);
For loop – multiple declarations
Although for loops typically iterate over only one variable, sometimes for loops need to work with
multiple variables. When this happens, the programmer can make use of the comma operator to
assign (in the init-statement) or change (in the end-statement) the value of multiple variables:
s = 0;
for(;;)
{
scanf("%d",&x);
// if user enters negative number, loop is terminated
if (x < 0) break;
s = s + x;
}
printf("%d\n",s);
For loop – continue
The continue statement skips some statements inside the loop.
The continue statement is used with decision making statement such as if…else.
For loop – continue
Next program computes the sum of maximum of 5 numbers. Negative numbers are skipped from
calculation.
s = 0;
for(i = 0; i < 5; i++)
{
scanf("%d",&x);
// if user enters negative number, loop is continued
if (x < 0) continue;
s = s + x;
}
printf("%d\n",s);
For loop – multiple test cases
518. Sum of two
s = 2; s = 5;
for (i = 3; i <= 11; i += 2) for (i = 5; i < 20; i += 5)
s = s + 3; s = s + 2;
printf("%d\n", s); printf("%d\n", s);
s = 1; s = 0;
for (i = 15; i > -5; i -= 5) for (i = 3; i < 20; i += 3)
s = s * 2; {
s = s + 3;
printf("%d\n", s);
i = i + 2;
}
printf("%d\n", s);
For loop – nested loops
C language allows to use one loop inside another loop.
The syntax for a nested for loop statement in C is as follows
int cnt = 0;
for (i = 1; i <= 5; i++)
for (j = 1; j <= 5; j++)
cnt++;
printf("%d\n", cnt);
For each value of i we run a j loop: For each value of i we run a j loop:
i = 4, j = 2, 3, 4, 5; i = 10, j = 7, 6, 5, 4, 3, 2;
i = 5, j = 2, 3, 4, 5; i = 9, j = 7, 6, 5, 4, 3, 2;
i = 6, j = 2, 3, 4, 5; i = 8, j = 7, 6, 5, 4, 3, 2;
i = 7, j = 7, 6, 5, 4, 3, 2;
cnt = 3 * 4 = 12
cnt = 4 * 6 = 24