2d Vector help

Good day to you all, I have a the following program to do:

Write a program that will have two user defined functions. One function will allow the user to
enter integer values and store them in a two dimensional vector of size 3 x 3. The user must be
prompted to enter values using the following output statement “Enter value for Row R
Column C. R and C will display the actual row and column number. This function must return
the vector. The other function will accept a two dimensional vector and output the values stored
in it.

This is my code:

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream> 
#include <vector>  // Required Library                                
using namespace std;

    // vector function for storing values in it:
    vector < int > StoreVector () {
	
	// Required Variables:
	const int Row = 3;
	const int Column = 3;
	int R = 0;
	int C = 0;
	
	// 2d Vector:
	vector < vector < int > > Vec2d ( Row, vector < int > (Column) ); // same as int a [Row] [Column];	
	
	// Initial Output
	cout << "\tYou will be requested to enter 9 values in total for a 3x3 Matrix.";
	cout << endl;
	
	// Loop that request input for 9 values:
	for ( int a = 0; a < Row; a++ ) {
		
		for ( int b = 0; b < Column; b++ ) {
		
		// Requesting data from user:
		cout <<"\tEnter value for Row R Column C: ";
		cin >> Vec2d [a] [b];
	}	
    } cout << endl;
  
    // Ouputting matrix values for better searching for the user:
		for ( int i = 0; i < Row; i ++ ) {
		
		for ( int j = 0; j < Column; j ++ ) {
			
			cout <<  Vec2d [j] [j] << "\t ";			
		}
		
		cout << endl;		
	} cout << endl;
	
    // Requesting data from user that will display the value sotred on the given Rown & Column position:
    cout << "\tEnter a value for Row and Column followed by a space: ";
    cin >> R >> C;
    
    // Final Output:
    cout << "\tThe Value Stored on Row " << R << ", and Column " << C << " Is: " << Vec2d [R] [C];
 
	
	return Vec2d [R] [C]; // Returning Array  
}

    // This vector will receive a vector and output its values:
    vector < int > printVector ( vector < vector <int> > values ( int, vector < int > (int) ) ) {
	
	// Display the numbers in the vector:
	for ( int x = 0; x < values.size(); x++ ) {
		
		for ( int y = 0; y < values.size(); y++ ) {
		
		cout << values [x] [y] << " "; // Outputting
	}	
}
}

int main () {
	
	StoreVector();
	
	vector <int> GetData (StoreVector());
	
	printVector(GetData); // Function Call
	
	return 0;
}
                                


I have problems when returning the 2d vector and a bunch other errors that i dont know what they are:

C:\Users\KingShaq\Documents\Programming I\Fall 2018\Assignment 1 q 5.cpp In function 'std::vector<int> StoreVector()':

51 21 C:\Users\KingShaq\Documents\Programming I\Fall 2018\Assignment 1 q 5.cpp [Error] could not convert '(& Vec2d.std::vector<_Tp, _Alloc>::operator[]<std::vector<int>, std::allocator<std::vector<int> > >(((std::vector<std::vector<int> >::size_type)R)))->std::vector<_Tp, _Alloc>::operator[]<int, std::allocator<int> >(((std::vector<int>::size_type)C))' from 'int' to 'std::vector<int>'

C:\Users\KingShaq\Documents\Programming I\Fall 2018\Assignment 1 q 5.cpp In function 'std::vector<int> printVector(std::vector<std::vector<int> > (*)(int, std::vector<int> (*)(int)))':

58 30 C:\Users\KingShaq\Documents\Programming I\Fall 2018\Assignment 1 q 5.cpp [Error] request for member 'size' in 'values', which is of non-class type 'std::vector<std::vector<int> > (*)(int, std::vector<int> (*)(int))'

60 31 C:\Users\KingShaq\Documents\Programming I\Fall 2018\Assignment 1 q 5.cpp [Error] request for member 'size' in 'values', which is of non-class type 'std::vector<std::vector<int> > (*)(int, std::vector<int> (*)(int))'

62 20 C:\Users\KingShaq\Documents\Programming I\Fall 2018\Assignment 1 q 5.cpp [Warning] pointer to a function used in arithmetic [-Wpointer-arith]

62 20 C:\Users\KingShaq\Documents\Programming I\Fall 2018\Assignment 1 q 5.cpp [Warning] pointer to a function used in arithmetic [-Wpointer-arith]

C:\Users\KingShaq\Documents\Programming I\Fall 2018\Assignment 1 q 5.cpp In function 'int main()':

73 21 C:\Users\KingShaq\Documents\Programming I\Fall 2018\Assignment 1 q 5.cpp [Error] cannot convert 'std::vector<int>' to 'std::vector<std::vector<int> > (*)(int, std::vector<int> (*)(int))' for argument '1' to 'std::vector<int> printVector(std::vector<std::vector<int> > (*)(int, std::vector<int> (*)(int)))'

I would gladly appreciate some help.
Last edited on
You haven't quite followed your instructions.

Write a program that will have two user defined functions.

One function will allow the user to
enter integer values and store them in a two dimensional vector of size 3 x 3. The user must be
prompted to enter values using the following output statement “Enter value for Row R
Column C. R and C will display the actual row and column number. This function must return
the vector. The other function will accept a two dimensional vector and output the values stored
in it.

The other function will accept a two dimensional vector and output the values stored
in it.
I don't see these two functions.
At line 51 you are returning the value of position [R][C] of the vector instead of the vector itself.
At line 55, could you explain what you passed to the function?
Hi, i really dont know what else to do. I tried new things, but the same error, i dont know how to return a 2d vector and i dont how why the other errors too.
> i dont know how to return a 2d vector
So experiment with a 10-line program first to make sure you understand the syntax, before you create a 50 line mess of systemic errors.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <vector>
using namespace std;

vector<int> oneD() {
    vector<int> result(1);
    result[0] = 1;
    return result;
}

vector< vector<int> > twoD() {
    vector< vector<int> > result(1,vector<int>(1));
    result[0][0] = 2;
    return result;
}

int main ( ) {
    vector<int> one = oneD();
    vector< vector<int> > two = twoD();
    cout << one[0] << endl;
    cout << two[0][0] << endl;
}


If you're doing something new, your edit / compile / test cycle should be 5 lines of code.
i dont know how to return a 2d vector
The syntax of declarations in C and C++ has always been tricky because of the terse/minimal syntax. My rule of thumb is, whenever declarations are unclear, it's time to use a typedef to simplify things.

This is what the skeleton of your app might look like. The two functions are empty, but the syntax problems are resolved, you just need to fill in the functionality.
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
#include <vector>
  
typedef std::vector<int> vec1d_type;
typedef std::vector<vec1d_type> vec2d_type;

vec1d_type init_vec1d(size_t ncols, int default_value = 0) {
    return vec1d_type(ncols, default_value);
}

vec2d_type init_vec2d(size_t nrows, size_t ncols, int default_value = 0) {
    return vec2d_type(nrows, vec1d_type(ncols, default_value));
}

vec2d_type get_values() {
    vec2d_type values = init_vec2d(3, 3);
    return values;
}

void show_values(const vec2d_type& values) {
}

int main() {
    vec2d_type vec2d = get_values();
    show_values(vec2d);
}
Last edited on
Hi guys, I appreciate all your help provided. Actually I made a code with the combination of all of your codes. This is my code:

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

#include <iostream>
#include <vector>
using namespace std;

// Function that will populate a 2d vector with entered user data:
vector < vector <int> > vec2d () {
	
	// 2d vector, same as result [3][3];
	vector<vector <int> > result (3, vector <int> (3));
	
	// Introduction to program:
	cout << "\tYou will be requested to enter a total of 9 values." << endl << endl;
	
	// Loop that will request data from user 9 times:
	for ( int i=0; i < 3; i++ ) {
		for ( int j=0; j < 3; j++ ) {
			
			// Requesting data from user:
			cout << "\tEnter value for Row R Column C: ";
			cin >> result [i] [j];
		}
	} cout << endl;
	
	// outputting Matrix
	cout << "\tMatrix: " << endl;
	
	for ( int i=0; i < 3; i++ ) {
		for ( int j=0; j < 3; j++ ) {
			
			cout << result [i] [j] << "\t ";
		} 
		
		cout << endl;
	} 	cout << endl << endl;
	
	
	
	return result; 
}

int main ( ) {
	
	vector<vector <int> > twoDvec= vec2d();
	
	// Variables for row and column:
	int R,C;
	
	// Requesting data from user:
	cout << "\tEnter Row number: ";
	cin >> R;
	
	// Requesting data from user:
	cout << "\tEnter Column number: ";
	cin >> C;
	
	//Final output:
	cout << "\tThe value on Row index " << R << " and Column index " << C << " is: " << twoDvec [R] [C];
	
	return 0;
}


Thanks, and tell me what ya think.
Looks fine to me.
Topic archived. No new replies allowed.