blob: 85082832ca4703c59b01460575802e4cacc2c4da (
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
42
|
#include <iostream>
#include <time.h>
#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[curCol][curRow] = -1 * (rand() % (max+1));
} else {
randArray[curCol][curRow] = (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[curCol][curRow] << " ";
}
cout << endl;
}
}
|