Hey, everyone~

Being the second time posting here, I'll try to make it simple but clearly as possible :)

So, my assignment this time is about writing a C program to print the following star patterns:

*
      *  *
   *  *  *
*  *  *  *

So, here is an answer that I found online:

/* This is a simple mirror-image of a right angle triangle */

#include
int main() {
 char prnt = '*';
 int i, j, nos = 4, s;
 for (i = 1; i <= 5; i++) { for (s = nos; s >= 1; s--) {  // Spacing factor
   printf("  ");
  }
  for (j = 1; j <= i; j++) {
   printf("%2c", prnt);
  }
  printf("\n");
  --nos;   // Controls the spacing factor
 }
 return 0;
}

BUT....BUT...!!!!

The lecturer in my class hasn't taught us this type of coding yet.
So we're supposed to write a simpler program for it, with using only char variable and printf w/ scanf instructions. So, I tried doing it like this:

#include <stdio.h>
    int main()
    {
    char x = '*'
    printf("%4c", x\n);
    printf("%3c %4c", x x\n);
    printf("%2c %3c %4c", x x x\n);
    printf("%1c %2c %3c %4c", x x x x\n);
    return 0;
    }

BUT I found it to be COMPLETELY WRONG.....So, I want to write the program like the one I did above here CORRECTLY...not like the program I found up there. Get me? If you get what I mean, please help me out and correct the mistakes I did in the program I wrote above here (^_*) Thanks in advance!

A good simple way to do this is to always use two nested for loops:

An outer for loop to control the row variable(s)
   and an inner for loop to control the printing on that one line currently being printed.

Think of it like you are always going to print a box, and the box is made up on each row, with spaces and *'s. There is a relationship between the row you're currently printing, and the number of starts that need to be printed. Also, the number of stars, and the number of spaces, equal the width of the box. That is true for every row, regardless.

You may now safely and full confidence, chuck your code in the dust bin, and promise me you will not try to use such as that again. ;) It's all about using loops. Otherwise, it's headache city!

Hey, everyone~

Being the second time posting here, I'll try to make it simple but clearly as possible :)

So, my assignment this time is about writing a C program to print the following star patterns:

*
      *  *
   *  *  *
*  *  *  *

So, here is an answer that I found online:

/* This is a simple mirror-image of a right angle triangle */

#include
int main() {
 char prnt = '*';
 int i, j, nos = 4, s;
 for (i = 1; i <= 5; i++) { for (s = nos; s >= 1; s--) {  // Spacing factor
   printf("  ");
  }
  for (j = 1; j <= i; j++) {
   printf("%2c", prnt);
  }
  printf("\n");
  --nos;   // Controls the spacing factor
 }
 return 0;
}

BUT....BUT...!!!!

The lecturer in my class hasn't taught us this type of coding yet.
So we're supposed to write a simpler program for it, with using only char variable and printf w/ scanf instructions. So, I tried doing it like this:

#include <stdio.h>
    int main()
    {
    char x = '*'
    printf("%4c", x\n);
    printf("%3c %4c", x x\n);
    printf("%2c %3c %4c", x x x\n);
    printf("%1c %2c %3c %4c", x x x x\n);
    return 0;
    }

BUT I found it to be COMPLETELY WRONG.....So, I want to write the program like the one I did above here CORRECTLY...not like the program I found up there. Get me? If you get what I mean, please help me out and correct the mistakes I did in the program I wrote above here (^_*) Thanks in advance!

best way is to use nested loops but i have corrected your code but its not a good one

#include <stdio.h>
    int main()
    {
    char x = '*'
    printf("%4c\n",x);
    printf("%3c %4c\n",x,x);
    printf("%2c %3c %4c\n",x,x,x);
    printf("%1c %2c %3c %4c\n", x,x,x,x);
    return 0;
    }
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.