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
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.
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; }
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)
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.”
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)
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.
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;
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; }
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; }
You have no input at all there, type cin >> number …
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.
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.
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).
norms )
Everything that was sent by you using Orphus – corrected. Thank you, Alexey! )
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.
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 )
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)
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.”
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)
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.
(((((((((( hmm, so the interest in programming has disappeared..
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; }
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) 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;
}
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;
}
You have no input at all there, type cin >> number …
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.
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.
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…