C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old December 12th, 2004, 01:07 PM
br459 br459 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 5 br459 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 40 m 50 sec
Reputation Power: 0
Question Two dimensional Arrays

I am having trouble getting my display function to work. My code currently outputs a series of rectangles instead of one rectangle with the characters incorporated like the output at the bottom. Any help will be appreciated.
#include <iostream>

usingnamespace::std;

constint NUM_ROWS = 10, // Board dimensions

NUM_COLS = 20;

// Function prototypes

void fillRectangle ( char board[NUM_ROWS] [NUM_COLS] ,

int row, int col,

int width, int height, char fillChar );

void displayBoard ( constchar board[NUM_ROWS] [NUM_COLS] );

int main ()

{

char board[NUM_ROWS][NUM_COLS]; // Message board

// Initialize the message board to all periods.

fillRectangle(board,0,0,NUM_COLS,NUM_ROWS,'.');

// Load and display a message.

fillRectangle(board,2,2,6,2,'C');

fillRectangle(board,4,2,3,4,'C');

fillRectangle(board,6,5,3,2,'C');

fillRectangle(board,4,10,3,1,'#');

fillRectangle(board,3,16,1,3,'#');

fillRectangle(board,3,11,1,3,'#');

fillRectangle(board,4,15,3,1,'#');

cout << endl;

displayBoard(board);




}

//--------------------------------------------------------------------

void fillRectangle ( char board[NUM_ROWS][NUM_COLS],

int row, int col,

int width, int height, char fillChar )

{

for (int i = 0; i < NUM_ROWS; i++)

{
cout << endl;

for (int j = 0; j < NUM_COLS; j++)

{

cout<< " " <<fillChar;

}

}

cout << endl;



}

//--------------------------------------------------------------------



void displayBoard ( constchar board[NUM_ROWS][NUM_COLS])

{



for (int i = 0; i < NUM_ROWS; i++)

{ cout << i<< " "<<endl;

for (int j = 0; j < NUM_COLS; j++)

{

cout << j<< " " <<endl;

}

}

cout << endl;



}



/*

....................

....................

..CCCCCC............
..CCCCCC...#....#...

..CCC.....###..###..

..CCC......#....#...

..CCCCCC............

..CCCCCC............

....................


Last edited by br459 : December 12th, 2004 at 01:18 PM. Reason: Still checking my code

Reply With Quote
  #2  
Old December 12th, 2004, 01:25 PM
kode_monkey kode_monkey is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 367 kode_monkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 21 sec
Reputation Power: 6
Not entirely sure what it is you need help with. Could you explain what you are trying to achieve please?

-KM-

Reply With Quote
  #3  
Old December 12th, 2004, 02:07 PM
br459 br459 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 5 br459 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 40 m 50 sec
Reputation Power: 0
The first time my program calls the function fillRectangle it is suppose to create a rectangle of periods that is ten rows of twenty columns. The next time fillRectangle is called it should start at the cell [2][2] and replace the periods with two rows that are six columns wide of the character C. Finally after all of the periods have been replaced with the C's and #'s the displayBoard function is called and it should print out on screen the output I included in the program I submitted. I can get the rectangle of periods no problem, but when I am making subsequent calls to the fillRectangle function all I get is more rectangles of characters instead of replacing the periods with the new fill character.

Quote:
Originally Posted by kode_monkey
Not entirely sure what it is you need help with. Could you explain what you are trying to achieve please?

-KM-

Reply With Quote
  #4  
Old December 12th, 2004, 09:32 PM
marmo marmo is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 37 marmo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 15 m 20 sec
Reputation Power: 5
I have fixed the errors in your code. You pretty much had it, you just need a little more work with loops.
Code:
   #include <iostream>
   using namespace std;
   
   // Board dimensions
   const int NUM_ROWS(10);
   const int NUM_COLS(20);
   
   // Forward declarations
   void FillRectangle( char board[NUM_ROWS][NUM_COLS], int row, int col, int width, int height, char fillChar );
   void DisplayBoard ( const char board[NUM_ROWS][NUM_COLS] );
   
   int main()
   {
   	char board[NUM_ROWS][NUM_COLS]; // Message board
   
   	// Initialize the message board to all periods
   	FillRectangle( board, 0, 0, NUM_COLS, NUM_ROWS, '.' );
   	DisplayBoard( board );
   
   	// Starting at ( 2, 2 ) fill 2 rows with 6 'C's
   	FillRectangle( board, 2, 2, 6, 2, 'C' );
   	DisplayBoard( board );
   
   	// Starting at ( 4, 2 ) fill 3 rows with 4 'C's
   	FillRectangle( board, 4, 2, 3, 4, 'C' );
   	DisplayBoard( board );
   
   	// Starting at ( 6, 5 ) fill 3 rows with 2 'C's
   	FillRectangle( board, 6, 5, 3, 2, 'C' );
   	DisplayBoard( board );
   
   	// Starting at ( 4, 10 ) fill 3 rows with 1 '#'s
   	FillRectangle( board, 4, 10, 3, 1, '#' );
   	DisplayBoard( board );
   
   	// Starting at ( 3, 16 ) fill 1 row with 3 '#'s
   	FillRectangle( board, 3, 16, 1, 3, '#' );
   	DisplayBoard( board );
   
   	// Starting at ( 3, 11 ) fill 1 row with 3 '#'s
   	FillRectangle( board, 3, 11, 1, 3, '#');
   	DisplayBoard( board );
   
   	// Starting at ( 4, 15 ) fill 3 rows with 1 '#'s
   	FillRectangle( board, 4, 15, 3, 1, '#' );
   	DisplayBoard( board );
   
   	cout << endl;
	return 0;
   }
   
   // Fills a rectangle with a specified char
   void FillRectangle( char board[NUM_ROWS][NUM_COLS], int row, int col, int width, int height, char fillChar )
   {
   	// Cycle through rows
   	for( int i(row); i < row + height; ++i )
   	{
   		// Cycle through cols
   		for( int j(col); j < col + width; ++j )
   		{
 			// Change the value at position ( i, j ) to the fillChar
   			board[i][j] = fillChar;
   		}
   	}
   }
   
   // Displays the board
   void DisplayBoard( const char board[NUM_ROWS][NUM_COLS] )
   {
   	// Cycle through rows
   	for( int i = 0; i < NUM_ROWS; i++)
   	{
   		cout << endl;
   
   		// Cycle through cols
   		for( int j = 0; j < NUM_COLS; j++)
   		{
   			// Display the value at ( i, j )
   			cout << board[i][j] << " ";
   		}
   	}
   	cout << endl;
   }
   

Reply With Quote
  #5  
Old December 13th, 2004, 06:20 PM
br459 br459 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 5 br459 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 40 m 50 sec
Reputation Power: 0
Smile

Thanks for your help.



Quote:
Originally Posted by marmo
I have fixed the errors in your code. You pretty much had it, you just need a little more work with loops.
Code:
#include <iostream>
using namespace std;
 
// Board dimensions
const int NUM_ROWS(10);
const int NUM_COLS(20);
 
// Forward declarations
void FillRectangle( char board[NUM_ROWS][NUM_COLS], int row, int col, int width, int height, char fillChar );
void DisplayBoard ( const char board[NUM_ROWS][NUM_COLS] );
 
int main()
{
	char board[NUM_ROWS][NUM_COLS]; // Message board
 
	// Initialize the message board to all periods
	FillRectangle( board, 0, 0, NUM_COLS, NUM_ROWS, '.' );
	DisplayBoard( board );
 
	// Starting at ( 2, 2 ) fill 2 rows with 6 'C's
	FillRectangle( board, 2, 2, 6, 2, 'C' );
	DisplayBoard( board );
 
	// Starting at ( 4, 2 ) fill 3 rows with 4 'C's
	FillRectangle( board, 4, 2, 3, 4, 'C' );
	DisplayBoard( board );
 
	// Starting at ( 6, 5 ) fill 3 rows with 2 'C's
	FillRectangle( board, 6, 5, 3, 2, 'C' );
	DisplayBoard( board );
 
	// Starting at ( 4, 10 ) fill 3 rows with 1 '#'s
	FillRectangle( board, 4, 10, 3, 1, '#' );
	DisplayBoard( board );
 
	// Starting at ( 3, 16 ) fill 1 row with 3 '#'s
	FillRectangle( board, 3, 16, 1, 3, '#' );
	DisplayBoard( board );
 
	// Starting at ( 3, 11 ) fill 1 row with 3 '#'s
	FillRectangle( board, 3, 11, 1, 3, '#');
	DisplayBoard( board );
 
	// Starting at ( 4, 15 ) fill 3 rows with 1 '#'s
	FillRectangle( board, 4, 15, 3, 1, '#' );
	DisplayBoard( board );
 
	cout << endl;
	return 0;
}
 
// Fills a rectangle with a specified char
void FillRectangle( char board[NUM_ROWS][NUM_COLS], int row, int col, int width, int height, char fillChar )
{
	// Cycle through rows
	for( int i(row); i < row + height; ++i )
	{
		// Cycle through cols
		for( int j(col); j < col + width; ++j )
		{
			// Change the value at position ( i, j ) to the fillChar
			board[i][j] = fillChar;
		}
	}
}
 
// Displays the board
void DisplayBoard( const char board[NUM_ROWS][NUM_COLS] )
{
	// Cycle through rows
	for( int i = 0; i < NUM_ROWS; i++)
	{
		cout << endl;
 
		// Cycle through cols
		for( int j = 0; j < NUM_COLS; j++)
		{
			// Display the value at ( i, j )
			cout << board[i][j] << " ";
		}
	}
	cout << endl;
}

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Two dimensional Arrays


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT