0

i've done this code to print a triangle after a side is scanned. It does show me the triangle but it prints me a new line right after i add a side which is not meant to be there. Here is my code and i'll also add my output, i've marked the new line with white. Please help, thank you.

#include<stdio.h>

void printTriangle (int n)
{
    for (int i = 0; i <= n; i++)
    {
        for (int j = 0; j < i; j++)
        {
            if (j > 0 && j < (i-1) && i > 0 && i < n)
            {
                printf("-");
            }
            else
            {
                printf("*");
            }
        }
        printf ("\n");
    }
}

int main ()
{
    int n;
    scanf ("%d", &n);
    printTriangle (n);

    return 0;
}

enter image description here

Aconcagua
  • 24,880
  • 4
  • 34
  • 59
Tara
  • 1
  • 1
  • 2
    Do you mean the case where `i == 0` *and* `j == 0` and the inner loop won't run, and you only print a newline? Some quick [rubber duck debugging](https://en.wikipedia.org/wiki/Rubber_duck_debugging) should have told you that. Or at the very least a quick session inside a debugger to step through the code statement by statement. – Some programmer dude Dec 16 '21 at 14:46
  • 1
    Also please don't post images of text, copy-paste text *as text*. – Some programmer dude Dec 16 '21 at 14:47
  • 1
    Consistent formatting makes the code much more readable... There's no need to put empty lines in between any two lines of code, that just blurs the code, and you shouldn't add any code after closing braces. Just compare to how I re-formatted (used an auto-formatter, thus now Allman style...). – Aconcagua Dec 16 '21 at 14:51
  • 1
    Off-topic: If you just want to print one single character `putchar` would be better choice, that's more efficient. – Aconcagua Dec 16 '21 at 14:51
  • Side note: `for(i = 0; i <= n; ++i)` will print n + 1 lines (including the unwanted empty one). You might want to start with 1 or instead `for(i = 0; i < n; ++i) for(j = 0; j <= n; ++j)`. – Aconcagua Dec 16 '21 at 14:55
  • I'd recommend, too, printing the asterisks outside of the inner loop, you spare quite a number of checks that way... – Aconcagua Dec 16 '21 at 14:58
  • 1
    Yes, that did work, thank you very much @Aconcagua – Tara Dec 16 '21 at 15:01
  • @Aconcagua in your 2nd last comment I didn't get why you kept `j<=n` rather than `j – Abhishek Mane Dec 23 '21 at 15:45
  • @Tara your for loop condition ` i > 0` is un-necessary as `j < (i-1)` fail if `i=0` and your 4th condition `i < n` also become un-necessary if you correct yourself as `Aconcagua` told. when you are combining conditions enclosing each one in bracket is good practise. – Abhishek Mane Dec 23 '21 at 15:50
  • 1
    @AbhishekMane Consider `n == 0` – the inner loop wouldn't run at all if you compared for `<` only – producing the empty line the question started upon... `n==1` is second line and would now print two characters. – Aconcagua Dec 23 '21 at 15:51
  • @AbhishekMane Why should `if((x != 0) && (j != 0))` be any superior to `if(x != 0 && j != 0)`??? That whole salad of additional *parentheses* (brackets are usually referred to `[]` unless you say *round brackets*) rather makes code harder to read... – Aconcagua Dec 23 '21 at 15:55
  • 1
    @AbhishekMane The asterisks are printed only at very first and very last loop run, in between only hyphens are printed – thus one could make the code much more efficient by skipping these tests entirely: `if(n) { putchar('*'); for(i = 2; i < n; ++i) { putchar('*'); for(j = 2; j < i; ++j) { putchar('-'); } putchar('*'); } if(n != 1) { for(i = 0; i < n; ++i) { putchar('*'); } }` – Aconcagua Dec 23 '21 at 16:06
  • @Aconcagua Noted regarding parentheses. By the way in your comment `for(j = 0; j <= n; ++j)` is `j<=n` by mistake or you use deliberately rather than `j<=i` ? – Abhishek Mane Dec 23 '21 at 16:06
  • @AbhishekMane Fixed that already... Note the indices in the loops, though: We need to start with 2 in both inner and outer loops as the single asterisk as well as the all asterisks lines are printed separately. Similarly the hyphens: Two values less because of the asterisks there being printed before and after the loop... – Aconcagua Dec 23 '21 at 16:12

0 Answers0