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. normalek children, I decided. And by the way the author was like a bug in the problem, type of stock is left – 65 boxes (and so on. that is, a negative number), I have no bugs))
    #include
    using namespace std;

    int main ()
    {
    int a /* the amount shipped */, b = 15 / * number of stock * /;
    cout << "На складе " << b <> a;
    for (int i / * Number of vehicles * / = 1; (a > 0) && (a <= b) ; i )
    {
    if (a == 15)
    {
    cout << "На складе не осталось ящиков";
    exit (0);
    }
    b -= a;
    cout << "На складе " << b << " boxes, how to ship " << i <> a;
    if (a == b)
    {
    cout << "На складе не осталось ящиков";
    exit (0);
    }

    }
    cout << "На складе нет столько ящиков";
    }

  2. it seems to me too much superfluous code…
    3 the task:

    #include “pch.h”
    #include

    using namespace std;

    int main()
    {
    system(“chcp 1251”);
    system(“cls”);

    cout << "На складе 15 boxes" << endl;
    int i=0 , max = 15, on one, sum = 0, sum1 = 0, sum2 = 0;
    for (;;) {
    cout << "Сколько погрузить в " << i + 1 << " car?" <> on one;
    if (on one <= max – sum) sum = sum + on one;
    else {
    cout << "Не больше чем : " << max – sum << endl;
    cout << "Сколько погрузить в " << i + 1 << " car?" <> on one;
    if (whether the max) cout << "Максимум 15!" << endl;
    sum1 = sum – max;
    sum2 = max – sum;
    if (sum < max) cout << " There are only " << sum2 << " boxes!" < max) {
    cout << " There are only " << sum1 << " boxes!" << endl;
    I - num;
    cout << " How immersed in " << i << " car?" <> on one;
    }
    i = 1;
    if (on one > max) cout << "Максимум 15!" << endl;
    else if (sum == max)
    break;
    }
    cout << "Вы погрузили все ящики!" << endl;
    cin.get();
    return 0;
    }

  3. I do somewhere went wrong place)))
    // ship boxes of apples. If not enough boxes will ship all that remains
    #include
    using namespace std;

    int main()
    {
    setlocale(LC_ALL, “rus”);
    int box = 654; // the number of boxes of apples
    int unbox = 0; // as you need to unload
    int mach = 0; // number of cars
    int i = 0;
    cout <> mach; // enter the number of cars
    for (;;) // infinite loading loop (while eating apples). If not enough boxes hammered last car to dump
    {
    cout << "Есть " << box << " boxes of apples! " << "Сколько загрузить ящиков в машину № " << i + 1 <> unbox; // We determine the number of boxes to be loaded
    i ; // We move on to the next machine
    if ((box – unbox) < 0) // boxes is not enough? ship that left and complete program
    {
    cout << "Нет столько ящиков! you have requested " << unbox << ", left " << box << "!" << "\nВсе оставшиеся ящики погружены в машину № " << i << " Программа завершила работу!\n";
    break;
    }
    box -= unbox; // ship boxes
    mach -= 1; // send machine
    if (mach == 0) // If the machine is not completing the cycle
    break;
    }
    cout << "Погрузка завершена! Until! Until!\n";
    system("PAUSE");
    return 0;
    }

  4. self-reference

    #include “pch.h”
    #include

    using namespace std;

    int main() {

    setlocale(0, “”);

    int visitors;
    int time;
    int allTime = 0;
    int priceInHour = 50;
    int sum;

    cout <> visitors;
    cout << endl;
    int i;
    for (i = 0; i < visitors; i ) {
    cout << "Введитеr количество часов которое провел в зале " << i + 1 <> time;
    cout << endl;
    allTime += time;

    }

    sum = priceInHour * allTime;
    cout << "Общая сумма, which visitors paid for training = " << sum << endl << endl;

    return 0;
    }

  5. You have the second job, where “Show code” below, error, written – “For the job yourself …”, there should be “For self-assignment…”. Very Good, thank you for your efforts :)

  6. #include “pch.h”
    #include
    #include
    #include

    using namespace std;

    int main() {
    setlocale(LC_ALL, “rus”);
    int arr[7] = { 0, 0, 0, 100, 0, 0, 0 };
    cout << "Enter the number of visitors of the gym " <> arr[0]; //number posttiteley
    // arr3 youngest, arr4 oldest, arr5 average age, arr6 the total amount for the calculation environment…
    for ( arr[1] = 0; arr[1] < arr[0]; arr[1]++ ) {
    cout << " Enter the age " << arr[1] + 1 << " each visitor :" <> arr[2]; //the age of each visitor
    if (arr[4] arr[2]) {
    arr[3] = arr[2];
    }

    arr[6] += arr[2];
    }
    arr[5] = arr[6] / arr[0];
    cout << "=========================================================" << endl;
    cout << " The most adult visitor – " << arr[4] << " years old " << endl;
    cout << " Youngest visitor – " << arr[3] << " years old " << endl;
    cout << " Average age of visitors " << arr[5] << " years" << endl;
    }

  7. #include
    using namespace std;

    int main(){
    setlocale(LC_ALL, “rus”);
    int a = 15;//the total number of boxes in stock
    int b = 0;//The number of mailboxes in submerged car
    for(int x = 1; ; x++){
    a-= b;
    if(a==0){
    cout<<"Ящиков на складе не осталось";
    break;
    }
    cout<<"Сколько ящиков загрузить в "<<x<<"-ю машину (ящиков на складе "<<a<>b;
    while(1){
    if(b>a){
    cout<<"На складе нет столько ящиков, повторите ввод"<<endl;
    cout<<"Сколько ящиков загрузить в "<<x<<"-ю машину (ящиков на складе "<<a<>b;
    }
    else {break;}
    }
    }
    return 0;
    }

Leave a Reply

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