0% found this document useful (0 votes)
3 views

Week 3 FOR

The document provides an overview of floating-point types in programming, detailing their storage sizes, value ranges, and precision. It includes examples of how to use and print floating-point numbers, as well as mathematical functions and loops in C programming. Additionally, it covers specific programming tasks such as calculating sums, checking for perfect squares, and using loops for various operations.

Uploaded by

vuqarmirza44
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Week 3 FOR

The document provides an overview of floating-point types in programming, detailing their storage sizes, value ranges, and precision. It includes examples of how to use and print floating-point numbers, as well as mathematical functions and loops in C programming. Additionally, it covers specific programming tasks such as calculating sums, checking for perfect squares, and using loops for various operations.

Uploaded by

vuqarmirza44
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Floating point type

The following table provides the details of standard floating-point types with storage sizes and
value ranges and their precision:

Type Storage size Value range Precision

float 4 bytes [1.2*10-38.. 4.3*1038] 6 decimal places

double 8 bytes [2.3*10-308.. 1.7*10308] 15 decimal places

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.

double x = 1.234567890123456789; The result of 14 / 3 is 4, because we have here integer


double y = 14.0 / 3; division.
printf("%lf\n",x); // 1.234568
The result of 14.0 / 3 is 4.666667, because double / int
printf("%lf\n",y); // 4.666667
is converted to double / double, so the result is double.
Floating point type
The next table shows how to print the data with exact number of digits:
If <n> is greater than the number of
Type Format
printed digits, spaces are added at
the beginning of the number.
int %<n>d
Remember, that decimal point ‘.’
takes one position for printing.
double %<n>.<m>lf
If <n> is less than the number of
printed digits, the number is printed
• <n> means that the number will be printed in n positions. with the same number of digits as it
• <m> means that m digits will be printed after the decimal point. contains.

printf("%5.3lf\n", 1.987654); // 1.988


printf("%7.3lf\n", 1.987654); // 1.988 (two spaces before)
printf("%2.5lf\n", 1.21); // 1.21000
printf("%10.5lf\n", 1.21); // 1.21000 (three spaces before)
Floating point type
The next table shows how to print the data with exact number of digits: Type Format

int %<n>d

double %<n>.<m>lf
8604. Sum and product of real numbers

Three real numbers are given. Find their sum and product.

Input. Three real numbers x, y, z.

Output. Print in one line the sum and the product of three numbers with 4 decimal digits

Sample input Sample output


1.2345 3.4566 -0.1236 4.5675 -0.5274

scanf("%lf %lf %lf", &a, &b, &c);

s = a + b + c;
p = a * b * c;

printf("%.4lf %.4lf\n", s, p);


8825. Value of variable 1

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.

Sample input Sample output


1 7.286

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

Hence, for example, it follows that

double x = 81;
double y = pow(x, 1.0 / 4); // x ^ (1/4)
printf("%lf\n", y);

y = exp(log(x) / 4); // e ^ (ln x / 4)


printf("%lf\n", y);
8876. Integer

One real number n is given. Print Ok, if n is integer and No otherwise.

Input. One real number n.

Output. Print Ok, if n is integer and No otherwise.


Sample input Sample output
7.000 Ok

Number n is an integer if its integer part equals to itself.


That is, if the equality floor(n) = n takes place.

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.

Input. One positive integer n.


Output. Print the required answer.
Sample input 1 Sample output 1
25 5

Sample input 2 Sample output 2


27 No

Let a =  n . If a * a = n, then number n is a perfect square.


scanf("%lf", &n);
a = (int)sqrt(n);
if (a * a == n) printf("%d\n", a);
else puts("No");
For loop
The for statement (also called a for loop) is ideal when we know exactly how many times we need
to iterate.

The for statement looks pretty simple:

for (init-statement; condition-expression; end-expression)


statement; init-statement

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:

1) The init-statement is evaluated. Typically, the init-statement condition- FALSE


expression
consists of variable definitions and initialization. This statement is
only evaluated once when the loop is first executed. TRUE

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

3) After the statement is executed, the end-expression is evaluated.


Typically, this expression is used to increment or decrement the
variables declared in the init-statement. After the end-expression has
been evaluated, the loop returns to step 2.
For loop
Let’s look at a sample for loop:
i=1
#include <stdio.h>
no
i ≤ 10
int main(void)
{ yes

for(int i = 1; i <= 10; i++) printf(i)


printf("%d ",i);
printf("\n"); i++
return 0;
}

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.

Input. One positive integer n (n ≤ 100).


Output. Print n times the numbered text OK in column.
Sample input Sample output
3 1 OK
2 OK
3 OK

Use a for loop. Print n times the numbered text OK in column.


scanf("%d", &n);
for (i = 1; i <= n; i++)
printf("%d OK\n", i);
8934. Marathon 3

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.

Input. Two positive integers a and b (a ≤ b ≤ 1000).


Output. Print the numbers of athletes in decreasing order.
Sample input Sample output
3 7 7 6 5 4 3

Use a for loop. Print numbers from a to b in descending order.


scanf("%d %d", &a, &b);
for (i = b; i >= a; i--)
printf("%d ", i);
For loop
Consider the next program. Let’s simulate it. s=3
i=6
s = 3;
for (i = 6; i < 20; i = i + 3) no
printf(s)
i < 20
if (i % 2 == 1) s = s + i;
printf("%d\n", s); yes

i s i % 2 == 1
yes

init 6 3 no s=s+i

1 iteration 9 3 6 is even, nothing is added to s


i=i+3
2 iteration 12 12 s = s + i = 3 + 9 = 12

3 iteration 15 12 12 is even, nothing is added to s

4 iteration 18 27 s = s + i = 12 + 15 = 27

5 iteration 21 27 21 < 20 is false, stop for loop


For loop – omitted expressions
It is possible to write for loops that omit any or all the expressions.

int count = 0; Initialization and incrementing


for ( ; count < 10; )
are done manually
printf("%d ",count++);

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:

for (i = 0, j = 9; i < 10; i++, j--)


printf("%d %d\n", i, j);

This loop assigns values to two variables: i to 0, and j to 9.


It iterates i over the range 0 to 9, and each iteration i is incremented and j is decremented.
For loop – break
It is sometimes desirable to skip some statements inside the loop or terminate the loop
immediately without checking the test expression.
The break statement terminates the loop (for, while) immediately when it is encountered.
The break statement is used with decision making statement such as if…else.
For loop – break
Next program computes the sum of integers until user enters nonnegative number.

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

Find the sum of two numbers.


Input. The first line contains number of test cases t (1 ≤ t ≤ 100). Each test consists of two integers
a and b.
Output. For each tests case print in a separate line the sum of two numbers a and b.
Sample input Sample output
3 5
2 3 -1
17 -18 11
5 6
scanf("%d",&tests);
The input pair a and b does not have to be on the
for(i = 0; i < tests; i++)
same line.
{
Read the number of test cases t. scanf("%d %d",&a,&b);
Process the tests sequentially: read a couple of printf("%d\n",a+b);
numbers and print their sum. }
For loop – quiz
What is the answer in the next part of the code?

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

for (init; condition; increment)


{
for (init; condition; increment)
{
statement2;
}
statement1;
}
For loop – nested loops
Look at the next program. How many iterations takes place?
What will be printed?

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: Variable cnt will be increased


i = 1, j = 1, 2, 3, 4, 5; 5 * 5 = 25 times
i = 2, j = 1, 2, 3, 4, 5;
i = 3, j = 1, 2, 3, 4, 5;
i = 4, j = 1, 2, 3, 4, 5;
i = 5, j = 1, 2, 3, 4, 5;
For loop – nested loops
What is the output for the next programs:

int cnt = 0; int cnt = 0;


for (i = 4; i <= 6; i++) for (i = 10; i > 6; i--)
for (j = 2; j < 6; j++) for (j = 7; j >= 2; j--)
cnt++; cnt++;
printf("%d\n", cnt); printf("%d\n", cnt);
For loop – nested loops
What is the output for the next programs:

int cnt = 0; int cnt = 0;


for (i = 4; i <= 6; i++) for (i = 10; i > 6; i--)
for (j = 2; j < 6; j++) for (j = 7; j >= 2; j--)
cnt++; cnt++;
printf("%d\n", 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

You might also like