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. because they need, without it we will not be able to run an infinite loop, break will not work, but it is not exactly, I think so

    2. You launch an infinite loop. It looks like this:
      for(;;){
      // block code
      }
      In principle we can and while(){} cycle of use, so even a few lines will be less. Just then this topic.

  1. The first problem was solved peacefully and immediately, but on second thought long, then I opened the code and still could not understand. If someone turned out to be smarter than please explain the second problem.

  2. #include
    using namespace std;

    int main(int argc, char** argv)
    {
    int x, y = 0;
    cout <> x;
    do
    {
    static int i = 1;
    cout << "How many boxes put in " << i <> Y;
    if ((x-y) < 0)
    {
    cout << "WTF!?!? Boxes are out, put less boxesnOn warehouse " << x << " boxes\n";
    }
    else
    {
    x = x – Y;
    ++i;
    }
    }
    while(x != 0);
    return 0;
    }

  3. Why do we have to assign to each variable “0”? Solving the second problem is not assigning variable value 0 heresy was obtained when calculating the average number of , why is this happening ?

  4. my code to third objective
    #include “stdafx.h”
    #include

    using namespace std;

    int main()
    {
    setlocale(LC_ALL, “ru”);
    int a = 15, n, sum = 0, count = 1;

    for (;;)
    {
    cout << "сколько ящиков загрузить в " << count <> n;
    sum += n;
    count ;

    if (n > a)
    cout << "введённая сумма больше кол-во яблок!" << endl;

    if (sum == a)
    {
    cout << "ящики закончились!" < a)
    {
    cout << "ошибка! you used more boxes than there. Number of boxes = " << a << endl;
    break;
    }

    }

    return 0;
    }

  5. The problem with drawers
    #include

    using namespace std;

    int main()
    {
    int nBox = 0;//Boxes user input
    int nMaxBox = 15;//Number of boxes in stock

    for (; nMaxBox != 0;)
    {
    cout << "Max box to warehouse is " << nMaxBox << "."<< endl;
    cout <> nBox;
    if (nBox <= nMaxBox)
    {
    nMaxBox = nMaxBox – nBox;

    } else {cout << "To many box. The max box is " << nMaxBox << ". Pleas, entered agane." << endl;}
    }
    cout << "\n The warehouse is empty." << endl;
    return 0;
    }

  6. The third task easier:

    #include
    using namespace std;
    int main()
    {
    setlocale(LC_ALL, “rus”);
    int yash=15;
    int pogr;
    int sum;
    cout<<"На складе "<<yash<<" ящиков.\n";
    for(int i=1;sum<yash;i )
    {
    cout<<"Приехала "<<i<>heated;
    sum + = pogr;
    if(sum>15)
    {
    cout<<"У нас нет столько ящиков.\n";
    sum = sum-pogr;
    i=i-1;
    }
    }
    cout<<"\nВсё, boxes ran, let's go home!";

    return 0;

    }

Leave a Reply

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