3

I want to add an additional space to the beginning of each new line. the current output is:

$****
 $***
  $**
   $*
    $
     Press any key to continue . . .

What I want is:

$****
 $***
  $**
   $*
    $
Press any key to continue . . .

I added the for-loop! it looks almost perfect except the "press any key to continue..." message is tabbed. How can this be fixed?

Code:

for(r=1; r<=5; r++) {
  printf("$");
  for(c=1; c<=5;c++) {
    if(6-r<=c) {
      printf(" ");
    } else {
      printf("*");
    }
  }
  printf("\n");
  n++;
  for (f=0;n>f;f++)
    {
        printf(" ");
    }
}
CorySTG
  • 81
  • 1
  • 2
  • 7
  • 1
    http://stackoverflow.com/a/276869/986924 has an example and helpful links. – Ben Graham Oct 24 '12 at 01:55
  • Can you give us the code segment where you printf the "Press any key" statement? – davepmiller Oct 24 '12 at 02:07
  • I'm using Visual studio and "running w/o debugging" after the code is executed in a command prompt window it says; "Press any key to continue . . .". My program doesn't say to do that. – CorySTG Oct 24 '12 at 02:11
  • so you don't want it to say "Press any key to continue" at all? – davepmiller Oct 24 '12 at 02:16
  • we haven't learned to have it not say that, so I'd rather it stay there. I just don't want it tabbed over like it is. I want it flush to the left-hand side of the screen. – CorySTG Oct 24 '12 at 02:18
  • I just updated my question. It now reflects what I want. sorry for the misleading info. – CorySTG Oct 24 '12 at 02:20
  • 1
    well, see [link](http://codepad.org/gicD3WyG) the solution is here, just go to the page and hit the submit button, the code will run and you can check the output. – NANDAGOPAL Oct 24 '12 at 02:23

4 Answers4

1

In its simplest form: just print spaces, then the '$', then the asterisks.

int r, c;
for(r=0; r<5; r++) {
  for(c=0; c<r; c++){
    printf(" ");
  }
  printf("$");
  for(c=r+1; c<5;c++) {
      printf("*");
  }
  printf("\n");
  n++;//What is this here for?
}

You can pare that down with a great printf trick (already submitted by someone else, so I won't go into it here:

int r, c;
for(r=0; r<5; r++) {
  printf("%*s", r+1, "$");
  for(c=r+1; c<5;c++) {
      printf("*");
  }
  printf("\n");
  n++;//What is this here for?
}

But wait, there's more! using a period (.), you can force a string to truncate, so:

int r;
for(r=1; r<6; r++) {
  printf("%*s%.*s\n", r, "$", 5-r, "*****");
}

Bam! Got the whole thing down to one statement!

FrankieTheKneeMan
  • 6,645
  • 2
  • 26
  • 37
  • the n is there as a counter. It makes more sense because I partially solved my problem. Now the "Press any key" message is tabbed and I'd like it to be left-align. – CorySTG Oct 24 '12 at 02:06
  • @CorySTG Check out the new version of my answer. – FrankieTheKneeMan Oct 24 '12 at 02:12
  • Also, in the future I want to alter this so it allows the user to enter the number of rows to have this pattern continue. and since I haven't learned strings yet, I'd rather stay away from this unless it's stupid simple to understand. – CorySTG Oct 24 '12 at 02:24
1

You can use the trick of variable left-padding when you write the $:

printf("%*s", r, "$");

The * in the string format field lets you specify a padding width.

paddy
  • 60,864
  • 6
  • 61
  • 103
  • This won't print the asterisks, but it's a good step in the right direction. – FrankieTheKneeMan Oct 24 '12 at 02:00
  • I haven't learned anything about strings or how to use them in my class. I'm sure this is a good way to do it, but I have no clue what is actually going on or how to implement it. – CorySTG Oct 24 '12 at 02:15
  • Both of you... This was a replacement for the line where you print a single `$`. I didn't see it necessary to include the rest of the code that prints the `*`s because I didn't need to modify that: http://ideone.com/uftrQ2 – paddy Oct 24 '12 at 02:32
1

Currently your n++; does nothing. To print(" ") n times you need to use something like a for loop. One way of doing this: Print the amount of spaces, Then print the amount of stars you need which should be 4 - n.

#include <stdio.h>

int main(int argc, char *argv[]) {
    int r;
    int c;
    int n = 0;
    for(r=1; r<=5; r++)
    {
        for (int i = 1; i<=n; i++) { //Print " " n number of times.
            printf(" ");
        }
        printf("$"); //Print dollar sign.
        for(c=0; c<(4-n);c++) //Print * 4-n times.
        {
            printf("*");
        }
        printf("\n");
        n++;

    }
}
Dair
  • 15,910
  • 9
  • 62
  • 107
1

Well you got the answer in your question itself, "how to say printf(" "); 'n' times" the easiest way to say printf 'n' times is to run it 'n' times, just like how you asked the program to print '*' (6-r) times!

well here is a very simple code to help you get the output you wanted

#include<stdio.h>
void main()
{
    int r,c,n=0;

    for(r=1; r<=5; r++)
    {
        for(c=n;c>0;c--)
        {
            printf(" ");

        }
        printf("$");
        for(c=1; c<=5;c++)
        {
            if((6-r)<=c)
            {
                printf(" ");
            }
            else
            {
                printf("*");
            }
        }

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

    }
}

If you want to check out the code just go to Codepad.org here you can run your code and see the output

NANDAGOPAL
  • 282
  • 2
  • 14