Lab 1
Lab 1
Exercise 1
1. Create and execute the following program:
#include <stdio.h> // header for standard input/output (printf)
void main() // definition of the main function
{ // main function starting bracket
printf(”Hello!\n”);
} // main function ending bracket
Declare two integer variables (a and b) and print result of: sum, subtraction, multiplication and
division. The results should be printed in a single line.
Zadeklaruj dwie zmienne całkowite (a i b) i wyświetl wynik: dodawania, odejmowania, dzielenia i
reszty z dzielenia. Wyniki wyświetl w jednym wierszu. Sprawdź przypadki, gdy a > b i a < b.
2. Write a program which reads from keyboard two numbers of double type and prints results of
following operations: multiplication, division, square root, cube root, fourth power, sinus and
cosinus. Print first what kind of input is expected before the program starts waiting for input:
printf(”Input first number:\n”);
scanf(”%lf”, &a );
3. Write a program which reads from keyboard coordinates of three arbitrary points In 2D.
Calculate distances between those points. Calculate also the area and perimeter of a triangle defined
by the points. Use Heron formula P = s ⋅ (s − a ) ⋅ (s − b ) ⋅ (s − c ) , where s = 1 (a + b + c ) , and a, b,
2
c are lengths of the triangle sides. Format printout of the results in a clear and readable way.
4. Modify the program from previous point by adding code calculating angles of the triangle
according to the following formulas:
γ
tg =
( p − a )( p − b ) , β
tg =
( p − a )( p − c ) , α
tg =
( p − b)( p − c ) .
2 p( p − c ) 2 p( p − b ) 2 p( p − a )
Print the angles in degrees with precision to three decimal places. Check also if the calculated
angles fulfill the condition: π = α + β + γ . The π can be defined using preprocessor command:
#define PI (4.*atan(1))
(remember not to use semicolon At the end of the preprocessor commands!). Do you know what
exactly the command means? If the condition is not satisfied print the error.
This time you can assign values to variables a and b directly inside the code. Run the program for
following values:
• 100000 and 100001;
• 1000000 and 1000001;
• 10000000 and 10000001;
• 100000000 and 100000001; etc.
What the analytical result? Can you continua to increase input values ad infinity? Repeat the same
calculations for floats. Can you explain the results?
printf( ”%lf\n”, 3 / PI );
printf( ”%lf\n”, 3 / PIa );
printf( ”%lf\n”, 3 / PIb );
Which results are true? What is the reason for the errors?