Filling 2d array with alphanumeric characters

So my code below compiles. However, whenever the console window pops out it just crashes. Is there something wrong with my code, particularly at the getRandom function.


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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
 #include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

const int MAX = 100;

//Function for rows and columns
void getRowColumn(int&, int&);

//Function to return values
char getRandom(int);

//Function to construct 2D array
void construct2D(char[][MAX], int, int, int);

//Function to print 2D array
void print2D(const char [][MAX], int, int, int);

int main()
{
	char str[MAX][MAX];
	int rows, columns;
	int k;
	
	
	srand(time (NULL));
	getRandom(k);
	construct2D(str, rows, columns, k);
	print2D(str, rows, columns, k);
}


void getRowColumn(int& rows, int& columns)
{
	
	rows = rand() % 7 + 4;
	columns = rand() % 7 + 4;
	

}


char getRandom(int k)
{
	
	const char alphaNumeric [] = 
	"0123456789"					  	 
	"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	"abcdefghijklmnopqrstuvwxyz";
	
	k = sizeof(alphaNumeric) - 1;
	
	return alphaNumeric[rand() % k];
	
}


void construct2D(char str[][MAX], int rows, int columns, int k)
{
	
	
	for (int i = 0; i < rows; i++)
	{
		for (int j = 0; j < columns; j++)
		{
			str [i][j] = k;	   	   
		}
	}

}


void print2D(const char str [][MAX], int rows, int columns, int k)
{
	for (int i = 0; i < rows; i++)
	{
		for (int j = 0; j < columns; j++)
		{
			cout << str [i][j] << "     ";	  	  
		}
		
		cout << endl;
	}
	
}
Last edited on
The problem with getRandom(...) is that you pass k as a copy not as a reference. Thus in main k remains uninitialized. It does not make sense to pass k to construct2D(...)/print2D(...).

In order to initialize str with different letters you should assign the result of getRandom() on line 67.

Further more rows/columns remain uninitialized because you don't call getRowColumn(...).
getRandom is passed a parameter k (which you haven't defined beforehand, and which won't get passed back after being set in getRandom unless you make it a reference parameter by preceding it by & in the function header). Your code crashes because after your call to getRandom you try to use an undefined k in other routines.

On the other hand, getRandom returns a char, which you don't do anything with. TBH, it's not clear what you are trying to do.
lastchance wrote:
Your code crashes because after your call to getRandom you try to use an undefined k in other routines.
No, that's not the reason. See my post above: It's the uninitialzed rows/columns.
Thanks for the correction, coder777

Was this what was intended? The passing of k between routines has been removed. (Coder777 was right - it was unnecessary, but not the cause of the crash) and the call to getRandom in main is replaced by getRowColumn(...).


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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

const int MAX = 100;

//Function for rows and columns
void getRowColumn(int&, int&);

//Function to return values
char getRandom(int);

//Function to construct 2D array
void construct2D(char[][MAX], int, int );       // Last parameter removed

//Function to print 2D array
void print2D(const char [][MAX], int, int );     // Last parameter removed

int main()
{
	char str[MAX][MAX];
	int rows, columns;
	int k;
	
	
	srand(time (NULL));
	getRowColumn( rows, columns );            // Was this what you wanted?
	construct2D(str, rows, columns);          // No k needed
	print2D(str, rows, columns );             // No k needed
}


void getRowColumn(int& rows, int& columns)
{
	
	rows = rand() % 7 + 4;
	columns = rand() % 7 + 4;
	

}


char getRandom()                                  // No parameter needed
{
	int k;                                     // but still have to declare k
	const char alphaNumeric [] = 
	"0123456789"					  	 
	"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	"abcdefghijklmnopqrstuvwxyz";
	
	k = sizeof(alphaNumeric) - 1;
	
	return alphaNumeric[rand() % k];
	
}


void construct2D(char str[][MAX], int rows, int columns )     // No k needed
{
	
	
	for (int i = 0; i < rows; i++)
	{
		for (int j = 0; j < columns; j++)
		{
			str [i][j] = getRandom();              // As per Coder777's suggestion	   	   
		}
	}

}


void print2D(const char str [][MAX], int rows, int columns )   // No k needed
{
	for (int i = 0; i < rows; i++)
	{
		for (int j = 0; j < columns; j++)
		{
			cout << str [i][j] << "     ";	  	  
		}
		
		cout << endl;
	}
	
}
Topic archived. No new replies allowed.