diff options
Diffstat (limited to 'ee1301/wk5/hw5_directory/strap012_HW5B.cpp')
-rw-r--r-- | ee1301/wk5/hw5_directory/strap012_HW5B.cpp | 25 |
1 files changed, 18 insertions, 7 deletions
diff --git a/ee1301/wk5/hw5_directory/strap012_HW5B.cpp b/ee1301/wk5/hw5_directory/strap012_HW5B.cpp index 4576e59..ca6d150 100644 --- a/ee1301/wk5/hw5_directory/strap012_HW5B.cpp +++ b/ee1301/wk5/hw5_directory/strap012_HW5B.cpp @@ -8,23 +8,34 @@ int main(int argc, char *argv[]) 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 + 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) { - randArray[curRow][curCol] = -1 * (rand() % max); + { + if (rand() % 2 + 1 == 2) { //Decide the sign of the number + randArray[curRow][curCol] = -1 * (rand() % (max+1)); } else { - randArray[curRow][curCol] = (rand() % max); + randArray[curRow][curCol] = (rand() % (max+1)); } } } - - cout << endl; + + //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; + } }
\ No newline at end of file |