This is a Clilstore unit. You can link all words to dictionaries.

Structure of program in С/C++ language. Input and output operators. Linear programs

Program in С/C++ language consists of the following components:

  1. Program heading
  2. Description of variables
  3. Program body

Program heading begins with the preprocessor directive #include  <name of the header file>. In one program can be several header files. Standard function libraries used in the program are stored in the header file.  Directive include connects the appropriate file.

For example: To connect standard mathematical functions listed in Table 3, it is necessary to connect filemath.h. I.e., write the following command: #include  <math.h>.

The header file has the extension h (h- header - header). 

As in Pascal, in C++ language it is required to describe (announce) the used variables.  General view of the description:

name_of the typelist_of variables;

For example:  int a,b;

char k1,k2,k3;

                 unsigned char c;

float x1,y1;

                 double e,f,g;  

Program body consists of one main function with the standard name main():

main()

{

statements;

}

Program body are enclosed in braces {}.

 

Formatted display

Formatted display operator has the following structure: printf (formatted string, list of arguments);

Formatted string is constrained by double quotation marks (i.e., is the text constant), and may include free text, control symbols and format specifiers. Format specifier will determine the form of the external representation of the output value. Format specifier begins with % symbol. The following format specifiersare used:

%d – decimal integer

%f – real numbers in the form with fixed point (float)

%u – decimalunsigned integer (type unsigned)

%e – real numbers in the form with float point (double)

%c – symbol

%s – string

%ld – long integer (longint)

%hu – short unsigned integer (shortunsigned)

%Lf – long real (longdouble)

Examples of use of  printf() statement:

printf(“\nSum=%d”, S);

printf(“\n  S =%d”, S);

printf(“ Result of the task”);

printf(“ \n  circle area=%f”, 3.14*R*R);

To display real numbers, the numeric parameters can be added to the format specifier: field width and accuracy.

Field width – is the number of positions to be allocated on the screen for value;

accuracy – is the number of positions for the fractional part (after point).

Parameters are written between icon % and format symbol and are separated by point from each other.

For example:  x=5.234532;

                 printf(“\n x=%4.2f”,x);

As a result it will be displayed x=5.23.

 

Formatted input from the keyboard

Operator of the formatted input from keyboard has the following structure:  scanf(formatted_string, list_of arguments);

List of arguments – is a list of the input variables, where icon & is put before the name of each variable.

For example: scanf(“%d”, &x);

scanf(‘%d%d%f”,&a,&b,&y);

Input and output operators printf(), scanf() are stored in header file stdio.h. Therefore, to use these operators in program it is required to write command #include<stdio.h>. (stdio - standart input output) in the program header.

Example 1.  Program for finding of the length of circumference and circle area by a known radius.

#include <stdio.h>

#include <conio.h>

main()

{ const  float pi=3.14;

int  r; float  L,S;

printf(“enter radius”);

scanf(“%d”,&r);

S=pi*r*r;

L=2*pi*r;

printf(“\n S=%4.2f  L=%4.2f”, S,L);

getch();

}

During program execution, if enter number 5, as a result of calculation will receive the following values  S=78.50 L=31.40.

To delay program operation results on the screen, operator getch() is used from header file  conio.h. 

Example 2. Program for calculation of the sum of digits of the three-digit integer.

#include <stdio.h>

#include <conio.h>

main()

{ int x,y,a,b,c;

printf(“ enter three-digit number”);

scanf(“%d”,&x);

a=x/100;

y=x%100;

b=y/10;

c=y%10;;

printf(“\n S=%d”,a+b+c);

getch();

}

In the program, operation “%” defines the remainder of the division of integers, and“/”identifies the integer part when dividing numbers.

Programming language С++ has own specific input-output means. It is a class library connected to the program using file iostream.h.  In this library the Standard symbol streams are identified as objects with the following names:

       cin    -  standard input stream from keyboard;

  cout  -  standard output stream to screen;

For example: input of values in variable х is implemented by operator cin>>x;

Data output is defined as placement into standard stream cout of the output values. Texts enclosed in double quotes, and values of expressions can be output. Sign of the operation of placement into stream <<.

For example: cout<<x*y;

cout<<”\nSum=”<<S;

cout<<”a=”<<a<<”b=”<<b<<endl;

The output element end l is a so-called manipulator, defining movement of the cursor to the newline (acts like a control symbol \n).

Example 3. Find triangle sides by coordinates of its three vertices.

#include <iostream.h>

#include <conio.h>

#include <math.h>

main()

{ int x1,y1,x2,y2,x3,y3; float a,b,c;

cout<<"enter coordinates of 1st vertex"; cin>>x1>>y1;

cout<<"enter coordinates of 2nd vertex"; cin>>x2>>y2;

cout<<"enter coordinates of 3rd vertex"; cin>>x3>>y3;

a=sqrt(pow((x2-x1),2)+pow((y2-y1),2));

b=sqrt(pow((x3-x1),2)+pow((y3-y1),2));

c=sqrt(pow((x2-x3),2)+pow((y2-y3),2));

cout<<"а = "<<a<<"\n b= "<<b<<"\n c== "<<c;

getch();

}

As a result of execution of the program it will be displayed on the screen

a=1.4142

b= 1

c= 2.2360 

Short url:   http://multidict.net/cs/6244