| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
Not entirely sure what it is you need help with. Could you explain what you are trying to achieve please?
-KM- |
|
#3
|
|||
|
|||
|
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:
|
|
#4
|
|||
|
|||
|
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;
}
|
|
#5
|
|||
|
|||
|
Thanks for your help.
Quote:
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Two dimensional Arrays |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|