Matrix creation and outputting it to the screen
Oct 28, 2017 at 7:00am UTC
Hello! I was trying to create to functions one to create matrix and other is to output it. User needs to enter N, which is the size of the matrix, but by some reason in side of a function N changes to some other random value and I totally cannot figure out why does this happen. Thank you!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
void MatrixCreator (int a[][1]);
void MatrixOutputer (int a[][1]);
int NxN[1][1];
int j,i,N;
int main ()
{
MatrixCreator (NxN);
cout << "Creation is done\n" ;
MatrixOutputer (NxN);
cout << "Output is out\n" ;
return 0;
}
void MatrixCreator (int a[][1])
{
srand (time(NULL));
cout << "Please enter N - size of desired matrix\nN = " ;
cin >> N;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
NxN [i][j] = rand() % 20 + (-10);
}
}
cout << N << endl;
}
void MatrixOutputer (int a[][1])
{
cout << N << endl;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
cout << "[" << a [i][j] << "] " ;
}
cout << "\n" ;
}
}
Last edited on Oct 28, 2017 at 7:00am UTC
Oct 28, 2017 at 7:29am UTC
line 32: change it to pass the parameter
I would suggest you to use a vector since the size of the array is arbitrary.
Last edited on Oct 28, 2017 at 7:32am UTC
Topic archived. No new replies allowed.