GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 27-Jun-2004, 01:01
crizzo crizzo is offline
New Member
 
Join Date: Jun 2004
Posts: 3
crizzo is on a distinguished road
Question

Need help with 2-D array print out


I have a problem where I have to print an airline seating chart and represent vacant seats with an asterick. I have the program working correctly, but I can't figure out how to print "Row 1", "Row 2", etc. at the beginning of each row when printing the array. Here is the part of the code where I initialize and print the array:
CPP / C++ / C Code:
#include <iostream>
#include <iomanip>

using namespace std;

const int rows = 13;
const int columns = 6;
int row;
int col;
char seatChart[rows][columns];

//function prototypes
void initSeatArray(const int, const int, int, int, char[13][6]);

void main ()
{
	initSeatArray(rows, columns, row, col, seatChart);
}
	

//Funtion Definitions

void initSeatArray(const int rows, const int columns,int row, int col, char seatChart[13][6])
{
	for(row=0; row<rows; row++)
	for(col=0; col<columns; col++)
		seatChart[row][col] = '*';
		
	cout<<"Any seat marked with an asterick is available.\n";
	cout<<setw(5)<<"A"<<setw(6)<<"B"<<setw(6)<<"C"<<setw(6)<<"D"<<setw(6)<<"E"<<setw(6)<<"F"<<endl;
		
	for(row=0; row<rows; row++)
	{
		for(col = 0; col<columns; col++)
			cout<<setw(5)<<seatChart[row][col]<<" ";
			cout<<endl;
	}
}

The output looks like this:

Any seat marked with an asterick is available.
Code:
A B C D E F * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

I would like it to look like this:

Any seat marked with an asterick is available.
Code:
A B C D E F Row 1 * * * * * * Row 2 * * * * * * Row 3 * * * * * * Row 4 * * * * * * Row 5 * * * * * * Row 6 * * * * * * Row 7 * * * * * * Row 8 * * * * * * Row 9 * * * * * * Row 10 * * * * * * Row 11 * * * * * * Row 12 * * * * * * Row 13 * * * * * *

Any help or suggestions would be greatly appreciated!
Last edited by JdS : 28-Jun-2004 at 09:49. Reason: Please insert [c] & [/c] tags between your example C codes
  #2  
Old 27-Jun-2004, 01:55
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Well you've only got a single line to add :-P

CPP / C++ / C Code:
for(row=0; row<rows; row++)
  {
    cout << "Row " << row << " "; // Can you tell me why this works?
    for(col=0; col<columns; col++)
      cout<<setw(5)<<seatChart[row][col]<<" ";
    cout<<endl; // I'll give you a hint... it's the same reason this line 
  }             //makes a new line after each row!
__________________
-Aaron
  #3  
Old 27-Jun-2004, 22:36
crizzo crizzo is offline
New Member
 
Join Date: Jun 2004
Posts: 3
crizzo is on a distinguished road

Duh- I was trying to make it way too hard!


Thanks Aaron!

Yes, I see- it is very simple and I was trying to make it really hard. I went down the path of adding a column to the array and then changing from char to string.....then writing a statement for each cell in the first column. I appreciate your response!

Cori
 
 

Recent GIDBlogFlickr uploads of IA pictures by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
looking for a way to identify a repetitive sequence of digits in an array or string sho C++ Forum 8 14-Jun-2004 23:59
Speed up C++ code about 3d array! Truong Son C++ Forum 0 16-Mar-2004 21:52
Extra null element in an array samtediou MySQL / PHP Forum 2 11-Dec-2003 11:52
Guestbook error BobbyDouglas Web Design Forum 1 16-Oct-2003 22:17

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:16.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.