Laboratory work ¹1. Basics of C++:

1.1. Structure of a program.

1.2. Variables. Data Types.

 

Let’s consider the example where it is neseccary to create a program which evaluate the sum: y=a+2b+c

 

# include <iostream.h>

# include <conio.h>

main()

{

int a,b,c;    //the variable  a,b,c  of integer type

float  y;      // the variable ó of real type

cin>>a>>b>>c;

y=a+2*b+c;

cout<<y;

getch();

}

Variants of the individual tasks

Find the value of a function

variants

Task¹

Function

Intermediate function

Values of the variables

 

A

;

a=0.1

b=9.5

B

t=10

x=1.1

 

A

b=21

y=2.2

B

f=3.2

x=1.4

 

A

f=42.5

x=10.2

B

x=-0.007

 

A

a=2.3

x=0.2

B

b=3.7

y=1.02

 

A

x=9.325

B

a=7.25

b=3

 

A

x=13.7

B

a=5

b=3.25

 

A

a=7

n=4

B

u=3.47

 

A

a=10.3

B

a=4.25

 

A

a=1.25

b=3

B

a=7.2

b=5

 

A

a=7.2

f=0.5

B

a=4.25

 

 

A

t=3.2

 

B

x=2

 

A

a=4.2

b=2.7

B

a=2.25

b=10.27

 

A

;

a=2.25

b=10.27

B

a=1.5

b=1.2

 

A

a=3

b=1.2

B

a=24

b=1.9

 

A

a=2.5

b=2a+3/1.5

B

a=1.1

b=2

 

 

 

Laboratory work ¹2.  If and while constructions

 

Variants

Tasks

1

2

1

2

3

 

4

5

6

7

8

9

10

11

12

13

14

15

 

sample of the performing the task

Example 1. Write  program which calculate  the N!-factorial

 

# include <iostream.h>

# include <conio.h>

main()

{

int n,i,p=1;

cin>>n;

for(i=1;i<=n;i++)

            p:=p*i;

cout<<p;

getch();

}

 

Example 1. Find most maximum element (ak) of a sequence a1,a2,a3,… an and its ordinal number (k).

# include <iostream.h>

# include <conio.h>

main()

{ int i,k=1,a,n,m;

cout<<”n=”; cin>>n;

cout<<”Insert the 1-st number” cin>>m;

for(i=2;i<=n;i++)

{ cout<<”Insert ”<<i<<”-number”; cin>>a;

  if (a>m) {m=a; k=i;}}

cout<<” most maximum element:”<<m;

cout<<” its ordinal number:”<<k;

getch();

}


Laboratory classe ¹3. Using control structures

 

 

variant

Task¹

Sum of unlimited series

 

Value of Õ

e accuracy

 

A

0,20

0,00001

B

0,10

0,00005

 

A

0,15

0,0001

B

0,12

0,0001

 

A

0,70

0,0001

B

-

0,0001

 

A

1,5

0,0005

B

-

0,00005

 

A

1,7

0,001

B

-

0,00005

 

A

0,75

0,0005

B

0,62

0,0001

 

A

0,20

0,0001

B

0,30

0,00001

 

A

0,25

0,0001

B

-

0,00005

 

A

0,50

0,0001

B

-

0,001

 

A

-

0,00005

B

0,45

0,0001

 

A

-

0,0005

B

0,90

0,00005

 

A

-

0,001

B

0,15

0,00001

 

A

-

0,00005

B

0,20

0,00005

 

A

0,75

0,0005

B

0,65

0,0001

 

A

0,35

0,00005

B

-

0,00005

 

 

 

 

Examples

 

Example 1.

This problem may be decided by means of several ways.

1) Evaluate a sum of an indefinite series

with precision  eps=10E-4 where õ=0,1.

To solve this problem it is necessary to find a common member of a series:

using a recurrent relation  we express n-member through  (n-1)- member:

We define the first member of a series a=x before a loop,  and then the other members will be defined by means of recurrent relation:

Program code:

 

# include <iostream.h>

# include <conio.h>

# include <math.h>

main()

{

float x,eps,a,s;  int n;

cin>>x; cin>>eps;

a=x; s=0; n=1;

         while (abs(a)>=eps)

         { s=s+a; n=n+1;

           a=-a*x*x/((2*n-2)*(2*n-1));

         }

cout<<”The sum of series = ”<<s;

cout<<”The number of series’ members =”<<n;

getch();

}