1.Explain what each instruction in the following procedure does. void get_table(
ID: 3775624 • Letter: 1
Question
1.Explain what each instruction in the following procedure does.
void get_table(vector<vector <double> >& table)
{
int rows = table.size();
for (int r = 0; r < rows; r++) {
int columns = table[r].size();
for (int c = 0; c < columns; c++)
{
cout << "Enter value for (" << r << ", " << c << "): ";
cin >> table[r][c];
}
}
}
2.Explain what each of the following instructions does.
int main()
{
vector<vector <double> > m;
int rows, columns;
cout << "how many rows?: ";
cin >> rows;
m.resize(rows);
for (int r = 0; r < rows; r++)
{
cout << "how many columns for row " << r << "?: ";
cin >> columns;
m[r].resize(columns);
}
get_table(m);
Explanation / Answer
1. Program 1 get_table having return type as void , and parameters it take as vector<vector<double>> i.e. vector is a type of data structure like array , storing the vector type of objects and each objects having elements as double
void get_table(vector<vector <double> >& table) // function get_table with return type as void and input parameter as vector of vector having double as element
{
int rows = table.size(); // initializing the variable rows with the size of table .
for (int r = 0; r < rows; r++) { // for loop starting with rows 0 to 1 less than number of rows
int columns = table[r].size(); // variable columns is initialized with the size of table .
for (int c = 0; c < columns; c++) // for loop starting from 0 to 1 less than coloumn .
{
cout << "Enter value for (" << r << ", " << c << "): "; // Print on console to take the user input for values rows and coloumns
cin >> table[r][c]; // initialized the table with r and c
}
}
}
.
2. int main() :- This is the main function which is calling the function get_table.
Explanation of each statement is given as below in comments .
int main()
{
vector<vector <double> > m; // Vector type data structure defined , stroing vectoor type objects of double type elements
int rows, columns; // variables rows and columns are defined of type int
cout << "how many rows?: "; // print on console "how many rows"
cin >> rows; // stores the user input in rows variable .
m.resize(rows); // resize function is used to resize the vector according to no of rows .
for (int r = 0; r < rows; r++) // for loop starting from 0 to 1 less than the number of rows .
{
cout << "how many columns for row " << r << "?: "; // print on console for how many coloumn for each row
cin >> columns; // storing the input in variable column
m[r].resize(columns); // resize function on vector m .
}
get_table(m); // function call with parameter as vector m .
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.