The basics of programming in c++ for beginners

Tasks: A for loop in c ++

Once you begin to tasks,  therefore already know what for loop. Let's look at a few of the tasks, solution in which it is applied, and, thereby, strengthen the knowledge gained. Programming practice– the best way to deal with the material and store information for a long time.

1. Write a program, that will show on the screen the number of the square,  entered by the user. The user has to decide for himself –  exit the program or continue writing. (Tip – You must run an infinite loop, which provide for termination of his, upon the occurrence of certain conditions).

2. In the gym every day comes a certain number of visitors. It should prompt the user to enter such data: how many people visited the gym for the day, enter the age of each visitor and ultimately show the age of the oldest and the youngest of them, as well as to calculate the average age of visitors.

For the job yourself, We offer you to solve a similar task. Arrange the number of visitors entering the gym and the number of hours spent by each of them in the gym. As a result, calculate and display the total amount, which customers have paid for training.

 3. In stock has a certain number of boxes of apples (in this example, 15). When the car arrives for pickup, ask the user to enter, how many boxes loaded into the first car,  the second and so on, until there are no more boxes of apples. Provide case, When the user enters the number of boxes more, than there is in stock.

If you have questions, please contact us in the comments.

161 thoughts on “Tasks: A for loop in c ++

  1. #include
    using namespace std;

    int main()
    {
    setlocale(LC_ALL, “RU”);
    int peopleDay;
    cout << "Введите количество посетителей : " <> peopleDay;
    int sum = 0;
    int* arrPeopleAge = new int[peopleDay]; //creating a dynamic array

    for (int i = 0; i < peopleDay; i )
    {
    cout << "Введите возраст посетителя" <> arrPeopleAge[i];
    sum = sum + arrPeopleAge[i];
    }

    int maxAge = arrPeopleAge[0];
    int minAge = arrPeopleAge[0];

    for (int i = 0; i < peopleDay; i )
    {
    if (maxAge arrPeopleAge[i])
    {
    minAge = arrPeopleAge[i];
    }
    }

    cout << "Максимальный возраст посетителя : " << maxAge << endl;
    cout << "Минимальный возраст посетителя : " << minAge << endl;
    cout << "Средний возраст посетителя : " << sum/peopleDay << endl;

    delete[] arrPeopleAge; // deleting a dynamic array

    }

  2. int main() {
    setlocale(0, “”);

    int visitors;
    int age;
    int min_age = 999;
    int max_age = 0;
    int average_age = 0;
    int time;
    double money_per_hour = 0;
    int general_money = 0;

    std::cout << "Введите стоимость одного часа занятий в спортзале" <> money_per_hour;
    std::cout << "Введите кол-во посетителей" <> visitors;

    for (int i = 0; i < visitors; i ) {
    std::cout << "Введите возраст посетителя №" << i + 1 <> age;
    if (age max_age) {
    max_age = age;
    }
    average_age = (average_age + age) ;
    }
    for (int i = 0; i < visitors; i ) {
    std::cout << "Введите время проведенное в зале посетителем №" << i + 1 <> time;
    general_money = general_money + time * money_per_hour;
    }
    std::cout << "Минимальный возраст посетителя – " << min_age << ", and the maximum – " << max_age << std::endl;
    std::cout << "Средний возраст равен " << average_age / visitors << std::endl;
    std::cout << "Общий доход за день составляет " << general_money << std::endl;
    return 0;
    }

  3. A task 1 через do while

    #include
    using namespace std;

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

    int num1{ 0 };
    char p = 'y';

    do
    { cout <> num1;
    cin.ignore(32767, ‘n’);
    cout << "Квадрат числа " << num1 << " is " << num1*num1;
    cout <> p;
    } while (p == ‘y’ || p == ‘Y’);

    cin.get();
    return 0;
    }

  4. #include
    using namespace std;

    int main()
    {
    setlocale(LC_ALL, “Russian”);
    int number = 1;;
    for (;;) {
    if (number == 1) {
    cout <> number;
    cout << "Квадрат числа " << number << " is: ";
    number = number * number;
    cout << number << "\nХотите продолжить? (1 – to continue, any other number – to stop." <> number;
    if (number >= 2 || number < 0) {
    break;
    }
    }
    }
    return 0;
    }

  5. This is the kind of shit I did)
    #include
    #include
    using namespace std;

    int main()
    {
    setlocale(LC_ALL, “russian”);
    int i;
    int ch;
    cout <> i;
    for (;;) // запуск бесконечного цикла
    {
    cout << "Число, squared : " << pow(i, 2) << endl;
    if (i == i++ and i == i–)
    break;
    }
    for (;;) {
    cout <> ch;
    if (ch > 0)
    break;
    if (ch == 0) {
    cout << "Здорово, let's continue!" << endl;
    for (;;) // запуск бесконечного цикла
    {
    cout <> i;
    cout << "Число, squared : " << pow(i, 2) < 0) {
    cout << "Охуенно, till!";
    }
    }

  6. #include

    using namespace std;

    int main()
    {
    setlocale(0, “”);
    int x;
    cout <> x;
    int *a=new int[x];//number of visitors
    int max;
    int min;//age
    float sred;
    floatsumm=0;
    int j = 1;//serial number

    for (int i = 0; i < x; i )
    {
    cout << "введите возраст посетителя № " << j <> a[i];
    j ;
    }
    j = 1;//serial number
    for (int i = 0; i < x; i )
    {
    cout <<"посетитель № "<<j<<", age: "<<a[i] << "\n";
    j ;
    }
    max = a[0];
    for (int i = 0; i < x; i )
    {
    if (max < a[i])
    {
    max = a[i];
    }

    }
    cout << "максимальный возраст: " << max << "\n";
    min = a[0];
    for (int i = 0; i a[i])
    {
    min = a[i];
    }
    }
    cout << "минимальный возраст: " << min << "\n";
    for (int i = 0; i < x; i )
    {
    summ += a[i];
    }
    sred = summ / x;
    cout << "средний возраст: " << sred << "\n";
    }

  7. #include
    #include

    using namespace std;

    bool checkInput(int temp)
    {
    if (cin.fail())
    {
    cin.clear();
    cin.ignore(32767, ‘n’);
    cout << "Invalid sign.Try again.\n";
    return false;
    }
    if (temp < 0)
    {
    cout << "This integer can't be negative.Try again.\n";
    return false;
    }
    return true;
    }

    int main()
    {
    int number, maxAge, minAge;
    wrongNumber:
    cout <> number;
    if (!checkInput(number))
    {
    goto wrongNumber;
    }
    cin.clear();
    cin.ignore(32767, ‘n’);
    vector age(number);
    maxAge = 0;
    minAge = 1000;
    if (number == 0)
    {
    cout << "The gym was empty today.\n";
    return 0;
    }
    for (int i = 0; i < number; i )
    {
    wrongAge:
    cout << "The age of " << i + 1 <> age[i];
    if (!checkInput(age[i]))
    {
    goto wrongAge;
    }
    cin.clear();
    cin.ignore(32767, ‘n’);
    if (maxAge age[i])
    minAge = age[i];
    }

    cout << "So:\nThe oldest person is of age " << maxAge << "\nThe youngest person is of age " << minAge << endl;
    float ArithmeticMean = 0;
    for (int i = 0; i < age.size(); i )
    {
    ArithmeticMean += age[i];
    }
    ArithmeticMean /= number;
    cout << "Arithmetic mean is " << ArithmeticMean << endl;
    }

    1. 2-ое задание,но с расчетом на ошибки пользователя при вводе данных

  8. #include
    using namespace std;

    int main()
    {
    setlocale(LC_ALL, “ru”);
    int STOP;
    STOP = 0;
    int number_1 = 0;
    cout <> number_1;
    for(;;)
    if (number_1 == STOP)
    {
    cout << "session ended"<< endl;
    break;
    }
    else
    {
    cout << number_1 * number_1 << "" << "\n" << "Enter STOPsesions closed or number to continue" <> number_1;
    }

Leave a Reply

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