Reputation: 1
I was creating a function that would give me random values of a matrix. I know how to make it in the main
function but I want a separate one. I also tried with void
and the same thing happened, and I keep getting the same error. I am beginner and I understand that the issue is something to do with pointers but I still don't know what should I do to make it work and fix it. I already Googled it but I can't make this work.
#include<stdlib.h>
#include<iostream>
#include<time.h>
int row, coll;
int random(int *mat[])
{
srand(time(0));
for(int i=0; i<row; i++)
{
for(int j=0; j<coll; j++)
{
mat[i][j] = rand()%10;
}
}
}
main()
{
std::cin >> row >> coll;
int mat[row][coll];
random(mat);
}
Upvotes: 0
Views: 1906
Reputation: 412
I wrote a simple struct of matrix for you to carry the data around functions. Also, a output overload to make it easier to demo. Please look over it, and don't hesitate to ask, if you need more elaborations.
#include<stdlib.h>
#include<iostream>
#include<time.h>
#include <iomanip>
struct Matrix
{
int *arr;
const int row, col;
Matrix() = delete;
Matrix(const int i, const int j): row(i), col(j)
{
arr = new int [i*j];
}
~Matrix() { delete [] arr; }
int operator()(const int i, const int j) const {return arr[i*col+j];}
int&operator()(const int i, const int j) {return arr[i*col+j];}
};
std::ostream& operator<<(std::ostream&osm, const Matrix&M)
{
for (int i=0; i<M.row; i++) {
for (int j=0; j< M.col; j++) osm << std::setw(8) << M(i,j);
osm << std::endl;
}
return osm;
}
void random(Matrix& amtx)
{
srand(time(0));
for(int i=0; i<amtx.row; i++)
{
for(int j=0; j<amtx.col; j++)
{
amtx(i,j) = rand()%10;
}
}
}
int main()
{
int r, c;
std::cout << "Input row and column : ";
std::cin >> r >> c;
Matrix mtx(r, c);
random(mtx);
std::cout << mtx;
return 0;
}
and the test run:
Input row and column : 5 5
6 3 1 9 3
6 5 1 4 1
1 2 7 5 5
9 3 2 8 1
7 4 8 3 5
Upvotes: 0
Reputation: 311058
You are trying to mix features of two different languages in one program.
The presented program is not a valid C++ program nor a valid C program.
For example variable length arrays as in your program
std::cin >> row >> coll;
int mat[row][coll];
is not a standard C++ feature.
The function main shall have the return type int
.
Your function random
int random(int *mat[])
has the return type int
but returns nothing.
The argument has the type (if to assume that variable length arrays are supported)
int ( * )[coll]
but the function parameter type is
int **.
If you are going to write a C++ program then instead of a variable length array use the standard container std::vector<std::vector<int>>
.
For example
std::cin >> row >> coll;
std::vector<std::vector<int>> mat( row, std::vector<int>( coll ) );
Otherwise write a C program that can look like
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void random( size_t row, size_t col, int mat[row][col], int limit )
{
srand( ( unsigned int )time( NULL ) );
for ( size_t i = 0; i < row; i++ )
{
for ( size_t j = 0; j < col; j++ )
{
mat[i][j] = rand() % limit;
}
}
}
int main( void )
{
size_t row, col;
scanf( "%zu %zu", &row, &col );
int mat[row][col];
random( row, col, mat, 10 );
return 0;
}
Upvotes: 2