The do … while loop in the following program is supposed to read some numbers until it reaches a sentinel (in this case, -1). It is suppose to add the double of each of the numbers except for the sentinel. If the data looks like:
12 5 30 48 -1
the program does not add the numbers correctly. Correct the program by rewriting as a while loop so that it adds the double of the numbers correctly.
#include using namespace std;int main(){int total = 0, count =0, number;do{cin >> number;total = total + (number * 2);count ++;}while (number != -1);cout < “the="" number="" of="" the="" data="" read="" is”="">< count=""><>cout < “the="" sum="" of="" the="" numbers="" entered="" is="" “=""><><>return 0;}
using namespace std;
int main()
{
int total = 0, count =0, number;
do
cin >> number;
total = total + (number * 2);
count ++;
}
while (number != -1);
cout < “the="" number="" of="" the="" data="" read="" is”="">< count=""><>
cout < “the="" sum="" of="" the="" numbers="" entered="" is="" “=""><>
<>
return 0;
Already registered? Login
Not Account? Sign up
Enter your email address to reset your password
Back to Login? Click here