You are on page 1of 6

Ministry of Education and Research of the Republic of Moldova

Technical University of Moldova


Department of Software and Automation Engineering

REPORT
Laboratory work nr. 1
At Computer Programming

Performed by:
St.gr. FAF-233 Postoronca Dumitru

Checked by:
Dr., univ. conf. Mikhail Kulev

Chisinau – 2023
Laboratory work 1
Topic: Use of control and loop instructions in C language
Purpose of the laboratory work: Studying techniques and methods of use a condition control
instructions and cyclic instructions in the C language for tabulating the function.
Problem condition [1] : To calculate and display on the screen the values of the argument x and the
values of the function F, defined by 3 given expressions, for the interval х1 ≤ x ≤ х2 and the step px of
incrementing the argument x. The values x1, x2, px and the parameters a, b, c are input data of type real.
Variant 7:

Initial data values:


x1=-10; x2=10; px=2; a=1; b=2; c=3;

Short theory on laboratory work topic:


C language gives us a choice of three types of loop statements: while, for and do-while. The while and the
for loop statements refer to Pre-test loop statements. The do – while loop statement corresponds to Post-
test loop statement.
Loop Statement while - The while loop statement structure consists of a block of code and a condition.
The loop will repeat as long as the condition is true. The while tests its condition at the top of the loop.
Therefore, if the condition is false to begin with, the loop will not execute at all. Because the condition of
the loop is tested before the block is executed, the while loop is called a pre-test loop statement.
Loop Statement for - The for loop is usually used when the number of iterations is predetermined (
count- controlled loop) . Similar to the while loop, the for loop is a pre-test loop. Unlike other kinds of
loops, such as the while loop, the for loop is often distinguished by an explicit loop counter or loop
variable. This allows the body of the for loop (the code that is being repeatedly executed) to know about
the sequencing of each iteration
Do-while loop statement - The do - while construct consists of a block of code and a condition. First, the
code within the block is executed, and then the condition is evaluated at the end. If the condition is true
the code within the block is executed again. This repeats until the condition becomes false. Because do-
while loops check the condition after the block is executed, the do-while loop is also known as a post-test
loop. The syntax is: initialization; do { body of the loop;} while (condition); The do-while loop statement
is used when you want to execute the loop body at least once.
Description of data (variables): a) input data:
a, b, c – simple variables of real type that are used as parameters of formula(entered from keyboard)
b) output data:
x, F - simple variables of real type, the values of the calculation formulas (to be displayed on the screen).
c) working data: x1, x2 – simple variables of real type that declare the interval of the function(entered
from keyboard).
Program code (text) in C language (listing of the program):

#include<stdio.h>
#include<math.h>
float a, b, c, x1, x2;
float F, px;
int n;
float func(int x){
if (x-2 > 0 && a==0){
return (a + log(x))/sin(c)-b*b;
}else if(x-2 < 0 || a!=0 ){
return (x-sin(x+1))/b;
}else return (a*x+c)/cos(x*2);
}
char display(float x){
return printf("%i) x = %.2f\tF = %.2f\n", n, x, func(x));
}
int implem_for(float x){
n=1;
printf("Solving the function using for loop:\n");
for (x=x1; x < x2; x+=px){
display(x);n++;
}return printf("\n");
}
int implem_while(float x){
n=1;
printf("Solving the function using while loop:\n");
while (x < x2){
display(x);n++;
x+=px;
}return printf("\n");
}
int implem_doWhile(float x){
n=1;
printf("Solving the function using do-while loop:\n");
do{
display(x);n++;
x+=px;
}while(x < x2);
return printf("\n");
}
int implem_goto(float x){
n=1;
printf("Solving the function using goto+if loop:\n");
begin:
display(x);n++;x+=px;
if (x < x2) goto begin;
else return printf("\n");
}
int main(){
printf("input the boundaries: ");scanf("%f %f", &x1, &x2);
printf("input the step: ");scanf("%f", &px);
printf("input the a, b and c: ");scanf("%f %f %f", &a, &b, &c);
implem_for(x1);
implem_while(x1);
implem_doWhile(x1);
implem_goto(x1);
return 0;
}

Results of running and testing the program (screenshots) :


Verification of the results:

Analysis of results and conclusions:


1)After the work I developed the abilities to declare loops in 4 different ways
2)Observed that all loops work as a combination of if and goto statements
Bibliography
1.Carcea L., Vlas S., Bobicev V. Informatics: Tasks for laboratory works. Chisinau: UTM, 2005. - 19 p.
2. The overview of Computer Programming course lessons for students (lecturer: associate professor M.
Kulev). Chisinau, UTM, FCIM, 2023
3. http://www.wolframalpha.com/
Screenshots of codeforces tasks
1A Theatre square

#include<stdio.h>
#include<inttypes.h>
int main(const int64_t *value){
int64_t n, m, a, aForN, aForM, result;
scanf("%"SCNd64, &n);
scanf("%"SCNd64, &m);
scanf("%"SCNd64, &a);
if (n % a != 0) aForN = n/a + 1;
else aForN = n/a;
if (m % a != 0) aForM = m/a + 1;
else aForM = m/a;
result=aForM*aForN;
printf ("%"PRId64, result);
return 0;
}

2A winner
Sincerely I don’t understand source of the troubles at test 10 because the code
follows all requirements in the task as they are written

You might also like