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.