reading data into a vector

So im having a bit of trouble figuring out the second part of this code. This first part just creates a txt file and populates it. BTW this code was provided, i did not write it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
int n = 10;
ofstream outFile( "vectors.txt", ios::out);
//exit program if unable to create file
if(!outFile)
{
cerr << "File could not be opened" << endl;
exit( 1 );
}

//write out the size of vector
outFile << "Size: " << n << endl;

//create vector1 with N random elements
outFile << "Vector1: " ;
for (int i = 0 ; i < n ; i++) outFile << rand()%n << ' ';
outFile << endl;

//create vector2 with N random elements
outFile << "Vector2: " ;
for (int i = 0 ; i < n ; i++) outFile << rand()%n << ' ';
outFile << endl;
return 0;
}


its the second part im having a hard time figuring out. We're supposed to read that information, and store it in a vector? this second part was also provided, i just have to fill in the blanks.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int dotproduct(int *Vector1,int *Vector2,int N);
int main()
{
string info;
int n, *vector1, *vector2;
//create input file to read the file vectors.txt
ifstream inFile("vectors.txt",ios::in);
if (!inFile)
{
cerr << "File could not be openned" << endl;
exit(1);
}
//read vector size from the input file and print out to screen
inFile >> info; cout << info;
inFile >> n;
cout << n << endl;
//add your code to allocate vector 1 & vector 2 of size N
// read vector 1 from the vectors.txt file
inFile >> info ; cout << info;
for (int i = 0 ; i < n ; i++)
{
inFile >> vector1[i] ; cout << vector1[i] << ' ';
}
cout << endl;
//add your code to read vector 2 from the vectors.txt file
//add your code to compute dot product and display the result
//add your code to deallocate vector 1 & vector 2
return 0;
}
int dotproduct(int *Vector1,int *Vector2,int N)
{
//add your code to compute and return the dot product
}


it doesn't even look like we're using vectors, just pointer of int type named vectors. Im just super confused. can someone point me in the right direction? how do i allocate a vector? everytime i try standard syntax, I get an error stating that i've previously declared vector1/2 as an int pointer. so if i cant allocate, how do i DEallocate. I just dont get whats going on.
You aren't using a std::vector, just a dynamic array.

http://www.cplusplus.com/doc/tutorial/dynamic/
Topic archived. No new replies allowed.