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 argc, const char * argv[]) {
    cout <> qt_box;
    for (;;)
    {
    qt_car++;
    cout << "Введите количество ящиков для погрузки в машину №" << qt_car <> temp_box;
    box = box + temp_box;
    if (box > qt_box)
    {
    cout << "Столько ящиков на складе нет! Осталось " << qt_box – (box – temp_box) << " boxes. \n";
    qt_car–;
    box = box – temp_box;
    }
    else if (box == qt_box)
    break;
    }
    cout << "Все ящики погружены \n";
    return 0;
    }

  2. The second problem was solved with arrays ( I had to do to get into the lead a few lessons)

    1. Because you have garbage written in the loop termination condition (i != 0 – never break), and the increment (there you do not know what is written).

    2. Generally, to your specific code to work, necessary I ++, replaced by i ++, and instead of i = 0, i = -1;

      Although I can not imagine, how it can be useful.

  3. In the second problem is no protection of the division by 0. it is necessary to add a condition if the number of visitors is greater than zero, then carry out all the necessary actions, if not, write something that visitors were not – to invest in advertising))

Leave a Reply

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