The goal is to allocate a 2d integer array, number of columns and rows are given by user input. But here, the rows must not be of the same size and the user will specify how many elements the individual rows are going to have. (This is the part that i do not understand) Then we'll have to assign values to the array elements by taking user inputs and then print the values. This is what I've come up with thus far. What am I doing wrong and what do I need to change?
#include<iostream>
using namespace std;
int main() {
int row,col;
cout<<"How many rows do you want? ";
cin>>row;
cout<<"How many columns do you want? ";
cin>>col;
int** arr = new int*[row];
for(int i = 0; i < row; i++){
arr[i] = new int[col];
}
for(int i = 0; i < row; ++i){
for(int j = 0; j < col; j++){
cout<<"R "<<i+1<<" C "<<j+1<<" value: ";
cin>>arr[i][j];
}
}
cout<<"Output array: \n";
for(int i = 0; i < row; ++i){
for(int j = 0; j < col; j++){
cout<<arr[i][j]<<"\t";
}
cout<<endl;
}
}
Thanks!
std::vector<T>instead ofT*pointing to heap allocated memory:vectorallocates and deletes its data automatically, copies itself properly, provides interface for algorithms etc.