There are so many things wrong it is hard to know where to begin. Take for example:
int M[R][C];
...
for(int i = 0; i <= R; i++) { // will move forward one position
for(int j = 0; j <= C; j++) {
By looping i <= R
and j <= C
, you attempt to access element outside the bounds of your array invoking Undefined Behavior.
Next:
int M[R][C]; /* the C++ standard does not provide VLAs */
The C++ standard does not provide Variable Length Arrays. Initialization of a fundamental type must be by defined numeric constant (e.g. #define R 5
).
Further, why are you using the fundamental type int
for a 2D array to begin with? C++ provide vector
as part of the containers library that provides automatic memory management of arrays and also allowing you to create a vector of vectors to simulate a 2D array of any type needed. See std::vector and also visit the Algorithms library.
Since the vector container can grow dynamically, you can simply add values to each row, and then add rows simulating a 2D array as small or as large as you need (up to the limits of the physical memory)
There use is simple. A vector of int is simply declared as:
vector<int> myarray;
You then add values to your array with:
myarray.push_back(int_value);
To simulate a 2D array you simply declare a vector of vectors, e.g.
vector<vector<int>> my2Darray;
To fill the array you simply fill a temp array and then push that back into your vector of vectors, e.g.
#include <vector>
...
int R, C;
vector<vector<int>> M; /* vector of vectors int */
...
for (int i = 0; i < R; i++) { /* fill array with random values */
vector<int> tmp;
for (int j = 0; j < C; j++)
tmp.push_back(dis(gen)); /* add random to tmp */
M.push_back(tmp); /* push tmp back as row in M */
}
Iterating over the values in M
is made simply by using the range based for
loop which will automatically traverse each element. For example your //show function
can be written using range based for
loop as:
// show function
for (auto& r : M) { /* for each row vector r in M */
for (auto& c : r) /* for each c int in r */
cout << setw(3) << c; /* set fixed width of 3 and output c */
cout << '\n'; /* tidy up with a newline */
}
Next don't use C rand()
(which you forgot to seed the random number generator by calling srand()
anyway). Instead, C++ provides Uniform Random Number Generator like std::uniform_int_distribution, use them instead, e.g.
#include <random>
...
std::random_device rd; /* random seed */
std::mt19937 gen(rd()); /* standard mersenne_twister_engine */
std::uniform_int_distribution<> dis(1, 50); /* uniform dist in range */
...
tmp.push_back(dis(gen)); /* add random to tmp */
Putting it altogether, you could do:
#include <iostream>
#include <iomanip>
#include <vector>
#include <random>
using namespace std;
int max_nr (const vector<vector<int>>&);
int main (void) {
int R, C;
vector<vector<int>> M; /* vector of vectors int */
std::random_device rd; /* random seed */
std::mt19937 gen(rd()); /* standard mersenne_twister_engine */
std::uniform_int_distribution<> dis(1, 50); /* uniform dist in range */
std::cout << "Rows: "; /* prompt for R */
if (!(cin >> R)) /* validate EVERY input */
return 1;
std::cout << "Cols: "; /* prompt for C */
if (!(cin >> C)) /* ditto */
return 1;
cout << endl;
for (int i = 0; i < R; i++) { /* fill array with random values */
vector<int> tmp;
for (int j = 0; j < C; j++)
tmp.push_back(dis(gen)); /* add random to tmp */
M.push_back(tmp); /* push tmp back as row in M */
}
for (auto& r : M) { /* output simulated 2D array */
for (auto& c : r)
cout << setw(3) << c;
cout << '\n';
}
cout << "\nmax: " << max_nr (M) << '\n'; /* output max */
}
int max_nr (const vector<vector<int>>& M)
{
int max = std::numeric_limits<int>::min();
for (auto& row: M)
for (auto& col : row)
if (col > max)
max = col;
return max;
}
Example Use/Output
$ ./bin/vector2Drand
Rows: 5
Cols: 5
12 41 44 46 3
6 34 37 38 16
3 40 19 10 7
41 28 47 20 11
21 30 45 35 14
max: 47
Look things over and let me know if you have questions.