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.

25 thoughts on “Tasks: C++ Functions

  1. int Kubiki();
    using namespace std;
    int main()
    {
    setlocale(LC_ALL, “rus”);
    SetConsoleCP(1251);
    SetConsoleOutputCP(1251);
    srand(time(NULL));

    you KTP, kub1;
    int = 50 0, P = 0;
    char c;
    ktp = rand() % 2;
    if (etc. == 0)
    {
    cout << "Вы бросаете первым! ";
    for (int i = 0; i < 4; i )
    {

    cout <> c;
    L + = Kubiki();
    L + = Kubiki();
    cout << "\nИтого у вас " << L << " points(_)";

    cout << "\n\nХод оппонента: ";
    cout <> c;
    P + = Kubiki();
    P + = Kubiki();
    cout << "\nИтого у противника " << P << " points(_)";
    }

    cout << "\nУ вас " << L <L)
    cout << "\nПК победил) Luck in the future!:)";
    if (P < L)
    cout << "\nВы победили, Поздравляем!";
    if (P == L)
    cout << "\nПобедила Дружба! Happens))";
    return 0;
    }

    else
    {
    cout << "Противник бросает первым! ";
    for (int i = 0; i < 4; i )
    {
    cout << "\n\nХод оппонента: ";
    cout <> c;
    P + = Kubiki();
    P + = Kubiki();
    cout << "\nИтого у противника " << P << " points(_)";

    cout <> c;
    L + = Kubiki();
    L + = Kubiki();
    cout << "\nИтого у вас " << L << " points(_)";

    }

    cout << "\nУ противника " << P < L)
    cout << "\nПК победил) Luck in the future!:)";
    if (P < L)
    cout << "\nВы победили, Поздравляем!";
    if (P == L)
    cout << "\nПобедила Дружба! Happens))";
    return 0;
    }

    }

    int Kubiki()
    {
    int kub1 = 1 + rand() % 6;
    switch (kub1)
    {
    case 1:
    cout << "\n@@@@@@@\n";
    cout << "@@@@@@@\n";
    cout << "@@@ @@@\n";
    cout << "@@@@@@@\n";
    cout << "@@@@@@@\n";
    cout << "1 Очко\n";

    break;
    case 2:
    cout << "\n@@@@@@@\n";
    cout << "@@@@@ @\n";
    cout << "@@@@@@@\n";
    cout << "@ @@@@@\n";
    cout << "@@@@@@@\n";
    cout << "2 Очкa\n";

    break;

    case 3:
    cout << "\n@@@@@@@\n";
    cout << "@@@@@ @\n";
    cout << "@@@ @@@\n";
    cout << "@ @@@@@\n";
    cout << "@@@@@@@\n";
    cout << "3 Очкa\n";
    break;

    case 4:
    cout << "\n@@@@@@@\n";
    cout << "@ @@@ @\n";
    cout << "@@@@@@@\n";
    cout << "@ @@@ @\n";
    cout << "@@@@@@@\n";
    cout << "4 Очкa\n";
    break;

    case 5:
    cout << "\n@@@@@@@\n";
    cout << "@ @@@ @\n";
    cout << "@@@ @@@\n";
    cout << "@ @@@ @\n";
    cout << "@@@@@@@\n";
    cout << "5 Oчей\n";
    break;

    case 6:
    cout << "\n@@@@@@@\n";
    cout << "@ @ @ @\n";
    cout << "@@@@@@@\n";
    cout << "@ @ @ @\n";
    cout << "@@@@@@@\n";
    cout << "6 Oчей\n";
    break;
    }
    return kub1;
    }

  2. Game of dice

    #include
    #include
    #include

    using namespace std;

    int ShowResult(int result) // output function cube symbol
    {
    switch (result)
    {
    case 1:
    cout << "\t\n" << result << " point(s):\n" <<"\t @ \n" << endl; //1
    break;
    case 2:
    cout << "\t\n" << result << " point(s):\n" << "\t@ @\n" << endl; //2
    break;
    case 3:
    cout << "\t\n" << result << " point(s):\n" << "\t @\n\t @\n\t @\n" << endl; //3
    break;
    case 4:
    cout << "\t\n" << result << " point(s):\n" << "\t@ @ \n\t@ @\n " << endl;//4
    break;
    case 5:
    cout << "\t\n" << result << " point(s):\n" << "\t@ @\n \t @\n \t@ @ \n" << endl;//5
    break;
    case 6:
    cout << "\t\n" << result << " point(s):\n" << "\t@ @ \n\t@ @\n\t@ @ \n" < sum_computer)
    cout << "Player wins" << endl;
    else if (sum_player < sum_computer)
    cout << "Computer wins" << endl;
    else
    cout << "Nobody wins, draw" << endl;
    }
    int main()
    {
    srand(time(NULL));

    char choice; // variable to enter the selection to play the game or not
    int first_walking; //variable to determine who goes first
    int result; // dice roll result
    int sum_player = 0; // credit meter
    int sum_computer = 0; // Computer glasses counter

    cout << "Welcome" << endl;
    line:
    cout << "Do you want to start playing?(enter y to srart)" <> choice;
    switch (choice) //
    {
    case 'and':
    cout << "Game starts" << endl;
    break;
    case 'n':
    goto line;
    default:
    cout << "Invalid choice" << endl;
    goto line;
    }
    cout << "Who will first roll the dice?" << endl;
    cout << "Computer determines" << endl;
    for (int i = 0; i < 5; i )
    {
    cout << "…" << endl;
    }

    first_walking = rand() % 2; // determining a first dice thrower
    if (first_walking == 1) // if the first player throws
    {
    cout << "You roll the dice first" << endl;

    for (int i = 0; i < 4; i )
    {
    char choice_dice;
    cout << "enter Y to roll the dice" <> choice_dice;
    if (choice_dice == 'Y')
    continue;
    for (int i = 0; i < 2; i )
    {
    result = 1 + rand() % 6;
    cout << "Result of roll the dice of player" << endl;
    cout << i + 1 << " cube:" << endl;
    ShowResult(result);
    sum_player += result;
    }

    for (int i = 0; i < 2; i )
    {
    result = 1 + rand() % 6;
    cout << "Result of roll the dice of computer" << endl;
    cout << i + 1 << " cube:" << endl;
    ShowResult(result);
    sum_computer += result;
    }
    cout << "Sum player = " << sum_player << endl;
    cout << "Sum computer = " << sum_computer << endl;
    cout << endl;
    }

    }
    else // if the first one throws the computer
    {
    cout << "Computer rolls the dice first" << endl;
    for (int i = 0; i < 4; i )
    {
    for (int i = 0; i < 2; i )
    {
    result = 1 + rand() % 6;
    cout << "Result of roll the dice of computer" << endl;
    cout << i + 1 << " cube:" << endl;
    ShowResult(result);
    sum_computer += result;
    }
    char choice_dice;
    cout << "enter Y to roll the dice" <> choice_dice;
    if (choice_dice == 'Y')
    continue;
    for (int i = 0; i < 2; i )
    {
    result = 1 + rand() % 6;
    cout << "Result of roll the dice of player" << endl;
    cout << i + 1 << " cube:" << endl;
    ShowResult(result);
    sum_player += result;
    }
    cout << "Sum computer = " << sum_computer << endl;
    cout << "Sum player = " << sum_player << endl;
    cout << endl;
    }

    }
    compare(sum_player, sum_computer);
    }

  3. #include
    #include
    #include

    using namespace std;

    int TwoCube (int arr[],int SIZE)
    {
    srand(time(NULL));
    for (int i = 0; i < SIZE; i )
    {
    arr[i] = rand() % 6 + 1;
    switch (arr[i])
    {
    case (1):
    cout << "@" << endl;
    break;
    case (2):
    cout << "@ @" << endl;
    break;
    case(3):
    cout << "@ @ @" << endl;
    break;
    case(4):
    cout << "@ @ @ @" << endl;
    break;
    case(5):
    cout << "@ @ @ @ @" << endl;
    break;
    case(6):
    cout << "@ @ @ @ @ @" << endl;
    break;
    }

    }
    cout << endl;
    return 0;
    }

    int i(int arr[], int SIZE)
    {
    int sum = 0;
    for (int i = 0; i < SIZE; i )
    {
    sum += arr[i];
    }
    return sum;
    }

    int main()
    {
    setlocale(LC_ALL, "rus");
    int const SIZE = 2;
    char choice;
    int arr[SIZE];
    int arr2[SIZE];
    int localSum1, localSum2;
    bool restart = true;

    do
    {

    cout << "Вы играете в кубики."<<endl;
    cout << "Бросьте кубики первыми" << endl;
    cout <> choice;
    if (choice == ‘y’)
    TwoCube(arr, SIZE);
    else
    cout << "Отказ не принимается, нажмите \"y\": ";
    } while (choice != 'y');
    cout << "Бросает компьютер, Wait…" << endl;
    Sleep(4000);
    cout << "Кубики ударились об стенку и выпало число…" << endl;
    Sleep(2000);
    TwoCube(arr2, SIZE);
    localSum1 = Sum(arr, SIZE);//your account
    localSum2 = Sum(arr2, SIZE);//the expense of the enemy
    cout << "Твой счёт: " << localSum1 << " | " << "Противника счёт:" << localSum2 << endl << endl;
    cout << "Бросьте кубик второй раз." << endl;
    cout <> choice;
    if (choice == ‘y’)
    TwoCube(arr, SIZE);
    else
    cout << "Отказ не принимается, нажмите \"y\": ";
    } while (choice != 'y');
    cout << "Бросает компьютер, Wait…" << endl;
    Sleep(4000);
    cout << "Подождите, swindles computer" << endl;
    Sleep(2000);
    TwoCube(arr2, SIZE);
    localSum1 += Sum(arr, SIZE);//your account
    localSum2 += Sum(arr2, SIZE);//the expense of the enemy
    cout << "Твой счёт: " << localSum1 << " | " << "Противника счёт:" << localSum2 << endl << endl;
    cout << "Бросьте кубик третий раз." << endl;
    cout <> choice;
    if (choice == ‘y’)
    TwoCube(arr, SIZE);
    else
    cout << "Отказ не принимается, нажмите \"y\": ";
    } while (choice != 'y');
    cout << "Бросает компьютер, Wait…" << endl;
    Sleep(4000);
    cout << "Компьютер злится и ждёт мести" << endl;
    Sleep(2000);
    TwoCube(arr2, SIZE);
    localSum1 += Sum(arr, SIZE);//your account
    localSum2 += Sum(arr2, SIZE);//the expense of the enemy
    cout << "Твой счёт: " << localSum1 << " | " << "Противника счёт:" << localSum2 << endl << endl;
    cout << "Бросьте кубик последний раз." << endl;
    cout <> choice;
    if (choice == ‘y’)
    {
    cout << "Кажется кубик куда-то закатился" << endl;
    Sleep (3000);
    TwoCube(arr, SIZE);
    }
    else
    cout << "Отказ не принимается, нажмите \"y\": ";
    } while (choice != 'y');
    cout << "Бросает компьютер, Wait…" << endl;
    Sleep(4000);
    cout << "Взламывает числа" < localSum2)
    {
    cout << "Мои поздравления, you have won over this stupid computer" << endl;
    Sleep(2000);
    }
    else
    {
    cout << "Ха-Ха-Ха, I won, I specially rigged your loss! Baie-has-been" << endl;
    Sleep(2000);
    cout << "Теперь смотри счёт" << endl;
    }

    cout << "Твой счёт: " << localSum1 << " | " << "Противника счёт:" << localSum2 << endl << endl;
    Sleep(1000);
    cout <> choice;
    if (choice == ‘y’)
    {
    cout << "Ура! Begin" << endl;
    restart = true;
    }
    else
    {
    cout <> choice;
    if (choice == ‘y’)
    {
    cout << "Я знал что ты передумаешь ;)";
    restart = true;
    }
    else
    {
    cout << "Счастливо!!!" << endl;
    restart = false;
    }
    }
    }while (restart);

    }

Leave a Reply

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