user2985083
user2985083

Reputation: 59

Understanding my mistake with for-loop

I'm new to C and I'm trying to practice my knowledge by doing programs by my own without the use of the internet. I'm stuck with a small and probably dumb mistake but i can't seem to understand what it is. I'm trying to build an hour glass but before i do so i need to understand printing triangles. I'm really going step by step with this and I have tried to understand my mistake for very long with this. I have my code below and I'm trying to print j where i need j to print normally 12345 then 1234, 123 etc.. but when I use temp-- it skips by printing 123,1,1 and closes the program. Can somebody have a look at it and tell me what the problem with me second for loop?

   #include <stdio.h>

   int main()
{
    int i,j,k,num, temp=0;

    printf("Enter a number: ");
    scanf("%d", &num);

    temp=num;

    for (i=1; i<=num; i++) // Step1: Take care of the rows --> check
   {
    for (j=1; j<=temp; j++)
    {
        printf("%d", j);
        temp-=1;

    }
    printf("\n");
  }
}

Upvotes: -1

Views: 98

Answers (2)

Jonathan Leffler
Jonathan Leffler

Reputation: 753455

You both increment j and decrement temp which leads to odd effects. You probably don't want to decrement temp. Or you need to reset temp before the inner loop, rather than before the outer loop.

Or, indeed, you do not really need temp at all. Here are two variants of the code, one creating a triangle growing, and one creating a triangle shrinking. Neither needs temp (though t-dn.c could use a variable in place of the expression num - i + 1, but the compiler will probably handle that anyway — it is a basic optimization).

t-up.c

#include <stdio.h>

int main(void)
{
  int i,j,num;

  printf("Enter a number: ");
  scanf("%d", &num);

  for (i=1; i<=num; i++)
  {
    for (j=1; j<=i; j++)
      printf("%d", j);
    printf("\n");
  }
}

t-dn.c

#include <stdio.h>

int main(void)
{
  int i,j,num;

  printf("Enter a number: ");
  scanf("%d", &num);

  for (i=1; i<=num; i++)
  {
    for (j=1; j<=num-i+1; j++)
      printf("%d", j);
    printf("\n");
  }
}

Example output

$ ./t-up
Enter a number: 5
1
12
123
1234
12345
$ ./t-dn
Enter a number: 5
12345
1234
123
12
1
$

t-ok.c

Another variant, keeping the temp variable around:

#include <stdio.h>

int main(void)
{
  int i,j,num,temp;

  printf("Enter a number: ");
  scanf("%d", &num);

  temp=num;

  for (i=1; i<=num; i++)
  {
    for (j=1; j<=temp; j++)
      printf("%d", j);
    printf("\n");
    temp--;
  }
}

It produces the decreasing pyramid I believe you want. It places the decrement outside the inner loop.

Upvotes: 1

Kenny P.
Kenny P.

Reputation: 66

You're decrementing your j-loop condition within the j-loop. This means that every time you print a number, you also decrease the size of your loop (not at all what you want). To fix this, you need to move your temp-=1 from

for (i=1; i<=num; i++) // Step1: Take care of the rows --> check
{
for (j=1; j<=temp; j++)
{
    printf("%d", j);
    temp-=1;
}
printf("\n");
}

to

for (i=1; i<=num; i++) // Step1: Take care of the rows --> check
{
for (j=1; j<=temp; j++)
{
    printf("%d", j);
}
temp-=1;
printf("\n");
}

This decreases the size of your j-loop AFTER you've finished going through it, so you should wind up with the right amount of numbers.

You could even do away with your temporary variable by reversing the i-loop like such

for ( i = num ; i > 0 ; i-- ) {
    for ( j = 1 ; j <= i ; j++ ) {
        printf("%d", j);
    }
printf("\n");
}

for a simpler code.

Upvotes: 1

Related Questions