I'm having a problem with a C++ program involving two dimensional arrays.
As part of the program I have to use a function which accepts as parameters two tables and adds them, returning another table.
I figured I could do something like this:
int** addTables(int ** table1, int ** table2)
{
int** result;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
result[i][j] = table1[i][j] + table2[i][j];
}
}
return result;
}
but I don't know how to find out the size of the table (rows and columns) for my "for" loops.
Does anybody have an idea of how to do this?
This is part of the code I was testing, but I'm not getting the right number of columns and rows:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main(int argc, char **argv)
{
const int n = 3; // In the addTables function I'm not supposed to know n.
int **tablePtr = new int*[n]; // I must use double pointer to int.
for (int i = 0; i < n; i++)
{
tablePtr[i] = new int[n];
}
srand((unsigned)time(0));
int random_integer;
for(int i = 0; i < n; i++) // I assign random numbers to a table.
{
for (int j = 0; j < n; j++)
{
random_integer = (rand()%100)+1;
tablePtr[i][j] = random_integer;
cout << tablePtr[i][j] << endl;
}
}
cout << "The table is " << sizeof(tablePtr) << " columns wide" << endl;
cout << "The table is " << sizeof(tablePtr) << " rows long" << endl;
return 0;
}
I appreciate any help, and please keep in mind that I'm new to C++.
vectororboost::multi_arrayfor this?