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()
    {
    int chelovek;//буду вводить кол во человек
    int vozrast;// буду вводить возраст
    int ma=0;// максимум
    int mi=100;// минимум
    int sr=0;//подсчёт суммы
    int sr1=0;//среднее

    cout<<chelovek;

    for(int i=1;i<=chelovek;i++)
    {
    cout<<"vvedite vozrst heloveka "<< i<>vozrast;

    if (vozrast>ma)//если возраст больше максимума, отобразиться первое после ноля
    ma=vozrast;
    if (vozrast<mi)//если возраст меньше минимума, отобразиться первое число от 100, при условии что возраст не превышает 100
    mi=vozrast;
    sr+=vozrast;//подсчитываю сумму

    }
    sr1=sr/chelovek;//считаю среднее
    cout<<"Samiy starshiy "<<endl;
    cout<<ma<<endl;
    cout<<"Samiy mladshiy"<< endl<<mi<<endl;
    cout <<"sredniy vozrast"<<endl<<sr1<<endl;

    return 0;
    }

  2. Дополнительная задача задание 2.
    Есть вопросик.
    Очень хотелось ввести имя каждого посетителя и затем часы, но с именем никак не получается.
    #include
    using namespace std;
    int main()
    {
    int chelovek;//буду вводить кол во человек
    float chasi;//количество часов
    float sum=0;//объявляю сумму

    cout<<chelovek;

    for(int i=1;i<=chelovek;i++)
    {
    cout<< "posetitel "<< i <>chasi;

    sum=sum+chasi;

    }
    cout<<"obshaya summa chasov "<< sum;

    return 0;
    }

  3. немного переделала задачу 3.В ней кол-во ящиков не фиксировано.
    #include
    using namespace std;
    int main()
    {
    int apple=0;
    int vvod=0;
    int sum=0;
    int itog=0;
    int um=0;

    cout<< apple;

    for (int i=1; ;i++)
    {
    cout<<"vvedite kolihestvo dla mashinu "<<" "<< i<> vvod;
    sum=vvod+sum;

    if(sum>apple)
    {
    itog=sum-apple;
    cout<<"prosti drug,no ne vlazit "<< itog <<" yashikov ";
    break;
    }
    }
    return 0;
    }

  4. int stock = 5;
    int car = 0;

    for (;;)
    {
    CAR : cout << car;
    if (car > stock)
    {
    cout << "Данное количество нет на складе! \n";
    goto CAR;
    }
    stock -= car;
    cout << "Остаток: " << stock << endl;

    if (stock == 0)
    {
    cout << "Яблоки закончились! \n";
    break;
    }

    }

  5. int main()
    {
    setlocale(LC_ALL, "rus");

    int number = 0;
    int result = 0;

    WELCOME : cout << number;
    result = number * number;
    cout << result << endl;
    goto WELCOME;
    }

  6. int main()
    {
    setlocale(LC_ALL, "rus");

    int number = 0;
    int result = 0;
    bool choise = true;

    for (;;)
    {
    AGAIN : cout <> number;
    result = number * number;
    cout << "Ваш результат: " << result << endl;
    cout << choise;
    if (!(choise == 0))
    {
    break;
    }
    }
    }

  7. А я вот так 3ю задачу решил
    int main()
    {
    setlocale(0, "");
    int jas, vv, n=0;
    cout << jas;
    for (; jas != 0 ;)
    {
    cout << vv;
    if (vv > jas)
    {
    cout << "Так не пойдет, ящиков меньше!\n";
    cout << "Их осталось " << jas << "\n";
    }
    else
    {
    jas -= vv;
    n++;
    }
    }
    cout << "Молодец! Ты погрузил все ящики в " << n << " грузовиков\n";
    return 0;
    }

  8. People, объясните кто может.
    я не понимаю по какой логике построен цикл for (int i = 0; i < amount; i ) во второй задаче.
    I.e, чем является переменная "i" и почему "i < amount".

Leave a Reply

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