C++ program that shows A typical use of counters and accumulators would involve
ID: 3795517 • Letter: C
Question
C++ program that shows A typical use of counters and accumulators would involve a check-writing program where counters, count the number of checks, and accumulators, accumulate the total value of the checks and display the sum. For this assignment, you will be using Visual Studio software on your XenDesktop to develop a C++ program that sums the values that the user enters. The user will begin by telling the program how many values they will enter, and then the user will input those values. After the initial prompt that will be asking for the number of values, you MUST use a for loop to obtain those values and accumulate them.
Most of the code is below. You need to fill in the code the for loop.
// Total a sequence of integers.
#include
using namespace std;
int main()
{
int total = 0; // current total
int number = 0; // number of values
int value = 0; // current value
// display prompt
cout cin >> number; // input number of values
// loop number times
// Code in the for loop goes here. The user will enter values as the for loop iterates.
{
} // end for
// display total
cout
} // end main
Explanation / Answer
#include <iostream>
using namespace std;
#include<conio.h>
int main()
{
int total = 0; // current total
int number = 0; // number of values
int value = 0; // current value
int count;
cout << "Enter the number of values "; // display the numbert of values
cin >> number;
if (number <= 0) // it check if wring number is entered
{
cout << " Wrong Number is entered. Program exit";// display the message
}
else
{
for (count = 0; count < number; count++) { // Loop starts
cout << " Enter the value";
cout << count + 1;
cout << " ";
cin >> value; // take the value
total = total + value; // sum the total
}
cout << " Sum is ";
cout << total << endl ; // Display the total
}
_getch(); // read the input from keyboard to exit
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.