![]() |
| | #1 |
| Registered User Join Date: Nov 2006
Posts: 23
| hollow square Exercise 1: Display a Square Learning Objectives: To verify data entry, use while loops, and use ifstatements.Read This First You should have already read up on ‘while’ loops in your text. You are expected to use this command to display the box. You are alsoexpected to use only a single ‘if-else’ command when displaying the box (this will require a bit of thought). Do NOT use ‘for’ loops.What To Do Write a program (square.c) that reads in the size of the square and prints a hollow square of that size using asterisks and blanks. Your program must make a decision of whether to print a '*' or ' ' at aparticular location. Keep in mind that, using the DOS window, the output can only be displayed from left to right and (when you reachthe end of a line) from top to bottom. Your program should onlyaccept and work only for even side sizes between 2 and 20. Forexample, if your program reads in a size of 5, the following outputwould be displayed: ***** * * * * * * ***** ok here is my bad code Code: #include <stdio.h>
/* function main begins program execution */
int main()
{
int row; /* initialize row */
int column; /* define column */
int x;
printf( "Enter a number of row to be printed\n" );
scanf ( "%d", &x );
row = x;
while ( row >= 1 ) { /* loop until row < 1 */
column = 1; /* set column to 1 as interation begins */
while ( column <= row ) { /* loop times */
printf("%s", row ? "*" : "*"); /* output */
column++;/* increment column */
}
printf( "\n" ); /* begin new output line */
x--;
if ( x < 1 ) break;
} /* end outer while */
return 0; /* indicate program ended successfully */
} /* end function main */
joe |
| jms318 is offline | |
| | #2 |
| Registered User Join Date: Nov 2006
Posts: 176
| Code: #include <stdio.h>
int main()
{
int row;
int column;
int x;
printf( "Enter a number of row to be printed\n" );
scanf ( "%d", &x );
row = x;
/* draw top */
while ( row > 0 ) {
printf("*");
row--;
}
printf("\n");
row = x - 2;
column = x;
/* draw middle */
while (row > 0)
{
while (column > 0)
{
if ((column == x) || (column == 1))
printf("*");
else printf(" ");
column--;
}
printf("\n");
column = x;
row--;
}
row = x;
/* draw bottom */
while ( row > 0 ) {
printf("*");
row--;
}
printf("\n");
return 0;
}
|
| sl4nted is offline | |
| | #3 |
| Registered User Join Date: Nov 2006
Posts: 23
| thank you so much it works perfect sl4nted the only part of your code i do not understand 100% is Code: while (column > 0)
{
if ((column == x) || (column == 1))
printf("*");
else printf(" ");
column--;
}
thanks again joe |
| jms318 is offline | |
| | #4 |
| Registered User Join Date: Nov 2006
Posts: 176
| since your printing a hollow box...each row other than the top and bottom only contains 2 *'s, one at the very start and one at the very end. if you look, column = x when the loop starts so if (column == x) || (column == 1) will be true for the first iteration through the loop and * will print. it will never be true again untill column = 1, which is the very last iteration through the loop. notice how after the inside while is done column is set back to x, to make sure that each inside row prints the first time throught the loop, and the last time through the loop (left side and right side) hopefully that clear enough |
| sl4nted is offline | |
| | #5 |
| Registered User Join Date: Nov 2006
Posts: 176
| the way I read your teachers outline, it looks like you should also check to make sure the user has entered an integer between 2 and 20. So you will edit though while loops a bit since you only are allowed 1 if statement. while ((row > 0) && (row < 21)) for each one, if I understand the project right that is edit: actually it looks like it has to be between 2 and 20 and be even so make a big while loop holding all the other while loops Code: scanf
while ((x >= 2) && (x <= 20) && (x % 2 == 0))
{
all the code
x = 0;
}
Last edited by sl4nted; 11-30-2006 at 11:24 PM. |
| sl4nted is offline | |
| | #6 |
| Registered User Join Date: Nov 2006
Posts: 7
| Code: #include <stdio.h>
#include <stdlib.h>
/* function main begins program execution */
int main()
{
int row; /* initialize row */
int column; /* define column */
int x;
printf( "Enter a number of row to be printed\n" );
scanf ( "%d", &x );
row = x;
while ( row >= 1 ) { /* loop until row < 1 */
column = 1; /* set column to 1 as interation begins */
while ( column <= row ) { /* loop times */
printf("%s", x==1||x==row||column==1||column==row ? "*" : " "); /* output */
column++;/* increment column */
}
printf( "\n" ); /* begin new output line */
x--;
if ( x < 1 ) break;
} /* end outer while */
system("pause");
return 0; /* indicate program ended successfully */
} /* end function main */
|
| jeremy75 is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| hollow square | mdnealy | C# Programming | 8 | 07-05-2009 03:23 PM |
| Loop seg error | Zishaan | Game Programming | 2 | 03-28-2007 01:27 PM |
| Forced moves trouble!! | Zishaan | Game Programming | 0 | 03-27-2007 06:57 PM |
| Help with my draughts game | Zishaan | C++ Programming | 9 | 03-24-2007 07:33 AM |
| Hollow square | Kayoss | C++ Programming | 4 | 09-16-2005 04:49 PM |