The basics of programming in c++ for beginners

Tasks: C++ Functions

Continuing to acquaint you withфункциями в C offerindependently  several tasks. Расположены они по уровню сложности.

1. Объявить два целочисленных array  with different sizes and write function, that fills their elements and values ​​shown on the screen. Функция должна принимать два параметра – массив и его размер.

2. Необходимо создать двумерный массив 5 х 5. Далее написать функцию, which fills its random numbers from 30 to 60. Создать еще две функции, которые находят максимальный и минимальный элементы этого двумерного массива. (ABOUTгенерации случайных чисел a separate article)

3. Написать игру в которой имитируется бросание кубиков компьютером и пользователем. В игре 2 dice and each of them may fall from 1 to 6 очков. Реализовать определение программой первого ходящего. Каждый делает по четыре броска.  After the shots show, нарисованные символами кубики и количество очков, выпавших на них. After a couple of shots (бросок компьютера + бросок пользователя) display the intermediate result– the number of the player and the computer score.  After announce, who won on the basis of all the shots.

37 thoughts on “Tasks: C++ Functions

  1. #include
    #include

    using namespace std;

    int who_first();
    int throw_cube(int cube1, int cube2);

    int main()
    {
    setlocale(LC_ALL, “ru”);

    bool var;

    int cube1 = 0;
    int cube2 = 0;

    int pc_points = 0;
    int player_points = 0;

    int pc_total = 0;
    int player_total = 0;

    cout <> name;
    cout << endl;

    var = who_first();

    for (int i = 0; i < 8; i )
    {
    if (var)
    {
    player_points = throw_cube(cube1, cube2);

    player_total += player_points;

    cout << "Ходит " << name << endl;
    cout << " << name << " dropped out " << player_points << " очков" << endl;
    cout << "Всего у " << name << " " << player_total << " очков" << endl;
    cout <<"===================================================="<< endl << endl;
    }

    else
    {
    int pc_points = throw_cube(cube1, cube2);

    pc_total += pc_points;

    cout << "Ходит компьютер" << endl;
    cout << "У компьютера выпало " << pc_points << " очков" << endl;
    cout << "Всего у компьютера " << pc_total << " очков" << endl<< endl;
    cout << "====================================================" << endl << endl;
    }

    var = !var;
    }

    if (pc_total == player_total) cout << "Победила дружба! " < player_total) cout << "Победа за компьютером :-( " << endl;
    else if (pc_total < player_total) cout << "Победил(and) " << name << "!" << endl;
    }

    int who_first()
    {
    srand(time(0));
    int result = rand() % 2 – 1;

    return result;
    }

    int throw_cube(int cube1, int cube2)
    {
    cube1 = rand() % 6 + 1;
    cube2 = edge() % 6 + 1;

    int result = cube1 + cube2;
    return result;
    }

  2. No subtotals.
    #include
    #include
    #include
    #include
    using namespace std;
    int PCscore, manscore;
    int cube();
    int cubePC();

    int main() {
    SetConsoleOutputCP(1251);
    SetConsoleCP(1251);
    srand(time(NULL));

    string x;
    cout << "КУБИКИ – LET'S PLAY?" << endl;
    for (int i = 0; i < 3; i ) {
    cout << "Бросить кубики?" <> x;
    if (x == “yes”)
    {
    cube();
    }
    else {
    cout << "Завершение работы…";
    return 0;
    }
    cout << "Ход компьютера:" << endl;
    cubePC();
    }
    cout << "Компьютер – " << PCscore << endl
    << "Игрок – " << manscore < manscore) cout << "Вы проиграли" << endl;
    else cout << "Вы выиграли" << endl;
    }

    int cube() {
    SetConsoleOutputCP(866);
    SetConsoleCP(1251);
    int man1 = 1 + rand() % 5, man2 = 1 + rand() % 5;
    cout << (char)201 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)187
    << "\t" << (char)201 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)187 << endl
    << (char)186 << " " << man1 << " " << char(186)
    << "\t" << (char)186 << " " << man2 << " " << char(186) << endl
    << (char)200 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)188
    << "\t" << (char)200 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)188 << endl;
    SetConsoleOutputCP(1251);
    SetConsoleCP(1251);
    return manscore += (man1 + man2);
    }
    int cubePC() {
    SetConsoleOutputCP(866);
    SetConsoleCP(1251);
    int PC1 = 1 + rand() % 5, PC2 = 1 + rand() % 5;
    cout << (char)201 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)187
    << "\t" << (char)201 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)187 << endl
    << (char)186 << " " << PC1 << " " << char(186)
    << "\t" << (char)186 << " " << PC2 << " " << char(186) << endl
    << (char)200 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)188
    << "\t" << (char)200 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)188 << endl;
    SetConsoleOutputCP(1251);
    SetConsoleCP(1251);
    return PCscore += (PC1 + PC2);
    }

  3. Task No. 3
    #include
    #include
    #include
    #include
    using namespace std;

    void ThrowCube(int &NumOnCube)
    {
    NumOnCube = rand() % 6 + 1;
    switch (NumOnCube)
    {
    case 1:
    cout << " _________" << endl;
    cout << "/ \\" << endl;
    cout << "| |" << endl;
    cout << "| * |" << endl;
    cout << "| |" << endl;
    cout << "\\_________/" << endl;
    break;
    case 2:
    cout << " _________" << endl;
    cout << "/ \\" << endl;
    cout << "| * |" << endl;
    cout << "| |" << endl;
    cout << "| * |" << endl;
    cout << "\\_________/" << endl;
    break;
    case 3:
    cout << " _________" << endl;
    cout << "/ \\" << endl;
    cout << "| * |" << endl;
    cout << "| * |" << endl;
    cout << "| * |" << endl;
    cout << "\\_________/" << endl;
    break;
    case 4:
    cout << " _________" << endl;
    cout << "/ \\" << endl;
    cout << "| * * |" << endl;
    cout << "| |" << endl;
    cout << "| * * |" << endl;
    cout << "\\_________/" << endl;
    break;
    case 5:
    cout << " _________" << endl;
    cout << "/ \\" << endl;
    cout << "| * |" << endl;
    cout << "| * * * |" << endl;
    cout << "| * |" << endl;
    cout << "\\_________/" << endl;
    break;
    case 6:
    cout << " _________" << endl;
    cout << "/ \\" << endl;
    cout << "| * * |" << endl;
    cout << "| * * |" << endl;
    cout << "| * * |" << endl;
    cout << "\\_________/" << endl;
    break;
    }
    }

    int main()
    {

    setlocale(LC_ALL, "rus");
    srand(time(NULL));
    bool WhoTurn = rand() % 2;
    const int round = 4;
    int PlayerCube1 = 0;
    int PlayerCube2 = 0;
    int PlayerScore = 0;

    int BotCube1 = 0;
    int BotCube2 = 0;
    int BotScore = 0;
    int stats[2][round] = { };
    for (int i = 0; i < round * 2; i )
    {
    if (WhoTurn == 1)
    {
    cout << "Ваш ход!" << endl;
    cout << "Бросить кубики(press any key)" << endl;
    _getch();
    ThrowCube(PlayerCube1);
    ThrowCube(PlayerCube2);
    cout << "\nСумма цифр на кубиках: " << PlayerCube1 + PlayerCube2 << "\n" << endl;
    stats[0][i/2] = PlayerCube1 + PlayerCube2;
    PlayerScore += stats[0][i/2];
    WhoTurn = 0;
    }

    else
    {
    cout << "Ход компьютера!" << endl;
    cout << "Компьютер бросает кубики" << endl;
    Sleep(500);
    ThrowCube(BotCube1);
    ThrowCube(BotCube2);
    cout << "\nСумма цифр на кубиках: " << BotCube1 + BotCube2 <<"\n" << endl;
    stats[1][i/2] = BotCube1 + BotCube2;
    BotScore += stats[1][i/2];
    WhoTurn = 1;
    }
    if ((i+1) % 2 == 0 && i != round * 2 – 1)
    {
    cout << "\n=================================" << endl;
    cout << " Player account tComputer account" << endl;
    cout << " " << PlayerScore << "\t " << BotScore << endl;
    cout << "=================================" < BotScore)
    {
    cout << "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@" << endl;
    cout << "@ Вы победили!!! Congratulations! @" << endl;
    cout << "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@" < PlayerScore)
    {
    cout << "#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^#" << endl;
    cout << "# Компьютер победил!!! Lucky next time! #" << endl;
    cout << "#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^#" << endl;
    }
    else
    {
    cout << " \t tDraw!" << endl;
    }
    cout << "==================================" << endl;
    cout << " Player account | Computer account" << endl;
    cout << " " << PlayerScore << " |" << "\t " << BotScore << endl;
    cout << " |" << endl;
    for (int i = 0; i < round; i )
    {
    cout << i+1 << " throw: " << stats[0][i] << " | " << " " << stats[1][i] << endl;
    }
    cout << "==================================" << endl;
    return 0;
    }

  4. When writing a program no. 3 noticed one thing: if unknowingly declare srand(time(NULL)) in the body of the program more than once, then the result of the throw between cycles will not change. Threw away 3 and 5 – so it will be until the end of the program to throw 3 and 5, until the program is restarted. And so over and over again. I fell for it. The error is not displayed on the debug screen in Visual Studio.

  5. Declare two integer arrays with different sizes and write a function,
    that fills their elements and values ​​shown on the screen.
    The function must take two parameters - an array and its size.

    #include
    using namespace std;

    const int ARRSIZE_1 = 4;
    const int ARRSIZE_2 = 5;

    void FuncArr(int arr[], int b)
    {
    for (int i = 0; i < b; i )
    {
    arr[i] = rand();
    }
    for (int i = 0; i < b; i )
    {
    cout << arr[i]<< "\t";
    }
    cout << endl;
    }
    int main()
    {
    setlocale(LC_ALL, "RU");
    int arrOne[ARRSIZE_1], arrTwo[ARRSIZE_2];
    cout << "Элементы первого массива : " << endl;
    FuncArr(arrOne, ARRSIZE_1);
    cout << "Элементы второго массива : " << endl;
    FuncArr(arrTwo, ARRSIZE_2);
    }

  6. You need to create a two-dimensional array 5 х 5.
    Далее написать функцию, which fills its random numbers from 30 to 60.
    Создать еще две функции, которые находят максимальный и минимальный элементы этого двумерного массива.
    (There is a separate article about random number generation.)

    #include
    #include
    using namespace std;

    const int ROW = 5;
    const int COL = 5;
    int arrOne[ROW][COL];

    void FuncAddArrNumber()
    {
    srand(time(NULL));
    for (int i = 0; i < ROW; i )
    {
    for (int j = 0; j < COL; j )
    {
    arrOne[i][j] = rand()%30+30;
    }
    }
    for (int i = 0; i < ROW; i )
    {
    for (int j = 0; j < COL; j )
    {
    cout << arrOne[i][j] << "\t";
    }
    cout << endl;
    }
    }
    void FuncMaxMinElement()
    {
    int maxElement = arrOne[0][0];
    int minElement = arrOne[0][0];
    for (int i = 0; i < ROW; i )
    {
    for (int j = 0; j < COL; j )
    {
    if (maxElement arrOne[i][j])
    {
    minElement = arrOne[i][j];
    }
    }
    }
    cout << "Максимальный элемент массива : " << maxElement << endl << "Минимальный элемент массива : " << minElement << endl;
    }
    int main()
    {
    setlocale(LC_ALL, "RU");
    FuncAddArrNumber();
    FuncMaxMinElement();
    }

  7. Near the counter of the store there is a queue with n customers. The service time of the i-th customer ti by the seller has a normal distribution with mathematical expectation 5 min and standard deviation 2 min.
    Develop an algorithm and make a simulation program to obtain the values ​​of ti , values ​​ci - waiting time for service by the i-th customer, service start time, end time for each customer. Find Average Service Wait Time.
    Present the result of the program in the form of a table, which consists of calculated values.

Leave a Reply to Timothy Cancel reply

Your email address will not be published. Required fields are marked *