I am trying to display num + num + num = sum for example:
Enter size: 3
first number 1
second number 2
third number 3
The sum of: 1 + 2 + 3 = 6. <- trying to do that.
so far my code is:
#include <iostream>
using namespace std;
void main() {
int size;
cout << "Enter amount of size: ";
cin >> size;
int count = 0;
int sum = 0;
int x;
while(count < size){
count+=1;
cout << +
"Enter number: ";
cin >> x;
sum+= x;
}
cout << "The sum is = " << sum;
}
im just trying to make it show num + num + num..etc = sum at the last line. Maybe I don't need to use an array then, what other way i could set it up to show the listed integers?
#include <iostream>
usingnamespace std;
int main()
{
int a = 0;
int b = 0;
int c = 0;
cout << "Please enter your first value: ";
cin >> a;
cout << "Please enter your second value: ";
cin >> b;
cout << "Please enter your last value: ";
cin >> c;
cout << "\n" << a << " + " << b << " + " << c << " = " << ( a + b + c );
cout << endl;
return 0;
}
#include<iostream>
#include<vector>
#include<algorithm>
int main( int argc, char ** argv)
{
std::vector<int> v;
std::cout << "enter number of values\n" ;
int num(0);
int val(0);
std::cin >> num ;
for ( int i = 0 ; i < num ; ++i)
{
std::cout << "enter a value: ";
std::cin >> val ;
v.push_back(val);
}
for( auto i = v.begin(); i != v.end() - 1; ++ i )
std::cout << *i << '+';
std::cout << v.back() << " = "
<< std::accumulate(v.begin(), v.end(), 0 ) << std::endl;
return 0;
}
Yeah, so in order for it to be an indefinite size -- you would have to use some sort of "changing" or "dynamic" (if you will) data structure. Because, something has to store all the numbers that have been input, so you can output them at the end of the program: num + num + num + num = sum
1) Ask user to input a number
2) User inputs number
3) Store that number in a [dynamic] array.
4) Ask user for another number
5) User inputs number
6) Store that number in a [dynamic] array.
7) Ask user for another number
8) User Inputs number
9) Store that number in a [dynamic] array.
.
.
.
10) Output each element of the array seperated by a " + " (Loop)
11) Sum all the elements of the array (Loop)
12) output the sum