Answer the question
In order to leave comments, you need to log in
C++ - How to fill a matrix in a function and pass it to Main?
I know it's a simple question, but I haven't found an answer to it yet. The bottom line is to create a matrix, for example, 5x4 and fill it in a function, and then pass the result back to Main so that you can then pass it to other functions. Or is it done somehow easier and I complicate everything?
Answer the question
In order to leave comments, you need to log in
You need to create a matrix, pass it to the function, fill it and return it to main.
For a matrix, either create a class, or do int m[5][4] .
Thanks to maaGames for the advice. If anyone is interested, here is the code itself:
#include "stdafx.h"
#include <iostream>
#include <ctime>
using namespace std;
int FillMatrix(int Mat[5][4])
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 4; j++)
{
Mat[i][j] = rand() % 50;
}
}
return Mat[5][4];
}
int main()
{
srand(time(0));
int Matrix[5][4];
FillMatrix(Matrix);
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 4; j++)
{
cout << Matrix[i][j] << "\t";
}
cout << endl;
}
system("pause");
return 0;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question