Structural Programming

Exercise 5

1. Control structures for loops

1.5. Task 4

Write a program that will print all four-digit numbers where the sum of the three least significant digits is equal to the most significant digit.

4031 (4=0+3+1), 5131 (5=1+3+1)

Solution:

    #include <iostream>
    using namespace std;

    int main() {
        int i, n, sum, first_digit, digit;
        i = 1000;
        while (i <= 9999) {
            first_digit = i / 1000;
            n = i % 1000;
            sum = 0;
            while (n > 0) {
                digit = n % 10;
                sum += digit;
                n /= 10;
            }
            if (sum == first_digit) cout << i << "\t";
            i++;
        }
        return 0;
    }

1.6. Task 5

Write a program that will print all numbers from a given range that are read the same from left to right and from right to left.

example numbers

12321 5061605

Solution:

    #include <iostream>
    using namespace std;

    int main() {
        int i, from, to, temp, op, digit;
        cin >> from >> to;
        for (i = from; i <= to; i++) {
            temp = i;
            op = 0;
            while (temp > 0) {
                digit = temp % 10;
                op = op * 10 + digit;
                temp /= 10;
            }
            if (op == i) cout << i << "\t";
        }
        return 0;
    }

1.7. Task 6

Write a program that will determine the number with the maximum value from an unknown number of integers entered from the keyboard. The program ends if an invalid number representation is entered.

Solution:

    #include <iostream>
    using namespace std;

    int main() {
        int n, max;
        if (cin >> max) {
            while (cin >> n) {
                if (max < n) {
                    max = n;
                }
            }
            cout << max;
        } else {
            cout << "No number was entered.";
        }
        return 0;
    }

1.8. Task 7

Write a program that will determine the number with the maximum value from an unknown number of integers entered from the keyboard. In this case, numbers greater than 100 are not taken into account i.e. they are ignored. The program ends if an invalid number representation is entered.

Solution:

    #include <iostream>
    using namespace std;

    int main() {
        int n, max;
        if (cin >> max) {
            while (cin >> n) {
                if (n > 100) continue;
                if (max < n) {
                    max = n;
                }
            }
            cout << max;
        } else {
            cout << "No number was entered.";
        }
        return 0;
    }

1.9. Task 8

Write a program that will determine the two numbers with the highest values from an unknown number of integers entered from the keyboard. The program ends if an invalid number representation is entered.

Example:

If the numbers 2 4 7 4 2 1 8 6 9 7 10 3 are entered, the program will print 10 and 9.

Solution:

    #include <iostream>
    using namespace std;

    int main() {
        int n, max1, max2, temp;
        if (cin >> max1 >> max2) {
            if (max2 > max1){
                temp = max1;
                max1 = max2;
                max2 = temp;
            }
            while(cin >> n) {
                if(n > max1){
                    max2 = max1;
                    max1 = n;
                } else if (n > max2) {
                    max2 = n;
                }
            }
            cout << max1 << endl;
            cout << max2 << endl;
        } else {
            cout << "Enter at least 2 numbers";
        }
        return 0;
    }

1.10. Task 9

Write a program that will determine the difference between the sums of the numbers at even and odd positions (according to the order of entry) from N integers entered from the keyboard. If this difference is less than 10, the screen prints “The two sums are similar”, otherwise “The two sums differ greatly”.

Example:

For the numbers entered from the keyboard:

2 4 3 4 2 1 1 6 1 7

sum_odd_positions = 9

sum_even_positions = 22

The screen will print:

The two sums differ greatly

Solution:

    #include <iostream>
    #include <cmath>
    using namespace std;

    int main() {
        int difference, i, n = 0, number = 0;
        int sum_odd_positions = 0, sum_even_positions = 0;
        cin >> n;
        for (i = 1; i <= n; i++) {
            cin >> number;
            if (i % 2) {
                sum_odd_positions += number;
            } else {
                sum_even_positions += number;
            }
        }
        difference = sum_even_positions - sum_odd_positions;
        if (abs(difference) < 10){
            cout << "The two sums are similar";
        } else {
            cout << "The two sums differ greatly";
        }
        return 0;
    }

1.11. Task 10

Write a program that will determine the positions (ordinal numbers of entry) of the two consecutive numbers that have the highest sum from an unknown number of integers entered from the keyboard. The program ends if two negative integers are entered one after the other (consecutively).

Solution:

    #include <iostream>
    using namespace std;

    int main() {
        int pol_position, position, max_sum, sum, previous, next;
        cin >> previous >> next;
        pol_position = position = 2;
        max_sum = sum = previous + next;
        while (1) {
            if (previous < 0 && next < 0) {
                break;
            }
            sum = previous + next;
            if (sum > max_sum) {
                max_sum = sum;
                pol_position = position;
            }
            previous = next;
            cin >> next;
            position++;
        }
        if (position > 2)
            cout << "The numbers are at positions " << pol_position - 1 << " and " << pol_position << " and their sum is " << max_sum;
        return 0;
    }

2. An example of an exam problem for the first midterm.

2.1. Task 1

In Morse code, the characters (letters, digits) are represented by dots and dashes. The digits from 0 to 4 are represented as follows: “—–,” “.—-,” “..—,” “…–,” “….-.” You enter a natural number N (N < 100,000) from the keyboard, and then N other natural numbers are entered. For each of the entered numbers, print the remainder when dividing that number by 5 in Morse code, each in a separate line.

Solution:

    #include <iostream>

    using namespace std;
    int main() {
        int n, current_number;

        cin >> n;

        for (int i = 0; i < n; ++i) {
            cin >> current_number;
            int rest = current_number%5;
            if (rest == 0) {
                cout<<"-----"<<endl;
            } else if (rest == 1) {
                cout<<".----"<<endl;
            } else if (rest == 2) {
                cout<<"..---"<<endl;
            } else if (rest == 3) {
                cout<<"...--"<<endl;
            } else {
                cout<<"....-"<<endl;
            }
        }

        return 0;
    }

2.2. Task 2

A “Rosary” is a “number” obtained by concatenating the first N numbers in order. For example, for N=4, it is 1234, and for N=11, it is 1234567891011. An “Reverse Rosary” is similar to the above, but it starts from N, and the numbers are concatenated in reverse order. So, for N=5, it is 54321.

For a given N, print in order on the screen: Rosaryfor 1, Reverse Rosary for 1, Rosary for 2, Reverse Rosary for 2, and so on, up to Reverse Rosary for N.

For example, for N=5, you should print:

1
1
12
21
123
321
1234
4321
12345
54321

Solution:

    #include <iostream>

    using namespace std;

    int main() {
        int i, j, N;
        cin >> N;

        for (i = 1; i <= N; i++) {
            for (j = 1; j <= i; j++) {
                cout << j;
            }
            cout << endl;
            for (j = i; j > 0; j--) {
                cout << j;
            }
            cout << endl;
        }

        return 0;
    }

2.3. Task 3

The average number is a number where each subsequent digit is smaller than the previous one (when considering from the most significant digit to the least significant digit). For example, 7421. Single-digit numbers are not considered average numbers.

To write a program, first enter a number N, which determines the total number of numbers to be entered from the keyboard. Then, enter the N numbers one by one. As output, you should print all the numbers that meet the condition for an average number, and then print the minimum average number from the entered numbers.

If there is no number that satisfies the condition, the value -1 is printed.

Example input:

5
435
643
12234
721
7720

Example output:

643
721
643

Решение:

    #include <iostream>

    using namespace std;

    int main() {
        int num, i, tmp, min = -1, N;
        cin >> N;

        for (i = 0; i < N; i++) {
            cin >> num;
            tmp = num;
            while (tmp > 9) {
                if (tmp % 10 >= (tmp / 10) % 10) break;
                tmp /= 10;
            }
            if (tmp < 10) {
                cout << num << endl;
                if (min == -1) min = num;
                else if (num < min) min = num;
            }
        }

        cout << min;
        return 0;
    }

3. Control structure for branching (switch-case)

3.1. Example

#include <iostream>
using namespace std;

int main() {
  char answer;
  cout << "Is SP easy to learn? (y/n): ";
  cin >> answer;
  switch (answer) {
  case 'Y':
  case 'y':
    cout << "I think so too!" << endl;
    break;
  case 'N':
  case 'n':
    cout << "Really?" << endl;
    break;
  default:
    cout << "Is this a yes or no question?" << endl;
  }
  return 0;
}

3.2. Example for choosing from a text menu

#include <iostream>
using namespace std;

int main() {
    int value, choice;
    cout << "Enter the initial value: ";
    cin >> value;

    do {
        do {
            cout << "Menu:" << endl;
            cout << "1 - Increase" << endl;
            cout << "2 - Decrease" << endl;
            cout << "3 - Double" << endl;
            cout << "0 - Exit" << endl;
            cout << "Choice: ";
            cin >> choice;
        } while (choice < 0 || choice > 3);

        switch (choice) {
            case 1:
                value++;
                break;
            case 2:
                value--;
                break;
            case 3:
                value *= 2;
                break;
            case 0:
                cout << "End" << endl;
                break;
            default:
                cout << "Invalid choice!" << endl;
                break;
        }

        cout << "Value = " << value << endl;
    } while (choice != 0);

    return 0;
}

3.3. Exercise 1

Write a program that will determine whether a character entered from the keyboard is a vowel, a digit or something else.


#include <iostream>

using namespace std;

int main() {
  char znak;
  cout << "Enter a character: ";
  cin >> znak; 

  switch (znak) {
    case 'a':
    case 'A':
    case 'e':
    case 'E':
    case 'i':
    case 'I':
    case 'o':
    case 'O':
    case 'u':
    case 'U':
      cout << "The entered character is a vowel: " << znak << endl;
      break;
    case '0':
    case '1':
    case '2':
    case '3':
    case '4':
    case '5':
    case '6':
    case '7':
    case '8':
    case '9':
      cout << "The entered character is a digit: " << znak << endl;
      break;
    default:
      cout << "The entered character is: " << znak << endl;
  }

  return 0;
}

3.4. Exercise 2

Write a program that represents a simple calculator. The program reads two numbers and an operator in the format:

num1 operator num2

After performing the operation, depending on the operator, the result should be printed in the following format:

num1 operator num2 = result
#include <iostream>
using namespace std;

int main() {
    double num1, num2, result;
    char op;

    cout << "Enter two numbers and an operator (e.g., 5 + 3): ";
    cin >> num1 >> op >> num2;

    switch (op) {
        case '+':
            result = num1 + num2;
            break;
        case '-':
            result = num1 - num2;
            break;
        case '*':
            result = num1 * num2;
            break;
        case '/':
            if (num2 != 0) {
                result = num1 / num2;
            } else {
                cout << "Division by 0 is not allowed." << endl;
                return 1;
            }
            break;
        default:
            cout << "Unknown operator." << endl;
            return 1;
    }

    cout << num1 << ' ' << op << ' ' << num2 << " = " << result << endl;

    return 0;
}