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

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

  1. Not offset, for retake :) – one familiar teacher would say so, seeing such decisions.

    Division by zero error in the first program. It is enough to enter zero as the first number.

    Trifle, but in the first problem you double check for equality to zero. MB is better to exit the loop immediately? – type
    while (true) {
    if (0 == val)
    break;
    sum += val;
    ++on one;
    }

    In the second task, it is enough to enter the end of the range less than the beginning of the range and the program will loop. Lacks “foolproof”. I think it's logical to ask for numbers in the range (5, 1), as (1, 5).

    On the third task, everything is ok. At least I don't see jambs.

  2. prompt , can not understand
    how exactly the condition is satisfied , in the first task , the difference between the entered number and zero? after all, no condition is set for interrupting the cycle when entering zero
    confused)

    while (true) {
    if (0 == val)
    break;
    sum += val;
    ++num;
    }

    this code is clearer )

    1. if (digit) – before executing the statement, the if statement checks the condition, if it is true, then the instruction is executed. The condition can be written as
      (digit != 0) but not necessarily. In case of if (digit) for any non-zero value of digit, the condition will be true, but if digit is zero (0 automatically returns false), when checking the condition, false will be returned and the instruction will not be executed.
      For example, it seems to me that in the second task, in 21 if line (i % 2 != 0) can be written simply as if (i % 2)

      1. Thank you, what was the answer to the newbie's question.
        About, how best to write if (i % 2 != 0) or if (i % 2) – programmers' opinions differ. According to latest coding conventions – it is better to use explicit conditions. I quote: “In boolean expressions («if», «for», «while», "Do" and the first operand of the ternary operator “?”) always write down equality and inequality explicitly.”

  3. I'm a beginner myself – second month since I started reading a book on C ++))) I'm very interested in the question (I didn’t find it on the forum website, so I’ll ask here): if you study the materials, provided on the site and solve all these problems, can you consider yourself a programmer (at least partly)? is it possible then to make an initial resume and try to look for a job?
    If not, then what else you need to learn and know for this?
    Thank you)

    1. What are you? ))) this is just the very beginning. My site still needs, least, in 100 articles. + many tasks. So far there are only very simple tasks on my site.. And in order to consider yourself a programmer at least partially – you need to learn a couple of languages.

  4. The solution in the third task turned out to be completely different.
    #include
    using namespace std;

    int main()
    {
    setlocale(LC_ALL, "rus");

    int iHeight(0), iConstructing(0), iCounting(0), iSymbols(1);

    cout < < "Введите высоту треугольника:"; cin >> iHeight;
    iConstructing = iHeight;

    do
    {
    iCounting = 0;
    while (iCounting < = (iConstructing - 1)) { cout << " "; iCounting++; } iCounting = 0; while (iCounting < iSymbols) { cout << "^"; iCounting++; } cout << endl; iSymbols += 2; iConstructing--; } while (iConstructing); return 0; }

  5. do you think this option is acceptable for solving the first problem?
    int main()
    {
    int number;
    int sum;
    int i=0;

    float aveRage;

    do
    {

    cout << "Please enter the " << i + 1 <> number;
    cout << "Number you entered is " << number << endl;

    i ++;
    sum += number;
    aveRage = sum / i++;

    }
    while (number != 0);

    cout << "The total number of digits that were inserted is " << i ++ << endl;
    cout << "The total amount of insereted digits summed together is " <<sum - 1 << endl;
    cout << "The average of these numbers is " << aveRage << endl;

    return 0;
    }

    1. 1) In your loop, I do not see the input of values ​​in number;
      2) Calculation of the average can be carried out per cycle.
      In total, we squeeze to:

      int main(){
      int i,sum=0,number=0;

      for(i=1;number;i++,sum+=number) {
      cout<>number;
      }

      cout<<"The total number of digits that were inserted is "<<i<<endl
      << "The total amount of insereted digits summed together is "<<sum<<endl
      <<"The average of these numbers is " << sum/i<< endl;
      cin.get();
      return 0;
      }

      1. Though not. If with do while then
        int main(){
        int i=0,sum=0,number=0;

        do{
        i++;
        cout<> number;
        sum+=number;
        } while(number);

        cout<<"The total number of digits that were inserted is "<<i<<endl
        << "The total amount of insereted digits summed together is "<<sum<<endl
        <<"The average of these numbers is " << sum/i<< endl;
        cin.get();
        return 0;
        }

      1. He was there, The site ate it for some reason, and substituted the expression
        number;
        I have the same in post. Apparently some special characters worked.

    2. Actually, you should almost never ask “option is acceptable for solution?”.
      Need to compile and run. If true for all conceivable input data sets – then this is an acceptable solution, whatever is written.

  6. Much more interesting (and has a wide practical application) task number 1 if the condition is slightly changed:

    > After entering a zero, show on the screen the number of numbers, which were introduced, their total, arithmetic mean and standard deviation from the mean (or variance … diffusion).

    Everything becomes much more fun…

Leave a Reply

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