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
    #include
    using namespace std;

    int main()
    {
    setlocale(LC_ALL, "rus");
    int x = 0;
    int y = 0;
    int min = 1000;
    int max = 0;
    int sum = 0;
    int srd = 0;

    cout <> x;
    int z = x;
    for (x; (x > 0); x--)

    {
    cout <> y;
    sum = sum + y;
    if (min > y)
    min = y;

    if (max < y)
    max = y;
    }
    srd = sum / z;
    cout << "Максимальный возраст - " << max << endl;
    cout << "Минимальный возраст - " << min << endl;
    cout << "Средний возраст - " << srd << endl;
    _getch();
    return 0;
    }

  2. but the third task didn’t work out

    #include
    #include
    using namespace std;

    int main()
    {
    setlocale(LC_ALL, "rus");
    int y = 0;
    int x = 0;
    cout << x;
    for ( ;;)
    {
    cout << y;
    x = x - y;
    if (x = 0)
    {
    break;
    }
    if (x < 0)
    {
    x = x + y;
    cout << "Введено недопустимо большое значение. Начните ввод для крайней машины заного. \n ";
    continue;
    }

    else
    {
    x = x - y;
    cout << "У вас осталось " << x << " ящиков \n";

    }

    }
    _getch();
    return 0;
    }

    what's the mistake?

    1. There is an error in your code at this point if (x = 0). I.e., in any case, x will not be negative, and will be constantly equal 0.

  3. This is how I got problem N3:
    ________________________
    #include
    #include
    using namespace std;
    int main ()
    {
    int a,h,b,c,k,p,v,g,n;
    a=0;//ящикoв
    setlocale(LC_ALL, "rus");
    b=0;//машини
    n=0;//ящики
    v=0;//лишились
    k=0;
    g=0;
    h=0;
    cout <<"Введите количество ящиков"<>a;
    cout <<"Введите количество машин"<>b;

    for (c=1;c<=b;c++)
    {
    :check_1
    cout <<"Сколько ящиков поставить в "<<c<<"-ю машину?"<>n;
    g+=n;//
    if (a<g)
    {
    g-=n;
    for (;;)
    {
    cout <<"Недостаточно ящиков"<<endl;
    cout <<"Сколько ящиков поставить в "<<c<<"-ю машину?"<>p;
    g+=p;//
    }
    if (a>g)
    {
    :check_1
    }
    else
    g-=p;
    goto check;
    }
    }
    }
    if (g<a)
    {
    cout <<"Ошибка!Нехватка машин!У вас осталось "<<a-g<<" ящиков"<<endl;
    }
    else
    cout <<"15 ящиков распределено по машинах"<<endl;

    system ("pause");
    return 0;
    }

    1. > This is how I got problem N3:

      Seeing this without markings – it's all the same, if only I could write all the code in one line. The code even in such a record will be compiled and executed, but that doesn't mean, that someone will read it.

    2. > This is how I got problem N3:

      Would you at least compile the code?, ran it through the computer, before showing it – there will be just a lot syntactic errors. Therefore, talk about “it worked” – won't suit you ;-).

  4. Why not write something like this? : int minAge = 100;
    Can this be done :
    int minAge = 0
    if (age < minAge || age == 0)

    Otherwise it’s not known, maybe life expectancy will increase in the future..

    1. > Can this be done :

      Can…
      But pay attention to this, what always when selected in sequence (array, list, etc.) element according to some criterion: min, max, … any – есть 2 different approaches:

      1. take as the initial value a value that is clearly not suitable for this (then, what is in your example … 100, 0, …);

      2. take as initial value first sequence element, and it's too much to lead, starting from next, 2-it elements.

      1. .. then set the condition for the second element..

        Well, whose brains are twisted. In general, this is the attractiveness of this business..

  5. I somehow managed the 3rd task differently

    #include
    using namespace std;

    int main()
    {
    you nNumBoxPerCar, nNumBox=0, i=1, nOstatokBox=15;

    for ( ;nNumBox < 15 ; i )
    {
    cout << "Сколько ящиков загрузить в машину " << i <> nNumBoxPerCar;
    nNumBox = nNumBox + nNumBoxPerCar;
    if (nNumBox >= 15)
    {
    nOstatokBox = (15 – nNumBox) + nNumBoxPerCar;
    break;
    }
    }
    cout << "Ящики закончились. Moreover, we managed to load the last machine: "
    << nOstatokBox << "\n";
    }

  6. I solved the first problem differently.
    using namespace std;
    int main()
    {
    setlocale(LC_ALL, “rus”);
    int number = 0;
    bool Continue = 0;
    for (int i = 0; i < 1; i )
    {
    cout <> number;
    cout << endl;
    cout << "Квадрат числа " << number << " = " << number*number << endl;
    cout <> Continue;
    if (Continue == 1)
    i–;

    }

    system(“pause”);
    return 0;
    }

  7. task №3
    awry, but the main thing is the meaning and the result…

    #include
    using namespace std;
    int main()
    {
    int i=0,a,b;
    char x;
    cout<<a;
    for(;;)
    {
    i+=1;
    cout<<i<<b;
    if (a>b)
    {
    a-=b;
    cout<<"Na sklade ostatok = "<<a<<endl;
    }
    else if (a<b)
    {
    cout<<"Na sklade otalos = "<<a<<endl;

    cout<<"zagruzat - "<<a<<"jachikov?";

    cout<<x;
    if (x=='y')
    {
    cout<<"zagruzili ostatok - "<<a;
    break;
    }
    else if (x=='n')
    {
    cout<<"izvenite za neudobstvo DOSVIDANIE";
    }
    }
    else if (a==b)
    {
    cout<<"Na sklade ostalos = 0"<<endl;
    break;
    }
    }
    return 0;
    }

Leave a Reply

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