Help on vectors

I need write a program to perform vector addition but not use the STL vector class and not include extended input validation. The program is to read from cin and do the math. Here is the algorithm I have:

Read in size of vectors and validate
For each element i of vector1
Read in vector1[i]
Likewise read in elements of vector2 and vector3
For each component c of vector
Let result[c] be 0
For each vector v
Increase result[c] by v[c]
Output the result vector

Im unsure of how to even start. I dont know how to code based on the input of the user to determine the size of vectors and to keep reading the component of an unknown sized vector.
Last edited on
Firstly, you will need to know more about the vector than just the size of it. You will need to know the whole coordinate (x,y,z). From there you can just apply the additions you need.

If the vector is multi-dimensioned, meaning vector2, vector3, etc just read that value from the user.

Example:

1
2
3
4
5
6
7
8
9
10
How many vectors? ____ EX: 2   // get number of vectors
     How many dimesions is the vector? ___  EX: 3      // create an array of size [# of vecs][# of dims]
     V1 - Input the value 1: ___ EX: 2
     V1 - Input the value 2: ___ EX: 2    // get inputs dim values from the user corresponding to the vector indices
     V1 - Input the value 3: ___ EX: 2    // perform this n times, the number of vectors

     V2 - Input the value 1: ___ EX: 3
     V2 - Input the value 2: ___ EX: 3
     V2 - Input the value 3: ___ EX: 3
     Result: ___ EX: <5, 5, 5>              // perform addition 


My opinion would just be to create a 2D array in the beginning and just redefine it to new sizes.

Don't you miss school projects like this?

"I want you to build a house, but first I'm going to poke out one of your eyes and break all of your fingers. Good Luck this is %90 of your final grade making the rest of what you did in my class a complete waste of your time."
Not very understand the algorithm. To my understanding, vector is a dynamic one-dimesion array in C++ ( of cause, you can set a vector in static).

I think firstly, you need to define your data structure, but again I don't quite understand your algorithm. May I could give me suggestion if you could explain it more.
From the professor:
the program should 1) input the length of the vectors (i.e., actual array size); 2) make sure the length is valid; 3) input three lists of numbers of the same length; and 4) compute the pair-wise addition of each number in the list (e.g., result[0] = list1[0] + list2[0] + list3[0]). The output is the resulting array with the sums. (C++ array declarations use a constant size: hence actual vs. maximum distinction.)

In our class we have only hit on using arrays to implement lists. The output should look like this:

Enter the size of the vectors [1-100]: 5

Enter the vector data [3x5 real values]
1 2.5 3.1 4 5
5 4 3.2 2.5 1.1
3 3.33 3.3 10 1.1
Result vector (addition):
9 9.83 9.6 16.5 7.2

Last edited on
Ok, then I misinterpreted the problem. I was thinking of adding math vectors. Does that not make sense? I don't see why you cannot do what I described to solve your problem.

Just create a 2D array

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
float vectorData = [number of vectors][size of each vector]

cout << "<";

float result[size of each vector];
for(int i = 0; i < size of each vector; ++i)
{
   for(int j = 0; j < number of vectors; ++j)
   {
         result[i] += vectorData[i][j]
   }
      cout << result[i]
}

cout << ">"


closed account (D80DSL3A)
If you can't use the STL then you need to dynamically allocate arrays for this.
Have you learned about using new and delete?
Here's the framework of what's involved:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

int main( )
{
	int size = 0;
	cout << "Vector length = "; cin >> size;// validate size!!

	// allocate memory for 4 arrays with size elements
	double* V1 = new double[size];// example

	// read in the element values	
	// add the elements together
	// display the result	

	// release the memory
	delete [] V1;// example
      
	return 0;
}

If you just need to find and display the vector sum you could do this with just 1 array. Start with all elements = 0 and increment each element as each of the 3 values are entered.
Hope that gets you started.
The most challenge thing is may you have to read the data out from the input.

1. read "1 2.5 3.1 4 5" as a string by using getline(cin, sample) //string sample;

2, then need to split them into sub string. You can use istringstream to split them as following:

istringstream iss(sample)
while(iss){
string sub;
iss >> sub;
}

3, you need change substrinng "sub" to double by using atof().

Last edited on
Topic archived. No new replies allowed.