![]() |
| | #1 |
| Registered User Join Date: Oct 2009
Posts: 1
| diamond shape, for loop only ****The question and output sample is in the tinypic link**** Here's what I have so far but I want it to print out asterisks instead of numbers. Thanks! Code: int main(){
int i;
printf("This program prints a diamond of stars with an odd number of rows in the range 1 to 19.\n\n\n");
do{
printf("Enter an odd number in the range 1 to 19 for the number of rows of the diamond:\t");
scanf("%d",&i);
if (i%2 == 0 )
{
printf("ERROR: Invalid entrry of rows.\n\n");
}
}while ( i%2 == 0 );
int j,k,l;
// print top
for ( j = 0; j<i/2 ; j++ ){
for ( k =0; k< i/2 - j; k++){
printf("-");
}
for( l=0; l < (2*j + 1); l++){
printf("%d",l+1);
}
printf("\n");
}
//print middle
for( l=0; l <i; l++)
{
printf("%d",l+1);
}
printf("\n");
for( l=i; l >0; l--)
{
printf("%d",l);
}
printf("\n");
//print bottom
for ( j = 0; j<i/2 ; j++ ){
for ( k =0; k<j+1 ; k++){
printf("-");
}
for( l=1; l < i - (2*j + 1); l++){
printf("%d",l);
}
printf("\n");
}
return 0;
}
|
| bombers is offline | |
| | #2 |
| Registered User Join Date: Oct 2006 Location: Canada
Posts: 1,230
| Im confused: is your program working as expected, with the only exception being that it prints "numbers" instead of "*"? If thats the case, isnt it easy to just change the number to an "*"? If this isnt the case, then give us more details. For example, give us the output for some sample input (i.e. input of 9, which should have same output as the link). Make sure to use "code" tags to preserve the whitespace in the output. EDIT: Also, arent you required to minimize the number of "printf" statements? It looks like your using quite a few here. Last edited by nadroj; 10-27-2009 at 03:23 PM. |
| nadroj is offline | |
| | #3 |
| Registered User Join Date: Sep 2006
Posts: 3,151
| You don't need a middle. You need two large for loops. The first one will print the upper half of the diamond shape, as the rows increment, by two, in the loop. Each of these two for loops will have two smaller loops inside them - either a while, or another for loop. The outer for loop controls how many rows will be printed. The first inner loop controls the number of spaces printed before the stars, on the line. The second inner loop controls printing the stars. Before you print anything, yo need some simple arithmetic: spaces = total stars - rows stars for this row = total stars - spaces You'll print up half the spaces, with another while or for loop, and then the stars in their own for or while loop. You don't need to print the second half of the spaces, at all. Then a newline. You'll start with rows = 1, and end when row >= total stars. The second for loop is just like the first, except you'll decrement the outer for loop variable of rows by two, each time through the for loop, and you'll start rows with a value equal to total stars - 2, because the longest line of stars, has already been printed. The rest of the loop, stays the same. Last edited by Adak; 10-27-2009 at 06:09 PM. |
| Adak is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Simple Blackjack Program | saber1357 | C Programming | 1 | 03-28-2009 03:19 PM |
| Bitwise Unwanted Output | pobri19 | C++ Programming | 4 | 09-15-2008 04:07 AM |
| Diamond loopy | swgh | C++ Programming | 2 | 09-24-2007 09:10 AM |
| Newbie- having trouble with functions | bnmwad | C Programming | 7 | 02-22-2005 04:41 PM |
| A solid shape diamond | viryonia | C++ Programming | 2 | 03-10-2003 10:22 PM |