so I have just started learning matrixes and I always like to keep my functions in c++ very flexible, so basically I wanted my functions to fill and show my matrix without setting limits to it in the functions parameters like I do while using unidimensional arrays but it wont let me, it seems like I must have to put a limit to it when I am just declaring the matrix and i have none to help me with it. This is the code that I am trying to make work:
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <ctime>
using namespace std;
int num1,num2,maxx;
void fillarray(int array1[][],int nummax)
{
int x=0;
srand(time(0));
for (x;x<num1;x++)
{
for (int y=0; y<num2;y++)
array1[x][y]=rand()%nummax;
}
}
void showarray(int array1[][])
{
int x=0;
for (x;x<num1;x++)
{
for (int y=0; y<num2; y++)
cout << array1[x][y]<<"\t";
cout << "\n"<<endl;
}
}
int main()
{
cout << "Input the number of lines in the mattrice: ";
cin >> num1;
cout << "Input the number of columns in the mattrice: ";
cin >> num2;
cout << "Input the maximum value in the mattrice: ";
cin >> maxx;
int matrice[num1][num2];
fillarray(matrice,maxx);
cout << "The matrix created: \n\n\n";
showarray(matrice);
return 0;
}
basically I put some limits to the matrixes and it works like this but this puts a lot of unused cells in the matrix and I wanted it to work like the first code that I wrote, is there anyway I can do it without declaring the limits the parameters? I am beginner so a bit of help would be really appreciated.
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <ctime>
using namespace std;
int num1,num2,maxx;
void fillarray(int array1[100][100],int nummax)
{
int x=0;
srand(time(0));
for (x;x<num1;x++)
{
for (int y=0; y<num2;y++)
array1[x][y]=rand()%nummax;
}
}
void showarray(int array1[100][100])
{
int x=0;
for (x;x<num1;x++)
{
for (int y=0; y<num2; y++)
cout << array1[x][y]<<"\t";
cout << "\n"<<endl;
}
}
int main()
{
cout << "Input the number of lines in the mattrice: ";
cin >> num1;
cout << "Input the number of columns in the mattrice: ";
cin >> num2;
cout << "Input the maximum value in the mattrice: ";
cin >> maxx;
int matrice[100][100];
fillarray(matrice,maxx);
cout << "The matrix created: \n\n\n";
showarray(matrice);
return 0;
}