blob: ca6d150b729aef4fde2913625bbf7d31226ec43d (
plain) (
blame)
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
|
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char *argv[])
{
if (argc != 4) {
cout << "Invalid input!" << endl << " USAGE: ./rand-array m n max" << endl;
return 2;
}
srand(time(NULL));
int m, n, max;
m = atoi(argv[1]); //number of rows
n = atoi(argv[2]); //number of columns
int randArray[m][n];
max = atoi(argv[3]); //Maximum number in array
//Loop to make the random array
for (int curRow = 0; curRow < m; curRow++)
{
for (int curCol = 0; curCol < n; curCol++)
{
if (rand() % 2 + 1 == 2) { //Decide the sign of the number
randArray[curRow][curCol] = -1 * (rand() % (max+1));
} else {
randArray[curRow][curCol] = (rand() % (max+1));
}
}
}
//Loop to output the array in the console
for (int curRow = 0; curRow < m; curRow++)
{
for (int curCol = 0; curCol < n; curCol++)
{
cout << randArray[curRow][curCol] << " ";
}
cout << endl;
}
}
|