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.

23 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);
    }

Leave a Reply

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