The basics of programming in c++ for beginners

Tasks: while loops, do while, nested loops

We met with loops while and do while in C++ and c nested constructions in loops. Let us tasks solving.

1. Organize continuous input of numbers with the keyboard, until the user has entered 0. After entering a zero, show on the screen the number of numbers, which were introduced, their total amount and the arithmetic mean. Tip: you must declare the counter variable, that will count the number of entered numbers, and variable, that will accumulate a total sum of numbers.

2. It is necessary to sum up all the odd integers, which will introduce the user to the keyboard.

3. The task is more difficult. Draw an isosceles triangle of characters ^. The height of a user selects. For example: height = 5, on the screen

task nested loops

177 thoughts on “Tasks: while loops, do while, nested loops

  1. #include
    #include
    using namespace std;

    int main()
    {
    int h;
    cout << "Enter height: " <> h;
    for (int i = 1; i <= h; i )
    {
    cout << setw(h – i+1);
    for (int l = 1; l <= i; l++)
    {
    if (l == 1)
    cout << "^";
    else
    cout << "^^";
    }
    cout << endl;
    }
    system("pause");
    }

  2. good afternoon!
    I can not figure out the last problem.

    #include
    using namespace std;
    int main()
    {
    setlocale(LC_ALL, “rus”);
    int height = 0; // triangle height
    cout <> height;
    for (int i = 0; i < height; i )
    {

    After that I cease to understand, then what happens.
    In general, with the cycles in your lessons, I like as understood, theory, associated with nested loops I, too, everything is clear, Although the problems with characters still appeared a couple of questions.
    first 2 the problem I decided to fold the third and with the help of your solutions, but I can not behold the third task to cope even with your decision, because it is not clear to me.

    Already I started to think about, to look for any other lessons, where everything is explained in more detail, but still I decided to leave a comment, because it is not a fact, I find these lessons, but to me your lessons have been well understood until the last threads, related cycles, it would be a shame just to score directly on these lessons because of a problem, that I myself was not able to solve.
    Hope, explain to me how it is done, and it seems to be the essence of the theme itself is understandable: loop, in which there is a cycle. Only in practice, it looks much more complicated, well, or I just find simple things too complicated.

    1. More simpler explanation you just do not find – It could not be easier (so the explanation here with simplifications, default, Consequently, inaccuracies).
      If you do not understand, it is not clear not, which is associated with C ++, otherwise, which is associated with the general logic of the calculation, algoritmirovaniem. Here on this part (logic) and note.

    2. Nested loops are simple enough in theory,, but there are also tricky in practice. Describe exactly what is unclear? Tut troynaya matrёshka: topmost – big loop output line, depending on the height of the figure, two nested loops in it “sit” side by side on two chairs, Only the team performed sequentially: first the first nested outputs desired number of spaces, then a second number of symbols necessary nested – then the next line…..and so on down to the final height of the figures.

    3. For those, who in the tank, I try to chew (for himself, too =) ):

      for (int i = 0; i > height the height of the triangle * /
      {
      for (int j = 1; j < height – i; j ) /*Loop, которым мы рисуем пробелы перед символом "^". Performed (height – i – 1) call in different. That is, for each pass of the outer loop blank line length is reduced by i (variable i is incremented by one for each pass of the outer loop, so the number of times execution of this small cycle is reduced by 1, respectively, an empty field length before the character on the line, too, every time is reduced).*/
      {
      cout << ' ';
      }

      for (int j = height – 2 * i; j <= height; j ) /*второй маленький цикл пишет символы "^"в кол-ве height – (height – 2 * i) + 1. It seems a little confusing at first glance,. If the outer loop prints the first row (i=0), the number of characters will be equal: height – height + 2*i + 1 = 1 + i*2;

      Seen from the equation, that the use of variable height here does not need it – she vzaimounichtozhaetsya for solving the equation. In any of its value does not affect the number of cycles performed. This is not a programming error, but, what is called, "быдлокод" – ugly solution to the problem with a completely unnecessary variable, deluding the reader.

      Therefore, it may seem difficult to understand such a complicated structure, ведь в ней совершенно не обязательно использование "высоты треугольника". Below I will propose his solution, without the participation of variable height, because instead noo can put any value at all, even zero, though -242148: the result will be the same! */
      {
      cout << '^';
      }
      cout << endl;
      }

      Here's how I solved this problem without the participation of variable height in the second nested loop (a change in only one cycle, everything else is left as it was):

      #include
      using namespace std;

      int main()
      {
      setlocale(LC_ALL, “rus”);

      int height = 0; // triangle height

      cout <> height;

      for (int i = 0; i < height; i )
      {
      for (int j = 1; j < height – i; j )
      {
      cout << ' ';
      }

      for (int j = 0; j < i * 2 + 1; j ) /* кол-во символов "^" a line defined by the equation: (Number of characters) = i * 2 + 1. As you can see, used only one variable from the outer loop. In this way, number of characters will grow on 2 each pass of the outer loop. It's simple! */
      {
      cout << '^';
      }
      cout << endl;
      }
      return 0;
      }

      Hope, my comment to someone will help you understand this task. All the best! P.S. I myself am still learning, Shoes do not rush =)))

  3. I make sure that I drew, just confused with this? I still do not understand all the actions, which are located in the outer loop.

    1. “I do not understand all the actions” – all this does not apply to the C ++ language, and furthermore, It does not apply to general programming.
      This is the area of ​​analytical thinking, mathematical method of perception, logical constructions … – taught this in other places and other courses.

  4. Create a list of a sequence of positive integers r1, r2, r3, … rn , are entered from the keyboard. Output data of first upside, and then in input order. From the resulting list to delete all the numbers, fold 3 and output the resulting list.

    1. Create a list of a sequence of positive integers r1, r2, r3, … rn , are entered from the keyboard. Output data of first upside, and then in input order. From the resulting list to delete all the numbers, fold 3 and output the resulting spisok.Pomogite me pzhsta

  5. Author, Thank you for your hard work!

    In the second nested loop you namudrili with variable height in the condition cycle. It is not needed, and extra trash, as they say, head fools novices. I myself did not realize, she was there extra. Here are my concise solution with only two instead of three variables:

    for (int j = 0; j < i * 2 + 1; j ) /* кол-во символов "^" a line defined by the equation: (Number of characters) = i * 2 + 1. As you can see, used only one variable from the outer loop. In this way, number of characters will grow on 2 each pass of the outer loop. It's simple! */

  6. Lomalla Bosco time 2. (Only mastering the C ++). Tight logic. The answer is not spying. The result was, but more than the author gromozche.
    By the way, author, what do you think?

    1. strangely, reason code on the site becomes another, not the, I threw off. where cin? Where prompted to enter your height.

      1. 1. Editor's comment spoils code.
        2. It is not a time to write here: Do not write your code in a comment – nobody needs.
        3. No one will comment your code, even if he was not distorted.

Leave a Reply

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