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.