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

Lab 1

Uploaded by

markyg2023
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Lab 1

Uploaded by

markyg2023
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

COMPUTER SCIENCE I

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 );

The printout should be formated as follows:


First number : 12.4
Second number : 32.22
Multiplication : 399.5
Division : 3.85e-001
square roots :|3.52 | 5.68|
cube roots :2.314589 3.182061 (use tabulator)
fourth powers :23642 1077711

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.

5. Execute following code:


double a, b;
double w1 = a * a – b * b;
double w2 = ( a + b ) * ( a – b );
printf(” w1 = %lf w2 = %lf\n”, w1, w2 );

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?

6. Modify the definition of the π (this part is optional):

#define PIa 4.*atan(1) // without brackets


#define PIb 4*atan(1) // without dot at 4

Compare results of the following operation for al. three definitions of π :

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?

You might also like