Rafa Romero
Rafa Romero

Reputation: 35

Line pattern in a diamond in C

I'm a beginner in C, I'm facing a problem in order to implement a diamond, I'm following guided exercises from a book but I got stuck, I have to implement the pattern:

enter image description here

According to the exercise, they suggest me to implement the pattern using the operator % enter image description here

I coded the structure but when I want to implement the pattern @.o.@ I just destroy everything

#include<stdio.h>


int main(){
  int n, space;
  printf("Number of sides: ");
  scanf("%d", &n);
  space = n - 1;
  for (int i = 0; i < n; i++){
       
        for (int j = 0;j < space; j++){
            printf(" ");
        }

        // patern @.o should be in this loop
        for (int j = 0;j <= i*2; j++){
            printf("@");
        }

        printf("\n");
        space--;
  }
  space = 0;
  for (int i = n; i >= 0; i--){
        
        for (int j = 0; j < space; j++){
            printf(" ");
        }
        // patern @.o should be in this loop
        for (int j = 0;j < (i*2)-1;j++){
            printf("@");
        }

        printf("\n");
        space++;
    }

}

I really appreciate it if somebody could give me a hint or help with this, I got stuck here for the last 2 weeks. Thank you.

Upvotes: 0

Views: 93

Answers (2)

Hamboy75
Hamboy75

Reputation: 1079

This is what you need

#include<stdio.h>



int main(){
  int n, space,i,j;
  char pattern[]={'@','.','o','.'};
  int position;
  printf("Number of sides: ");
  scanf("%d", &n);
  space = n - 1;
  for (i = 0; i < n; i++){

        position=0;
        for (j = 0;j < space; j++){
            printf(" ");
        }

        // patern @.o should be in this loop
        for (j = 0;j <= i; j++,position++)
            printf("%c",pattern[position%sizeof(pattern)]);

        position-=2;
        for(;j<=2*i;j++,position--)
            printf("%c",pattern[position%sizeof(pattern)]);

        printf("\n");
        space--;
  }
}

The output for some cases:

./test
Number of sides: 10
         @
        @.@
       @.o.@
      @.o.o.@
     @[email protected].@
    @.o.@[email protected].@
   @[email protected][email protected].@
  @[email protected][email protected].@
 @[email protected][email protected][email protected].@
@[email protected].@[email protected][email protected].@

./test
Number of sides: 5
    @
   @.@
  @.o.@
 @.o.o.@
@[email protected].@

./test
Number of sides: 2
 @
@.@

For a diamond:

#include<stdio.h>



int main(){
  int n, space,i,j;
  char pattern[]={'@','.','o','.'};
  int position;
  printf("Number of sides: ");
  scanf("%d", &n);
  space = n - 1;
  for (i = 0; i < n; i++){

        position=0;
        for (j = 0;j < space; j++){
            printf(" ");
        }

        for (j = 0;j <= i; j++,position++)
            printf("%c",pattern[position%sizeof(pattern)]);

        position-=2;
        for(;j<=2*i;j++,position--)
            printf("%c",pattern[position%sizeof(pattern)]);

        printf("\n");
        space--;
  }

space = 0;
  for (int i = n-1; i >= 0; i--){

        for (int j = 0; j < space; j++){
            printf(" ");
        }
        position=0;

        for (j = 0;j <= i; j++,position++)
            printf("%c",pattern[position%sizeof(pattern)]);

        position-=2;
        for(;j<=2*i;j++,position--)
            printf("%c",pattern[position%sizeof(pattern)]);

        printf("\n");
        space++;
    }
}

And the output

 ./test
Number of sides: 10
         @
        @.@
       @.o.@
      @.o.o.@
     @[email protected].@
    @.o.@[email protected].@
   @[email protected][email protected].@
  @[email protected][email protected].@
 @[email protected][email protected][email protected].@
@[email protected].@[email protected][email protected].@
@[email protected].@[email protected][email protected].@
 @[email protected][email protected][email protected].@
  @[email protected][email protected].@
   @[email protected][email protected].@
    @.o.@[email protected].@
     @[email protected].@
      @.o.o.@
       @.o.@
        @.@
         @

Upvotes: 1

Łukasz Strugała
Łukasz Strugała

Reputation: 75

you have to just change printf("@") to printf("%c",pattern[j%4])

Here is a code:

int main(){
  int n, space;
  char pattern[4] = {'@','.','o','.'};
  printf("Number of sides: ");
  scanf("%d", &n);
  space = n - 1;
  for (int i = 0; i < n; i++){

        for (int j = 0;j < space; j++){
            printf(" ");
        }

        // patern @.o should be in this loop
        for (int j = 0;j <= i*2; j++){
            printf("%c",pattern[j%4]);
        }

        printf("\n");
        space--;
  }

  space = 0;
  for (int i = n; i >= 0; i--){

        for (int j = 0; j < space; j++){
            printf(" ");
        }
        // patern @.o should be in this loop
        for (int j = 0;j < (i*2)-1;j++){
            printf("%c",pattern[j%4]);
        }

        printf("\n");
        space++;
    }
}

The reason why you "destroy everything" is probably because you tried to do it with printf(pattern[j%4]). printf as first parameter wants string (address in memory when string is stored) when you pass pattern[j%4], it thinks that for example '@' (in ASCII 64) is adress in memory, and want to read from it, but it is not valid memory adress so operating system kill your program and it "get's destroyed"

Upvotes: 1

Related Questions