Hello again!
For today’s quiz we had to write a code which could ask the user for undeterminated number of values and calculate the sum of the squares.
For example:
Let’s suppose that the user will introduce this values to the program:
1, 2, 3, 4, 5.
Now, what the program will do is the following:
1^1+2^2+3^3+4^4+5^5 and it will print 55
When we want to stop giving values to our program it is indicated that the user must have to type 0.
So, everything will look like this:
//Sum the Squares of an undeterminated number of values #include <iostream> #include <vector> using namespace std; int sumsquares_list (vector<int> v){ int len = v.size(); int sum = 0; for(int i = 0; i < len; i++) { sum= sum + (v[i]*v[i]); } return sum; } int main() { int c; vector<int>v; cout<<"Please introduce a serie of values and write 0 to finish."<<endl;//0 will be the signal to stop asking for values cin >> c; while(c != 0) { v.push_back(c); cin >> c; } cout<<""<<endl; cout<<" The sum of the squares of the numbers that you typed in is: "<<sumsquares_list(v)<<endl; return 0; }
And the execution of the program will look just like this:
And, if you want to see my codes on GitHub, remember, just clic HERE.
Have an awesome week!
-The Admin
Quiz #8! by esaupreciado is licensed under a Creative Commons Attribution 4.0 International License.