Looking for help on how to sum up the components of a vector/array. The basic outline is that I input a vector/array a of length N,and want the sum of the square of the components. I have defined a vector/array b which is just hte components of a squared, so all I need to do is sum it up. Only problem is, I don't know how! Help please.
Here is my program thus far:
#include <iostream>
using namespace std;
int main(void)
{
int N; int a[11]; int n; int b[11];
//Entering and printing N
cout<<"Enter the dimension of the vector, between 1 and 10 "<<endl;
cin>>N;
cout<<"N="<<N<<endl;
//Entering the vector components
cout<<"Enter the vector components for the vector of dimension size N, as specified above"<<endl;
Declare a variable "total", set it to 0, loop through the array/vector and add the current element to "total". You already have the looping code (twice, even!), you already have variable declarations (four, even!) and you already have an example of array-accessing and how to perform operations on them. This should be a piece of cake!
I'm very confused you don't understand, since you already used it...
1 2 3 4 5 6
//Summing the square of the vector components
for (n=1; n<N+1; n=n+1) // 'loops' from n = 1->N
{
b[n]=(a[n])*(a[n]); // 'accesses' the elements, and assigns one element a value
cout<<b[n]<<endl;
}
Just do the same, but instead, turn the 4th line into 'total += b[n];'.