0

I am writing a program in C that first prompts the user for an integer , then prints two strings: a blank space and a # on a variable number so that the result would be a right sided pyramid, like this:

     #
    ##
   ###
  ####
 #####
######

The problem i have resides in my for loop, here's what it looks like : (n is the integer that the user prompts)

for (int i = 0 ; i < n ; i++) 
{
    printf("%c",n-1,' ');  // printing the blank spaces gradually
    printf("%c\n",i+2,'#'); // printing the hashes gradually
}

The idea is to print an decreasing number of spaces and an increasing number of hashes depending on the int.

P.S : Please consider helping me by saying what is wrong with my code, not giving me an actual new working one.

5
  • 1
    Welcome to stack overflow! Unfortunately this is not a good question for this site, it is not a homework help site, although you can find lots of information that may help you do your homework here. As a general rule when you post code, you need to format it as code, you need to say what the exact error message / problem with it is, and in general you need to give a minimal working example. You should review the guidelines for questions in the help center so that you know what is an appropriate question to post in the future. (This one is pretty likely to be closed, I'm sorry to say.) Commented Sep 6, 2015 at 0:51
  • 1
    Both printf calls actually print the value of (char)(n - 1) or (char)(i+ 2) once and ignores the remaining arguments. Whatever makefile you're using, you should include -Wall in your cc line at the very least if your compiler supports it (or investigate the options your compiler has for turning up warnings/warning levels) and -Wextra if possible. That will catch a lot of coding mistakes these days, such as unused printf arguments. A common solution is a loop inside your current loop that prints n - i - 1 spaces followed by another loop after that to print '#' i times. Commented Sep 6, 2015 at 1:02
  • I've removed some extra stuff from the question that wasn't directly related to the question, and also added what I think you want the output to look like. (If I'm wrong about that, please fix it!) Commented Sep 6, 2015 at 1:11
  • Hey , thanks for the fast responses. Chris i've seen similar questions about string's length posted here , this is absolutely not a do-my-homework for me question , i think my P.S shows that pretty well doesn't it ? Now I do get a unused argument concerning the '#' and the ' ' but I read somewhere online that they were expected and that the program would run fine regardless of that , that's why I didn't post the actual error. Chrono I'm using clang ( p.s , thanks immibis the output shoud look like that and i struggled to make that happen on my question) Commented Sep 6, 2015 at 1:12
  • 3
    "I read somewhere online" is not a good way to learn C :) read the manual pages for printf, you will see that %c expects to match a character, not a number of items Commented Sep 6, 2015 at 1:27

3 Answers 3

1

First, the problem with the code. It is based on an incorrect understanding of printf. printf cannot be used in the way desired in the code. Furthermore, even if did, it wont be printing spaces 'gradually' because you have fixed 'n-1'. You probably meant 'n-i' so it can gradually shrink.

Anyways, to fix: There are a number of solutions, including rewriting with new logic. For your homework though, the code can still be 'fixed' by writing your own printf variant with desired functionality. e.g:

void mprintf(const char *fmt, int n, char c)
{
    while (n--)
   {
       printf("%c", c);
    }
 }

And your code becomes:

for (int i = 0 ; i < n ; i++)
{
    mprintf("%c",n-i,' ');  // printing the blank spaces gradually
    mprintf("%c",i,'#'); // printing the hashes gradually
    printf("\n");
}

Extra credit: Find out how you can simplify this code by removing certain useless thing.

Sign up to request clarification or add additional context in comments.

Comments

0

You know every row has the same number of chars, counting the spaces and #, which is n. In the first row you have only one #, so n - 1 spaces, on the second you have 2 #'s, so n - 2 spaces, and so on. So you iterate from 0 to n - 1, and print a char every iteration, and you just have to keep count of how many chars you've already print. Start printing spaces and as soon as you have printed enough spaces, print a #. Code for it:

for (int i = 0 ; i < n ; i++) 
{
    int j = 0;
    while (j < n)
    {
        if (j < n - i - 1)
             printf(" ");
        else
             printf("#");
        j++;
    }
    printf("\n");
}

Comments

0

You say:

Please consider helping me by saying what is wrong with my code, not giving me an actual new working one.

Ignoring cosmetic changes, your loop body is currently:

for (int i = 0; i < n; i++) 
{
    printf("%c", n-1, ' ');    // printing the blank spaces gradually
    printf("%c\n", i+2, '#');  // printing the hashes gradually
}

Problems include:

  • Too many arguments to the printf() calls.
  • %c requires a single argument, which is the character to be printed.
  • For n = 6, you will be printing Control-E rather than any blanks with the first print.
  • For the second print, you'll be printing Control-B through Control-G and no hashes.

You could use:

printf("%.*s", n - i - 1, " ");

to print a field of blanks of the appropriate width. You can't repeat a single character with printf(), though, so you'd have to use some other technique, such as a loop using putchar(), to print the appropriate number of hashes.

You should read, and re-read, and re-re-read the specification for printf(). I have to read it a couple of times a year to try and keep current; I usually find something new (or forgotten) on a given re-reading. Of course, I've only been coding in C for 30 years, and the specification has changed a few times over the years (and it hasn't gotten any simpler yet!), so it isn't very surprising.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.