Char Arrays

So for my assignment, I was told to create two arrays in which can output this:
0 1 2 3 4
0 X X X X X
1 X X X X X
2 X X X X X
3 X X X X X
4 X X X X X
5 X X X X X
6 X X X X X
7 X X X X X
8 X X X X X
9 X X X X X

However, I am very inexperienced and don't understand many of the lessons taught in class. I know how to make a regular char array (like "char array[12]") but I do not know how to print out this Char Table array.

This is what I have so far....


const int ROW_SIZE=10; //Number of rows
const int COLUMN_SIZE=5; //Number of columns
char matrix[ROW_SIZE][COLUMN_SIZE]; // 2D Array
int header[10][5]; //Numeric Header

Last edited on
I do not know how to print out this Char Table array

To print it out, just make a loop inside another loop. Since you have the array dimensions, two for-loop can fit your needs.

The outer for-loop runs through the first array dimension (let’s say ‘rows’) and the inner loop runs through the second dimension (let’s say the ‘columns’).
you can think of it 2 ways, depending on what else you want to DO.
if your only goal is to print it, you have 3 ways actually.

if the goal is only to print:
method 1 is to just make one huge string. you can store spaces and end of lines right along with the data, end with zero end of string marker, and a single cout << table will do the job.

method 2 is an array of strings. format each line with spaces etc, and you can then loop over the rows and say cout << row[i] << endl //or store endl in the row too.

and method 3 is to treat the data as a table, and write a more complex print function that adds spaces and end of lines as it goes(this seems to be your approach). This is the right approach if you manipulate the data a lot before printing the table, maybe change and print again, etc.

Methods 1 and 2 are more useful if the data is just print once and done, without accessing it or modifying it or anything else (after a 1 time generation of it). Method 2 is sort between 1 and 3, regen a row if you need to change it, but not really directly accessing data via [row][col] approaches.
Last edited on
Hello moswager703,

jonnin has offered 3 ways to deal with printing the array. You may find using nested for loop a bit easier to understand right now. Later creating a "std::string" may be easier to understand.

You said:
I was told to create two arrays in which can output this:

Were you told that the 2 arrays have to be 2D arrays?

Given this code:
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
#include <iostream>
//#include <limits>  // <--- Not needed yet.
//#include <string>

int main()
{
	const size_t MAX_ROW{ 10 }; //Number of rows
	const size_t MAX_COL{ 5 }; //Number of columns

	int header[MAX_COL]{ 0, 1, 2, 3, 4 }; //Numeric Header. Define and initialize array.
	//std::string header{ "  0 1 2 3 4" };  // <--- Another possibility.

	char matrix[MAX_ROW][MAX_COL]  // <--- Define and initialize array.
	{
		'X', 'X', 'X', 'X', 'X',
		'X', 'X', 'X', 'X', 'X',
		'X', 'X', 'X', 'X', 'X',
		'X', 'X', 'X', 'X', 'X',
		'X', 'X', 'X', 'X', 'X',
		'X', 'X', 'X', 'X', 'X',
		'X', 'X', 'X', 'X', 'X',
		'X', 'X', 'X', 'X', 'X',
		'X', 'X', 'X', 'X', 'X',
		'X', 'X', 'X', 'X', 'X'
	}; // 2D Array

	//char matrix[MAX_ROW][MAX_COL]{};  // <--- Define and initialize array to "\0".

	// <--- For loop to set array to whatever chatacter you want. The same concept can be used for printing out the arrays.
	//for (size_t row = 0; row < MAX_ROW; row++)
	//{
	//	for (size_t col = 0; col < MAX_COL; col++)
	//	{
	//		matrix[row][col] = 'A';
	//	}
	//} 

Although there is nothing wrong with the names you used and you are free to use whatever you like, "MAX_ROW" or "ROW_MAX" better describe what they are for.

For the header array you only need a 1D array. The 2D array you started with is a lot of wasted space that can be taken care of in a for loop.

I am not sure what you had in mind or how you intended to to give "matrix" its values, but I have found this is the best way to initialize a 2D array and the best way of looking at a 2D array,

Lines 27 - 36 are a different way of defining and giving the array its values. The empty {}s at the end of line 27, the uniform initializer, is available form C++11 standards on. I have found this, ({}), to be the easiest way to initialize a variable or array.

Andy
Topic archived. No new replies allowed.