Tuesday, July 14, 2009

REctangle code C++ 2005 express?

Im having a hard time, can some one help me with this program??? This is my assignment, im lost


Write a program that displays a rectangle of given the length (an integer size less


than or equal to 21) and the width (an integer less than or equal to 79). Your


program should accept as input from the keyboard the character used to form the


rectangle, and the two values for the length and width of the rectangle.





Out of range input values should result in an informative error message.





Your program should process only one input case, legal or not. Termination occurs


after either the error message is printed or the diamond is drawn.





It is necessary that the program be written using loops.





Sample runs:





Rectangle drawing program


Enter the character, and length and width (eg. x 7):A 7 12


AAAAAAAAAAAA


A A


A A


A A


A A


A A


AAAAAAAAAAAA





Rectangle drawing program


Enter the character, and length and width (eg. x 7):R 3 5


RRRRR


R R


RRRRR





Rectangle drawing program


Enter the character, and length and width (eg. x 7):A 27 32


You have entered an illegal value for the length. It must be between 1 and 21.





You should use a named constant (const) to define and refer to the box size limits.


This will make it easy to modify the program at a later date if the size restriction is


changed. Write a class called Rectangle. Have the appropriate data members for


that class. Also have a constructor and a function to output the rectangle.

REctangle code C++ 2005 express?
So you need to have 2 functions, one for the header and footer block, and the other for the middle pieces. Then using math figure out how many times to do it.





psuedo code:





gets(userinput);





for ( int i = 0; i %26lt; userinput[2]; i++ )


{


int length = userinput[4];





if (userinput[2] == i || i == 0 )


drawFullCharacterRowOf ( userinput[0], length );


else


drawPartialCharacterRowOf( userinput[0] );


}








void drawFullCharacterRowOf ( char val, int length)


{


for ( int i = 0; i %26lt; length; i++ )


cout %26lt;%26lt; val %26lt;%26lt; endl;


}








This is extremely pseudo code, but you see the point I would imagine.
Reply:// TestCPPConsole.cpp : Defines the entry point for the console application.


//





#include "stdafx.h"


#define MAX_LENGTH 21


#define MAX_WIDTH 79





class Rectangle


{


private:


char m_cUseThisChar;


int m_iHeight;


int m_iWidth;





public:


Rectangle( char cUseThisChar, int iHeight, int iWidth )


{


m_cUseThisChar = cUseThisChar;


m_iHeight = iHeight;


m_iWidth = iWidth;


};





void DrawRectangle( char * szRectangle )


{


char * szTemp = ( char * ) malloc( ( m_iWidth + 2 ) * sizeof( char ) );


try


{


szRectangle[ 0 ] = 0;





memset( szTemp, 0, m_iWidth + 2 );


memset( szTemp, m_cUseThisChar, m_iWidth );


szTemp[ m_iWidth ] = '\n';





for( int iCnt = 1; iCnt %26lt;= m_iHeight; iCnt++ )


strcat( szRectangle, szTemp );


}


catch(...)


{


strcpy( szRectangle, "Error" );


}


if( szTemp != NULL )


{


free( szTemp );


szTemp = NULL;


}


};


};





int _tmain(int argc, _TCHAR* argv[])


{


char cUseThisChar;


int iHeight, iWidth;


printf( "Enter the character, and length and width (eg. x %d %d) ):\n", MAX_LENGTH, MAX_WIDTH );


scanf( "%c %d %d", %26amp;cUseThisChar, %26amp;iHeight, %26amp;iWidth );





if( iHeight %26gt; MAX_LENGTH )


{


printf( "You have entered an illegal value for the length. It must be between 1 and %d.", MAX_LENGTH );


getch();


return 1;


}


if( iWidth %26gt; MAX_WIDTH )


{


printf( "You have entered an illegal value for the width. It must be between 1 and %d.", MAX_WIDTH );


getch();


return 1;


}





Rectangle rect( cUseThisChar, iHeight, iWidth );


char * szBuffer = ( char * ) malloc( ( ( ( iWidth + 2 ) * iHeight ) + 10 ) * sizeof( char ) );


rect.DrawRectangle( szBuffer );


printf( szBuffer );


free( szBuffer );


szBuffer = NULL;


getch();


return 0;


}


======================================...


Disclaimers:


1. Content of stdafx.h file is:





//------------------------------------...


#pragma once


#define WIN32_LEAN_AND_MEAN


#include %26lt;stdio.h%26gt;


#include %26lt;stdlib.h%26gt;


#include %26lt;string.h%26gt;


#include %26lt;string.h%26gt;


#include %26lt;tchar.h%26gt;


#include %26lt;conio.h%26gt;


//------------------------------------...





2. Error handling must be improved


3. Due to deprecation, functions like getch(), strcpy() etc will get warnings. Use other library methods if you are picky.


No comments:

Post a Comment