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.
View code
задача do while c++
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
usingnamespacestd;
intmain()
{
setlocale(LC_ALL,"rus");
intamountDigits=0;// количество введенных чисел
doubletotalSum=0;// общая сумма
doubledigit=0;//
cout<<"Введите числа, для расчета (выйти - 0)\n\n";
do
{
cout<<"Число "<<amountDigits+1<<" = ";
cin>>digit;
if(digit)// если digit любое значение кроме 0(false)
{
amountDigits++;
totalSum+=digit;
}
}while(digit);// пока digit любое значение отличное от 0
I’m the only one who doesn’t understand the working principle of whille??? working program,but not sure,what do while is needed. and especially for “upstarts” input “fool's protection” case 9: { /*. Необходимо суммировать все нечётные целые числа в диапазоне, который введёт пользователь с клавиатуры*/
double a = 0; double b = 0; int i = 0; double s = 0;
cout << a; cin >> b; do { if (a < b) { for (i = a; i <= b; i++) { if (i % 2 == 0) { continue; } s += i; cout << "нечетные числа из вашего диапазона\t" << i << endl;
}cout << "сумма этих чисел = \t" << s < b) { for (i = b; i <= a; i++) { if (i % 2 == 0) { continue; } s += i; cout << "нечетные числа из вашего диапазона\t" << i << endl; } cout << "сумма этих чисел = \t" << s << endl; }
break; } while (a == b); cout << "алльенЬ\n"; break; }
int a = 0, b = 0, i = -1; do { a = _getche() - 48; b = a + b; i++; } while (a != 0); if (b == 0) exit(0); cout << "\nСумма = " << b << endl << "Колличество введеных знаков = " << i << endl << "Среднее арефметическое = " << static_cast(b) / i << endl;
Regarding the third task. more or less figured out the cycles, which set the height of the shape and the number of spaces. But for the life of me I can’t figure out the cycle, which outputs the symbol, a for (int j = height – 2 * i; j <= height; j ). Please who- someone explain why the variable j is initialized to this particular value. Thanks in advance!
They forgot to mention in the problem statement, that the symbol output operators, spaces and line breaks can only be used once…that's when it gets interesting ;)
“Tasks: while loops, do while, nested loops” It is not clear why the third problem was solved using for(;;), doesn't this contradict the topic?? And how to frame the code?)
The for loop, while, do while – these are different forms the same thing – each of them can be rewritten through any other. So there's not much difference.
[quote]The for loop, while, do while are different forms of the same thing - each of them can be rewritten in terms of any other. So there's not much difference.[/quote] I understand this perfectly,however for for loops() there is a separate topic. That is. logically, or collect all the cycles into one topic, or pay attention to the disclosure of the announced topic.
how to frame the code? This is **text markup** by inserting BBCode tags. In order for the, to mark up **program code** you need to write something like this (only instead of square brackets [ ] write angular ), like this: [code] … this is where you write your code … [/code] (Do a search on BBCode for a more detailed explanation.)
cout << "Сумма не четных чисел: " << endl; while (a != b + 1) { if (a % 2 != 0) { sum += a; if (a == b – 1 || a == b) { cout << a; } else cout << a << " + "; } a++; } cout << " = " << sum << endl;
cout << "Сумма не четных чисел: " << endl; while (a != b + 1) { if (a % 2 != 0) { sum += a; if (a == b – 1 || a == b) { cout << a; } else cout << a << " + "; } a++; } cout << " = " << sum << endl;
If anyone is suddenly interested, an alternative solution to the second problem using a for loop. :)
#includeusing namespace std;
int main(){
setlocale(LC_ALL, "rus");
int debut;
int fin;
int number;
int sum = 0;
cout << debut;
cout << fin;
for(int i = (debut - 1); i < fin; ++i){
number = debut;
if (number % 2 != 0){
sum = sum + number;
debut = debut + 1;
}
else{
debut = debut + 1;
continue;
}
}
cout << "Сумма нечётных чисел в данном диапазоне: " << sum << endl;
}
I’m the only one who doesn’t understand the working principle of whille??? working program,but not sure,what do while is needed. and especially for “upstarts” input “fool's protection”
case 9:{
/*. Необходимо суммировать все нечётные целые числа в диапазоне,
который введёт пользователь с клавиатуры*/
double a = 0;
double b = 0;
int i = 0;
double s = 0;
cout << a;
cin >> b;
do
{
if (a < b)
{
for (i = a; i <= b; i++)
{
if (i % 2 == 0)
{
continue;
}
s += i;
cout << "нечетные числа из вашего диапазона\t" << i << endl;
}cout << "сумма этих чисел = \t" << s < b)
{
for (i = b; i <= a; i++)
{
if (i % 2 == 0)
{
continue;
}
s += i;
cout << "нечетные числа из вашего диапазона\t" << i << endl;
}
cout << "сумма этих чисел = \t" << s << endl;
}
break;
} while (a == b);
cout << "алльенЬ\n";
break; }
int main(){
setlocale(LC_ALL, "rus");
int a = 0, b = 0, i = -1;
do
{
a = _getche() - 48;
b = a + b;
i++;
} while (a != 0);
if (b == 0)
exit(0);
cout << "\nСумма = " << b << endl
<< "Колличество введеных знаков = " << i << endl
<< "Среднее арефметическое = " << static_cast(b) / i << endl;
system("pause");
return 0;
}
третья
int main(){
setlocale(LC_ALL, "rus");
cout << h;
do
{
if(h!=1)
cout << setfill(' ') << setw(h-1) << ' '
<< setfill('^') << setw(k) << '^' << endl;
else
{
cout << setfill('^') << setw(k) << '^' << endl;
}
--h; k += 2;
} while (h);
system("pause");
return 0;
}
Regarding the third task.
more or less figured out the cycles, which set the height of the shape and the number of spaces.
But for the life of me I can’t figure out the cycle, which outputs the symbol, a for (int j = height – 2 * i; j <= height; j ).
Please who- someone explain why the variable j is initialized to this particular value. Thanks in advance!
int main(){
setlocale(LC_ALL, "rus");
int height = 0; // высота треугольника
cout << height;
for (int i = 0; i = i; j--)
{
cout << "!";
}
for (int j =0; j <= i; j++)
{
cout << '^';
}
for (int j=1; j<= i; j++)
{
cout <<'^';
}
cout << endl;
}
return 0;
}
They forgot to mention in the problem statement, that the symbol output operators, spaces and line breaks can only be used once…that's when it gets interesting ;)
“Tasks: while loops, do while, nested loops”
It is not clear why the third problem was solved using for(;;), doesn't this contradict the topic??
And how to frame the code?)
The for loop, while, do while – these are different forms the same thing – each of them can be rewritten through any other.
So there's not much difference.
[quote]The for loop, while, do while are different forms of the same thing - each of them can be rewritten in terms of any other.
So there's not much difference.[/quote]
I understand this perfectly,however for for loops() there is a separate topic. That is. logically, or collect all the cycles into one topic, or pay attention to the disclosure of the announced topic.
how to frame the code?
This is **text markup** by inserting BBCode tags.
In order for the, to mark up **program code** you need to write something like this (only instead of square brackets [ ] write angular ), like this:
[code]
… this is where you write your code …
[/code]
(Do a search on BBCode for a more detailed explanation.)
Of course, message editor here horrible!
Thank you
[code with ++]
int main() {
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
int a, b;
int sum = 0;
cout << "Введите диапазон чисел" << endl;
cout <> a;
cout <> b;
cout << "Сумма не четных чисел: " << endl;
while (a != b + 1) {
if (a % 2 != 0) {
sum += a;
if (a == b – 1 || a == b) {
cout << a;
}
else cout << a << " + ";
}
a++;
}
cout << " = " << sum << endl;
return 0;
}
[/code]
[code]
int main() {
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
int a, b;
int sum = 0;
cout << "Введите диапазон чисел" << endl;
cout <> a;
cout <> b;
cout << "Сумма не четных чисел: " << endl;
while (a != b + 1) {
if (a % 2 != 0) {
sum += a;
if (a == b – 1 || a == b) {
cout << a;
}
else cout << a << " + ";
}
a++;
}
cout << " = " << sum << endl;
return 0;
}
[/code]