1. Technology for efficient programming of algorithms

Introduction

C and C ++ standards, compilers

 Dev-C++, Code::Blocks for Windows, gcc for Linux, BSD, etc. UNIX ОС
Console applications, input and output

Standard input and output in C and C ++

C
scanf("<format>",<address>);
int ik; 
unsigned uk;
long lk;
unsigned long ulk;
double dk;
scanf("%d %u %ld %lu %lf", &ik, &uk, &lk, &ulk, &dk);

printf("<format>",<name>);

int ik = -10;
unsigned uk = 10;
long lk = -1000000;
unsigned long ulk = 1000000; // 16 bit
double dk = 2.52;
printf("%d %u %ld %lu %lf\n", ik, uk,lk, ulk, dk);

C++

cin >> <name>;

int k;
cin >> k;

cout << <name>;

int ik = -10;
unsigned int uk = 10;
long lk = -1000000;
unsigned long ulk = 1000000; // 16 битов цял тип данни
double dk = 2.52;
cout << ik << " " <<uk << " " << lk << " "
     << ulk<< " " << dk << "\n";

Input and output redirection

C or C ++ program file with standard input and output:
prog.exe

Program input text file::
test1.inp

Program run:
prog <test1.inp >test1.out

Text file produced by the program in this run:
test1.out

Example 1: Sum of two numbers

Two integers  in the interval [-100, 100] are given. Write a program that calculates the sum of the numbers.

#include <iostream>

using namespace std;
int main()
{
        int i, j;
        cin >> i >> j;
        cout << (i+j);    
        return 0;
}

Source file: add.cpp
Executable file (Windows): add.exe

Run:

C:\my_dir\>add < inp.txt > out.txt

Input file: inp.txt

12
5

Outpiut file: out.txt

17

Input for many test examples
- Input for fixed number of examples (numbers)
int n;
cin >> n;
for (int i = 0; i < n; i++) { cin >> ... }

- Input to the end of file for numbers and strings (words)
while (cin >> x) { ...}
while (cin >> x && x > 0) { ... }


-  Input to the end of file for strings (lines)
string st;
while(getline(cin, st)) { ... }

Example 2: Sum of two numbers
Two integers  in the interval [-100, 100] are given. Write a program that calculates the sum of the numbers..

Input
There are many examples from standard input. The first number is the number of examples, each following line contains two numbers to sum.

Output
For each example from the input, of the standard output (on a separate line), one number is output - the sum of the numbers from the corresponding input example.

Example:
Input
3
-10 2
20 3
30 4


Output
-8
23
34


Решение:
// Николай Киров F12345
#include <iostream>
using namespace std;

int main()
{
    int n;
    cin >> n;
    for (int k = 0; k < n; k++)
    {
       
int i, j;
        cin >> i >> j;
        cout << (i+j) << endl;
    }    

    return 0;
}

Решението като файл: f123456_0a.cpp

Example 3: Sum of two numbers
Two integers  in the interval [-100, 100] are given. Write a program that calculates the sum of the numbers.

Input
Many examples are entered from the standard input, each row contains two numbers to sum.

Output
For each example from the input, of the standard output (on a separate line), one number is output - the sum of the numbers from the corresponding input example.

Example:
Input
-10 2
20 3
30 4


Output
-8
23
34


Solution:
// Николай Киров F12345
#include <iostream>
using namespace std;

int main()
{
   
int i, j;
    while(cin >> i >> j) cout << (i+j) << endl;
    return 0;
}


Тестване на програмата:
- стандартни случаи;
- гранични случаи;
- минимални и максимални размерности на входа;
- специални случаи;
- НЕ се разглеждат случаи, които са извън зададените ограничения (НЕ се прави проверка за коректност на входа).
Testing the program:
- standard cases;
- borderline cases;
- minimum and maximum sizes of the input;
- special cases;
** Cases that are outside the set limits are NOT considered (DO NOT check the correctness of the input).

* Big numbers

Example 4: Sum of two numbers
Two integers  in the interval [-100, 100] are given. Write a program that calculates the sum of the numbers.

Input
Many examples are entered from the standard input, each row contains two numbers to sum.

Output
For each example from the input, of the standard output (on a separate line), one number is output - the sum of the numbers from the corresponding input example.

Example:
Input
-100000000000000000000000000 1
200000000000000000000000000000000000000 -
200000000000000000000000000000000000000

Output
-999999999999999999999999999
0

Solution:
// Николай Киров F12345
#include <iostream>
#include <string>
using namespace std;

string sum(string s1, string s2)
{
    ......
}

int main()
{
   
string i, j;
    while(cin >> i >> j) cout << sum(i, j) << endl;
    return 0;
}


* Personalization

Example 5: Sum of two numbers
Two integers  in the interval [-100, 100] are given. Write a program that calculates the sum of the numbers and student's faculty number.

Input
Many examples are entered from the standard input, each row contains two numbers to sum.

Output
For each example from the input, of the standard output (on a separate line), one number is output - the sum of the numbers from the corresponding input example.

Example:
Input
-10 2
20 3
30 4


Output
...

* Random generator.

Example 6: Sum of two numbers
 Find the sum of the smallest and largest numbers in the series of 100 random numbers in the interval[-100, 100].

Input
Many numbers (test examples) are entered from the standard input, each number specifies an initial value for a random series.

Output
For each example from the input, of the standard output (on a separate line), one number is output - the sum of the numbers from the corresponding input example.

Example:
Input
2
20
33


Output
5
-1
3

Solution:
// Николай Киров F12345
#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
   
int i;
    while(cin >> i)
    {

        srand(i);       
        int mi = 101, ma = -101;
        for (int j=0; j<100; j++)
        {
            int x = rand()%201 - 100;
            if (x > ma) ma = x;
            if (x < mi) mi = x;
        }
        cout << (ma + mi) << endl;
    }
    return 0;
}