Makeing a table in C++?

Started by
8 comments, last by Vegadam 22 years, 3 months ago
Ok I need to know how to make a table that would print in two dimensions. Like this: 1 | 2 1 2 3
Advertisement
I would use a 2D array myself, given that I understand what you're talking about.

Here's a C++ example...

const int ROWS = 3; // number of rows
const int COLS = 2; // number of columns
int table[ROWS][COLS] = {0}; // all elements are 0

for ( int row = 0; row < ROWS; row++ )
{
for ( int col = 0; col < COLS; col++ )
{
cin >> table[row][col]; // user enters element of table
}
}

and then to print... replace the cin with a cout and throw a cout << endl; after the brackets for the nested for loop.

If you want a dynamic array...

int **pp_table = null;
int *p_table = null;
int rows, cols;

cin >> rows;
cin >> cols;
**pp_table = new int*[rows]; // array of pointers to array
*p_table = new int[cols]; // array of ints

for ( int j = 0; j < rows; j++ )
{
for ( int k = 0; k < cols; k++ )
{
cin >> pp_table[j][k];
}
}

and always clear memory when done with dynamic arrays:
delete pp_table[][];
delete p_table[];

Cheers
D

PS. anyone notice what a bitch it is to find good examples of dynamic multi-dimentional arrays on the net, sheesh!

[Edit:] Yeah, using i as an index value is a bad ideaa unless you use our "source" tags... The board interprets (without the spaces) as &lt;i>, meaning italics.<br><br>Edited by - Oluseyi on December 15, 2001 12:00:03 AM
Let''s try that again

I would use a 2D array myself, given that I understand what you''re talking about.

Here''s a C++ example...

quote:
const int ROWS = 3; // number of rows
const int COLS = 2; // number of columns
int table[ROWS][COLS] = {0}; // all elements are 0

for ( int row = 0; row < ROWS; row++ )
{
for ( int col = 0; col < COLS; col++ )
{
cin >> table[row][col]; // user enters element of table
}
}

and then to print... replace the cin with a cout and throw a cout << endl; after the brackets for the nested for loop.

If you want a dynamic array...
quote:
int **pp_table = null;
int *p_table = null;
int rows, cols;

cin >> rows;
cin >> cols;
**pp_table = new int*[rows]; // array of pointers to array
*p_table = new int[cols]; // array of ints

for ( int i = 0; i < rows; i++ )
{
for ( int j = 0; j < cols; j++ )
{
cin >> pp_table[j];
}
}

and always clear memory when done with dynamic arrays:
quote:
delete pp_table[][];
delete p_table[];

Cheers
D

PS. anyone notice what a bitch it is to find good examples of dynamic multi-dimentional arrays on the net, sheesh!
Ok, so it''s not going to cooperate... just note that there is a braket ''['' with an ''i'' and a closing braket '']'' right before the [j] near the end.

Maybe one of the moderators can fix that

Cheers again,
D
Thank you much!!
Ok one more question how do I make table print On top, and side to side?
In other words, what would array table look like?
#include



int column, row;
const int ROWS = 4;
const int COLUMNS = 2;

int main()
{
char string1[] = { "Adam", "Charles", "Austin", "Thorax" };

cout << "To check who is online, Please press enter";
cin.ignore(256,''\n'');
cout<<"press enter to continue..."< cin.get();

cout << "Checking.....";


That is what I have so far. I need to print the four names and if they are online or not.
So my row would be character, and columns Online and offline.
Vegadam,

Is this for homework? If so, look it up yourself. It''s not that hard. Trial and Error, get used to it

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

You posted, among other things, the following code:

char string1[] = { "Adam", "Charles", "Austin", "Thorax" }; 


A CString is an array of characters. You obviously want an array of CStrings. To do that, you need an array of arrays of characters. This is essentially what a two-dimensional array is. It is an array of arrays. A 3d array is an array of arrays of arrays, and so on. For this reason, that one line of code should be something like this:

	char string[4][16] = { "Adam", "Charles", "Austin", "Thorax" };	//Then, try the following:	cout << string[0] << endl;      // "Adam" 	cout << string[1] << endl;      // "Charles"	cout << string[0][0] << endl;   // ''A''	cout << string[1][1] << endl;   // ''h'' 


The reason this works is that the code could just as well be rewritten:

char string[4][16] = {{''A'', ''d'', ''a'', ''m'', ''\0''},                      {''C'', ''h'', ''a'', ''r'', ''l'', ''e'', ''s'', ''\0''},                      {''A'', ''u'', ''s'', ''t'', ''i'', ''n'', ''\0''},                      {''T'', ''h'', ''o'', ''r'', ''a'', ''x'', ''\0''}}; 

This topic is closed to new replies.

Advertisement