-1

So i have assignment to type a code which will ask me for a "w". After i type in a number it will create a rhombus with a diagonal which is 2w. The rhombus must be made of intervals and * . The problem that i face now is that when i type w=5 the diagonal is 5 instead of 10 ....

main()
{
int w;
int i;
int j;

printf("w: ");
scanf("%d", &w);
printf("");

i = 0;
while (w >= i)
{
    for (j = 0; j < (w - i); j++)
        printf(" ");
    for (j = 0; j < i + 1; j++) {
        printf("*");
        if (j <= i) {
            printf(" ");
        }
    }
    printf("\n");
    i = i + 1;
}
i = w - 1;
while (i >= 0)
{
    for (j = 0; j < (w - i); j++)
        printf(" ");
    for (j = 0; j < i + 1; j++) {
        printf("*");
        if (j <= i) {
            printf(" ");
        }
    }
    printf("\n");
    i = i - 1;
}
return 0;
}
unwind
  • 391,730
  • 64
  • 469
  • 606
Juginator
  • 9
  • 1
  • 5

2 Answers2

2

If you add the line w = 2*(w-1) + 1; before any of the loops then you get the correct number of *s to print out ( I just looked for the pattern you were getting and modified the input)

You can also do this problem with just one loop!

Edit:

#include <stdio.h>
#include <math.h>

#define min(a, b) (((a) < (b)) ? (a) : (b))
int main(){

    int input, row, column;

    printf("input a number: ");
    scanf("%d", &input);
    printf("");

    input = 2*(input-1) + 1;
    int pivot = input;
    int total_spaces = input*2+1;
    for(row = 0; row < total_spaces; row++){
        for(column = 0; column < total_spaces; column ++){
            if(min(abs(total_spaces - row -1),abs(0 - row)) +
                  min(abs(total_spaces - column -1),abs(0 - column)) 
                        >= input && (row%2 != column %2)){
                printf("*");
            }
            else printf(" ");
       }
       printf("\n");
    } 
}

That was a doozy!

Treesrule14
  • 711
  • 7
  • 14
-1

I ran your program and I had this :

/a.out
w: 5
     *
    * *
   * * *
  * * * *
 * * * * *
* * * * * *
 * * * * *
  * * * *
   * * *
    * *
     *

I do not see where your diagonal is 5. Can you be more specific ?

Also, I understand how this may not be important for you but as-is your code won't compile. At least add int before your main function.

nsierra-
  • 21
  • 5
  • the number "w" that you type in is 1/2 of the diagonal.For example if w=10 the rhombus should look like this: [link](http://i.imgur.com/EBCgSBj.jpg) – Juginator Nov 06 '14 at 17:25
  • @nsierra, count from the left-most star. The next star to the right along the horizontal rhombus diagonal is at a distance of 1. Continue counting to the right-most star and you will reach a distance of 5. Are you confusing *How many fence panels* with *How many fence posts*? – Weather Vane Nov 06 '14 at 17:29