-1

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;
}
  • 2
    Stop using C-style arrays. Forget that they exist in the language. Use `std::array` and/or `std::vector` exclusively. It'll make your life easier. – Jesper Juhl Apr 19 '23 at 13:17
  • 2
    [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) Consider using a [class](https://stackoverflow.com/a/2076668/4944425) instead. – Bob__ Apr 19 '23 at 13:37

1 Answers1

1

As suggested in comments, you should not use C-arrays. Use std::vector and std::array and get yourself familiar with standard algorithms.

However, a simple way to make your functions "more flexible" is to refactor to write functions that operate on single elements:

#include <iostream>
#include <algorithm>

void set_elem(int& x) { x = 42; } 
void print_elem(int x) {std::cout << x; }


int main() {
    int matrix[5][5]{};    
    for (auto& row : matrix) {
        for (auto& elem : row) {
            set_elem(elem);
            print_elem(elem);
        }
        std::cout << "\n";
    }
}

Above code is not the end of the story, you should really get rid of the c-array. However, set_elem and print_elem can be used also after you replaced the c-array with std::vector or std::array. You could also use them eg with the standard algorithm std::for_each, though since range based loops are available the loop is often more convenient.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185