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

Laborator 2. Programare

The document describes a laboratory work on algorithms with alternative and loop structures in C language. The task is to calculate and output function values for an argument interval x1 to x2 with step dx. An algorithm and flowchart are presented showing the use of while and if-else statements. The C program code implements the algorithm by taking input, using conditional and loop statements, and outputting the results. Testing the program verifies the efficiency of the methods used to solve the problem.

Uploaded by

Natalia Chele
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
144 views

Laborator 2. Programare

The document describes a laboratory work on algorithms with alternative and loop structures in C language. The task is to calculate and output function values for an argument interval x1 to x2 with step dx. An algorithm and flowchart are presented showing the use of while and if-else statements. The C program code implements the algorithm by taking input, using conditional and loop statements, and outputting the results. Testing the program verifies the efficiency of the methods used to solve the problem.

Uploaded by

Natalia Chele
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

MINISTERUL EDUCAIEI REPUBLICII MOLDOVA

UNIVERSITATEA TEHNIC A MOLDOVEI Facultatea Calculatoare, Informatic i Microelectronic FILIERA ANGLOFON

RAPORT
Lucrare de laborator nr. 2 la Programarea calculatoarelor

A efectuat:

st. gr. FAF-121 (l. englez)


A verificat:

Gra Dumitru

dr., conf. univ.

Mihail Kulev

Chiinu 2012

LABORATORY WORK Nr. 2


Topic: Algorithms with alternative and loop structures. Conditional and loop statements in C language. Condition of the problem: To calculate and output on the screen argument and function values given by several expressions, for the argument interval xn x xk and step of argument dx. Parameters t,x are real numbers. Input data (chosen by user) have to be introduced from keyboard. Variant No. 7:

Work processing Short Theory on laboratory work topic & methods used in laboratory work
A program consists of a number of statements which are usually executed in sequence. Programs can be much more powerful if we can control the order in which statements are run. Statements fall into three general types;

Assignment, where values, usually the results of calculations, are stored in variables. Input / Output, data is read in or printed out. Control, the program makes a decision about what to do next.

This section will discuss the use of control statements in C. We will show how they can be used to write powerful programs by;

Repeating important sections of the program. Selecting between optional sections of a program.

C gives you a choice of three types of loop, while, do while and for.

The while loop keeps repeating an action until an associated test returns false. This is useful where the programmer does not know in advance how many times the loop will be traversed. The do while loops is similar, but the test occurs after the loop body is executed. This ensures that the loop body is run at least once. The for loop is frequently used, usually where the loop will be traversed a fixed number of times. It is very flexible, and novice programmers should take care not to abuse the power it offers.

The while loop is used when you want to execute a block of statements repeatedly with checked condition before making an iteration. Here is syntax of while loop statement:
while (loop condition) { // statements }

do while loop statement allows you to execute code block in loop body at least one. Here is do while loop syntax:
do { // statements } while (loop condition);

There are three parts which is separated by semi-colons in control block of the for loop:

The execution of the loop continues until the loop_condition is false. This expression is checked at the beginning of each loop iteration. The increment_expression, is usually used to increment the loop counter. This is executed at the end of each loop iteration.

Data analysis
Input data: t,x x1, x2 dx Output data: P Z simple variable of real type, current argument value; simple variable of real type, function value for current argument x. simple variables of real type, real parameters in the formula for F(x); simple variables of real type, endpoints of the interval to which the values of x belong simple variable of real type, the step of argument variation.

Algorithm Flowchart

START clrscr()
Enter t,dx,x1,x2

t,dx,x1,x2

x=x1
The results are:

x<=x2

x<3

P=2*cos(x)*cos(x)*cos( x)+pow(fabs(1-x),1/3.)

P=log(t)+sin(x*x)

Z=1+pow(fabs(x-t),1/6.)+P

x=x+dx
db, y, T

getch()

STOP

Text of the program in C language

#include<stdio.h> #include<conio.h> #include<math.h> int main() { float x1,x2,t,dx,x,P,Z; clrscr(); printf("Introduce variable t:"); scanf("%f",&t); printf("Introduce variable dx:"); scanf("%f",&dx); printf("Introduce interval x1 (x1>=1), x2 (x2<=6) \n"); scanf("%f%f",&x1,&x2); x=x1; printf("The results are"); while (x<=x2) { if(x<3) { P=2*cos(x)*cos(x)*cos(x)+pow(fabs(1-x),1/3.); } else { P=log(t)+sin(x*x); } Z=1+pow(fabs(x-t),1/6.)+P; x+=dx; printf("\n P=%f,Z=%f,x=%f",P,Z,x); } getch (); return 0; }

Input data , results

Conclusion
According to the main steps in using a computer as a problem-solving tool, such as performing data analyzing, creating an algorithm and a flowchart, writing the program in the C programming language, entering the program into the computer, testing and debugging it and at the end running the program properly, inputting the necessary data and getting the results from the computer, I personally had the opportunity to observe the used methods, algorithms and functions. To calculate the function set value it is necessary to use the preconditional countercontrolled while loop statement, the conditional statement if-else, printf() and scanf() functions, local variables of integer and real type, including the stdio.h, conio.h, math.h libraries. After the verification of the results outputted by the computer, the efficiency of the program is proved.

Bibliography
1. http://cprogramminglanguage.net/

You might also like