Hello friends. I'm trying to write a program that accepts an odd number from 1-9 and outputs the diamond of asterisks as follows

user enters 5
_ _ _ * _ _ _
_ _ * * * _ _ 
_ * * * * * _
_ _ * * * _ _ 
_ _ _ * _ _ _

I was able to accomplish the problem using nested for loops but I then realized that I needed to use a 2d array, which is a new topic to me. I need help re writing the following program to include a 2d array.
Here is my original code than runs with nested for loops:

#include <stdio.h>
int main()
    {
    int i, j, k, rows;
    char wait;
    printf("Specify the number of rows (between 1-9) in the dimond: ");
    scanf ("%d",&rows);
    scanf("%c", &wait);
    printf("\n");
  
    for(i=1; i<=rows-2; i++)
        {
        for(j=1; j<=rows-i; j++)
        printf("%c",' ');
        for(k=1; k<=2 * i-1; k++)
        printf("%c",'*');
        printf("\n");
        }
        int l,m,n;
        for (n = rows-3; n > 0;n--)
        {
        for (l = 1; l <= rows- n; l++)
        printf("%c",' ');
        for (m = 1; m<= 2 * n- 1; m++)
        printf("%c",'*');
        printf("\n");
        }
        printf("Enter any key to end");
        scanf("%c", &wait);
         return(0);
       }

I'm really having trouble understanding how to fill the 2d array with the appropriate number of asterisks.
I wrote a function to print the array:

void printArray(char array[MAXROW][MAXROW], int rows)
{
    int r, c;
    for(r = 0; r < rows; r++)
      {
         for(c = 0; c < rows; c++)
         {
                 printf("%c", array[r][c]);
         }
         printf("\n");
      }  
    printf("\n");
}

I defined MAXROW as 9 and tried to call the function like this:

char array[MAXROW][MAXROW];
printArray(array, rows);

but it returned a bunch of nonsense characers.
I'm stuck at this point and ask for some help. I need to introduce a 2D array into the program.
Thanks for any help.
*EDIT*
I just realized that the original nested for loop solution is not correct when I entered 3 instead of 5.
I will try to resolve this problem first then on to the array.

Recommended Answers

All 8 Replies

I resolved the problem for the nested if statements by using a series of if statements
Here is my revised code:

#include <stdio.h>
 
int main()
    {
    int i, j, k, rows;
    char wait;
    printf("Specify the number of rows (between 1-9) in the dimond: ");
    scanf ("%d",&rows);
    scanf("%c", &wait);
    printf("\n");
    if(rows == 9)
    rows=rows-4;
    else if(rows == 7)
    rows=rows-3;
    else if(rows == 5)
    rows=rows-2;
    if(rows == 7)
    rows=rows-3;
    if(rows == 1)
    rows=rows;
    else
    printf("Invalid input\n");
    for(i=1; i<=rows; i++)
        {
        for(j=1; j<=rows-i; j++)
        printf("%c",' ');
        for(k=1; k<=2*i-1; k++)
        printf("%c",'*');
        printf("\n");
        }
        int l,m,n;
        for (n = rows-1; n > 0;n--)
        {
        for (l = 1; l <= rows- n; l++)
        printf("%c",' ');
        for (m = 1; m<= 2 * n- 1; m++)
        printf("%c",'*');
        printf("\n");
        }
        printf("Enter any key to end");
        scanf("%c", &wait);
            return(0);
       }

If any one has a more efficient way than using if/else please let me know.
Still looking for some input on a 2d array solution.

I just realized again that the code in my last post is flawed.

if(rows == 7)   
rows=rows-3;    
if(rows == 1)    
rows=rows;

These two if statements hsould be else if.
But that is not really the issue here. ;)
Sorry to make so many consecutive posts. I really need to proofread my code before posting it.
I have constructed the 2d array as such

char board [rows][cols];

I'm struggling to find a way to fill it properly and output the correct pattern. Any help would be greatly appreciated. Thanks.

You can do this without the 'if' checks and with 2 nested for loops.

Break your problem up to it's smallest pieces and choose a path that solves the pieces with the least redundancy.

Smallest piece - put '*' in array at correct place...

"--*--"

ar[0][cnt/2] = '*'

Next piece - do it as whole row ...

cStart = cnt/2;
cEnd  = cStart+1;

for(c = cStart; c < cEnd ; c++)
{
    ar[r][c] = '*';
}

Now examine the next higher piece - multiple rows...

What is the relationship between rows ? Well, starting out cStart is 1 less and cEnd is 1 greater...

"--*--"
"-***-"
"*****"
"-***-"
"--*--"


cStart = cnt/2;
cEnd  = cStart+1;

for(r = rStart; r< rEnd; r++ )
{
    for(c = cStart; c < cEnd ; c++)
    {
        ar[r][c] = '*';
    }
    cStart--;
    cEnd++;
}

Well, that makes a nice christmas tree. Not quite what you're after but it works for the first part so ...

"--*--"
"-***-"

cnt 	= 5;
rStart 	= 0;
rEnd 	= rStart + (cnt - 1)
rMid 	= rStart + (cnt/2)
cStart 	= rMid;
cEnd  	= cStart+1;

for(r = rStart; r< rMid; r++ )
{
    for(c = cStart; c < cEnd ; c++)
    {
        ar[r][c] = '*';
    }
    cStart--;
    cEnd++;
}

From here all you have to do is figure out how to set the other half of the array and the center row. You can do it without adding any more loops or variables.

Thanks SVR. I didn't exactly use your simplified solution but I was able to resolve my problem by filling and printing the array. Thanks for taking the time to help me.
I do have another question that I can use some help with.
I want to print the diamond pattern but have each star encased in a box.
|_|_|_|*|_|_|_|
|_|_|*|*|*|_|_|
|_|*|*|*|*|*|_|
|_|_|*|*|*|_|_|
|_|_|_|*|_|_|_|
Here is the code I'm using:

#include <stdio.h>
#define MAX 9
int main()
    {
    char board [MAX][MAX];
    int i, j, k, rows;
    char wait;
    printf("Specify the number of rows (between 1-9) in the diamond: ");
    scanf ("%d",&rows);
    scanf("%c", &wait);
    printf("\n");
    
    if(rows == 9)
    rows=rows-4;
    else if(rows == 7)
    rows=rows-3;
    else if(rows == 5)
    rows=rows-2;
    else if(rows == 3)
    rows=rows-1;
    else if(rows == 1)
    rows=rows;
    else
    {
        printf("Invalid input\n");
        rows=0;
    }
    
    for(i=1; i<=rows; i++)
        {
             /*top half horizontal*/
             for(j=1; j<=rows-i; j++)
             /* prints spaces before stars */
             {board[i][j]=' ';
             printf("%c", board[i][j]);}

             /*top half vertical*/
             for(k=1; k<=2*i-1; k++)
            { board[j][k]='*';
             printf("%c", board[j][k]);}
             printf("\n");
        }
        int l,m,n;
    for (n = rows-1; n > 0;n--)
        {
            /* bottom half horizontal*/
            for (l = 1; l <= rows- n; l++)
            {
                board[n][l]=' ';
             printf("%c", board[n][l]);}
            /* bottom half vertical*/
            for (m = 1; m<= 2 * n- 1; m++)
            {board[l][m]='*';
             printf("%c", board[l][m]);}
            printf("\n");
        }
        printf("\n");
        printf("Enter any key to end");
        scanf("%c", &wait);
        return(0);
        
        }

Any suggestion would be appreciated. Thanks.

Just change your printf("%c" to printf("|%c" and printf("\n" to printf("|\n"

You should probably split the code into sections though, like: ClearArray, FillArray, and ShowArray. Then you can make them functions.

btw, the changes to fill the other half were ...

int main(int argc, char* argv[])
{
   char ar[10][10];
   
   memset(ar,' ',sizeof(ar));   // clear array

   int cnt     = 5; // user input

   int rStart  = 0; // could adjust this to center diamond
   int rEnd    = rStart + (cnt - 1);

   int mid     = rStart + (cnt/2);

   int cStart  = mid;
   int cEnd    = cStart+1;

   // Fill the array

   ar[mid][mid] = '*';

   for(int r = rStart; r < mid; r++)
   {

      ar[mid][r       ] = '*';
      ar[mid][rEnd - r] = '*';

      for(int c = cStart; c < cEnd; c++)
      {
          ar[r     ][c] = '*';
          ar[rEnd-r][c] = '*';
      }

      cStart--;
      cEnd++;

   }

   // show the array
   for(int r1 = 0; r1< 9; r1++)
   {
      for(int c1 = 0;c1<9;c1++)
      {
         printf("|%c",ar[r1][c1]);
      }
      printf("|\n");
   }

	return 0;
}

Oh, just a note on loops & arrays...

In 'C' array indexes start at 0 not 1 like basic.
So your loops should go from 0 to max-1 or (i=0;i<max;i++).

Thanks for the suggestion. However, I'm having a bit of trouble gettin the design I want. Here is what I get:
|_| |_| |_|*|_
|_| |_|*|_|*|_|*|_
|_|*|_|*|_|*|_|*|_|*|_
|_| |_|*|_|*|_|*|_
|_| |_| |_|*|_
Here is the code I'm using:

for(i=1; i<=rows; i++)
        {
             /*top half horizontal*/
             for(j=1; j<=rows-i; j++)
             /* prints spaces before stars */
             {board[i][j]=' ';
             printf("|_|%c", board[i][j]);}

             /*top half vertical*/
             for(k=1; k<=2*i-1; k++)
            { board[j][k]='*';
             printf("|_|%c", board[j][k]);}
             printf("|_\n");
        }
        int l,m,n;
    for (n = rows-1; n > 0;n--)
        {
            /* bottom half horizontal*/
            for (l = 1; l <= rows- n; l++)
            {
                board[n][l]=' ';
             printf("|_|%c", board[n][l]);}
            /* bottom half vertical*/
            for (m = 1; m<= 2 * n- 1; m++)
            {board[l][m]='*';
             printf("|_|%c", board[l][m]);}
            printf("|_\n");
        }
        printf("\n");
        printf("Enter any key to end");

Any ideas to get the output from my last post. Thanks.

I think this code runs best

#include <stdio.h>
int main()
    {
    int i, j, k, rows;
    char wait;
    printf("Specify the number of rows (between 1-9) in the dimond: ");
    scanf ("%d",&rows);
    scanf("%c", &wait);
    printf("\n");
  
    for(i=1; i<=rows-2; i++)
        {
        for(j=1; j<=rows-i; j++)
        printf("%c",' ');
        for(k=1; k<=2 * i-1; k++)
        printf("%c",'*');
        printf("\n");
        }
        int l,m,n;
        for (n = rows-3; n > 0;n--)
        {
        for (l = 1; l <= rows- n; l++)
        printf("%c",' ');
        for (m = 1; m<= 2 * n- 1; m++)
        printf("%c",'*');
        printf("\n");
        }
        printf("Enter any key to end");
        scanf("%c", &wait);
         return(0);
       }

Thanks to SVR for the help.

This is just some of my tinkering on the topic:

#include <stdio.h>

void diamond(int size)
{
   int i, j, i2;
   for ( i = 0, i2 = size / 2; i < size; ++i )
   {
      int stars = i <= i2 ? 2 * i + 1 : 2 * (size - i) - 1;
      int start = i <= i2 ? i2 - i : i - i2;
      for ( j = 0; j < size; ++j )
      {
         putchar(j >= start && j < start + stars ? '*' : ' ');
      }
      putchar('\n');
   }
   putchar('\n');
}

int main()
{
   int i;
   for ( i = 3; i < 10; i += 2 )
   {
      printf("%d:\n", i);
      diamond(i);
   }
   return 0;
}

/* my output
3:
 *
***
 *

5:
  *
 ***
*****
 ***
  *

7:
   *
  ***
 *****
*******
 *****
  ***
   *

9:
    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *
*/
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.