Archived

This topic is now archived and is closed to further replies.

Vegadam

Makeing a table in C++?

Recommended Posts

Guest Anonymous Poster   
Guest Anonymous Poster
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 [ i ] (without the spaces) as <i>, meaning italics.

Edited by - Oluseyi on December 15, 2001 12:00:03 AM

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
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!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
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

Share this post


Link to post
Share on other sites
Vegadam    122
#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.

Share this post


Link to post
Share on other sites
TerranFury    142
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''}};

Share this post


Link to post
Share on other sites