In my computing class we have got to make a program which outputs the following

"XXXXXXXXXX
YXXXXXXXXX
YYXXXXXXXX
YYYXXXXXXX
YYYYXXXXXX
YYYYYXXXXX
YYYYYYXXXX
YYYYYYYXXX
YYYYYYYYXX
YYYYYYYYYX
YYYYYYYYYY"

We have to create a function and we can only use if and loops. but I'm having difficultly doing it outside of a function to start with. Please can somebody help?

I think I would need variables for the following:

loop counter
variable for x
variable for y
character counter

could I get by on 4 variables or do I need anymore

You'll need a variable to count the loops, and then another for the upper limit:

for(i=0;i<UpperLimit;i++)

In this case, Upper limit equals both the longest number of char's in the last row of the pyramid, but also, the number of rows to be printed. Convenient. ;)

How about a 'j' variable for the number of y's that have been printed, in that row.

Then

if(j<i) putchar('y')
  else putchar('x');

I've tried and I'm getting annoyed with this please could you put it in the correct format.

I've attempted below.

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

main()
{
      int i=0; // loop counter
      int l=0;  //loop 2
      int j=10; // number of y's printed

      for(l=0;l<10;l++)
      {
      for(i=0;i<10;i++)
      {
      if(j<i) putchar('y');
      else putchar('x');
      }
      printf("\n");
      }
      system("pause");
}

I would recommend not using the lowercase 'l' as a variable name; it's too easily confused with the numeral '1'.

Also, you don't need the extra variable here; the two loop indices are enough information. if you rename 'l' as 'j' and reverse the test in the if statement, it ought to work.

Finally, I suggest that you be more careful with you indentation; you should have the inner loop indented more than the outer one, and the if indented even more. The exact style of indentation is up to you, but you really ought to indent it so that the levels of nesting are clear:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

main()
{
      int i=0; // loop counter
      int j=0; // number of y's printed

      for(j=0;j<=10;j++)
      {
          for(i=0;i<10;i++)
          {
              if(j>i) 
                  putchar('Y');
              else 
                  putchar('X');
          }
          printf("\n");
      }
      system("pause");
}
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.