nested for loop triangle in c

i need an explanation on a triangle made by nested loop i'm trying

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 21399 Views - Last Post: 26 February 2010 - 01:57 PM Rate Topic: -----

#1 aleeza  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 26-February 10

nested for loop triangle in c

Posted 26 February 2010 - 10:49 AM

actually i am novice in c and c++ and i start using for loop and seeking an output like this:
54321
.4321
..321
...21
....1

#include<stdio.h>
#include<conio.h>
void main(void)
{
for(int i=5;i>=1;i--)
{
for(int j=5;j<=i-1;j--)
{
printf("%d",j);
printf("\n");
}
}
getch();
}



so far i've come up with this code em kind ov confused in j<=i-1 this area need help an explanation will b highly appreciated :)
tiger007

This post has been edited by NickDMax: 26 February 2010 - 12:02 PM
Reason for edit:: fixed formatting


Is This A Good Question/Topic? 0
  • +

Replies To: nested for loop triangle in c

#2 no2pencil  Icon User is online

  • Dic Head
  • member icon

Reputation: 5174
  • View blog
  • Posts: 26,866
  • Joined: 10-May 07

Re: nested for loop triangle in c

Posted 26 February 2010 - 11:30 AM

This code creates the following output :

#include <stdio.h>

#define MAX 6

int main(void) {
  char str[6]="54321";
  int i=0,j=5;
  for(i=MAX;i>=0;i--) {
    for(j=0;j<=i;j++) {
      printf("%c",str[j]);
    }
    printf("\n");
  }
  return 0;
}



Quote

~/code/c $ ./output
54321
54321
54321
5432
543
54
5


It's close to what you are looking for :)
Was This Post Helpful? 1
  • +
  • -

#3 Ferencn  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 71
  • View blog
  • Posts: 322
  • Joined: 01-February 10

Re: nested for loop triangle in c

Posted 26 February 2010 - 11:35 AM

Do you mean an output like this:
54321
 4321
  321
   21
    1

or this:
54321
4321
321
21
1

This post has been edited by Ferencn: 26 February 2010 - 11:36 AM

Was This Post Helpful? 0
  • +
  • -

#4 Ferencn  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 71
  • View blog
  • Posts: 322
  • Joined: 01-February 10

Re: nested for loop triangle in c

Posted 26 February 2010 - 11:42 AM

void main(void)
{
for(int i=5;i>=1;i--)
{
for(int j=5;j<=i-1;j--) // <--- j will never be <= i-1, so this for loop will never execute!
                        // i starts at 5 and decrements, so i-1 starts at 4 and decrements.
                        // j is starting at 5, so j will always be bigger than i-1!!!  
{
printf("%d",j);
printf("\n");
}
}
getch();
}


Was This Post Helpful? 0
  • +
  • -

#5 aleeza  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 26-February 10

Re: nested for loop triangle in c

Posted 26 February 2010 - 12:24 PM

actually i need an output like this
54321
.4321
..321
...21
....1


yeah

This post has been edited by NickDMax: 26 February 2010 - 12:34 PM
Reason for edit:: moderator to the rescue!

Was This Post Helpful? 0
  • +
  • -

#6 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: nested for loop triangle in c

Posted 26 February 2010 - 12:32 PM

aleeza, our forums compress spaces and remove any leading spaces when (actually it is HTML that does this, but that is another topic).

To get formatted text to display correctly you can use code tags: [code]... your text/code here... [/code]

Or you can get creative with some of the other BBCode tags available.

For example I to format your post above I used the technique I talked about here on my blog
Was This Post Helpful? 0
  • +
  • -

#7 aleeza  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 26-February 10

Re: nested for loop triangle in c

Posted 26 February 2010 - 12:34 PM

so can i thought this decrement logic will work
can u pleeeez suggest and explain me this...
:)
Was This Post Helpful? 0
  • +
  • -

#8 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: nested for loop triangle in c

Posted 26 February 2010 - 12:41 PM

Here is an example that is *almost* what you need. I intentionally left it off a little so that you can do *something*

#include<stdio.h>

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


here the output is:
12345
.1234
..123
...12
....1


not quite what you need but you should be able to see how to finish it up.
Was This Post Helpful? 1
  • +
  • -

#9 no2pencil  Icon User is online

  • Dic Head
  • member icon

Reputation: 5174
  • View blog
  • Posts: 26,866
  • Joined: 10-May 07

Re: nested for loop triangle in c

Posted 26 February 2010 - 12:44 PM

I like using the string array :(
Was This Post Helpful? 0
  • +
  • -

#10 Ferencn  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 71
  • View blog
  • Posts: 322
  • Joined: 01-February 10

Re: nested for loop triangle in c

Posted 26 February 2010 - 12:51 PM

What you want to do is:
starting at 5, for trianglesize 5:
5: 0 spaces, then 5,4,3,2,1
4: 1 space, then 4,3,2,1
3: 2 spaces, then 3,2,1
2: 3 spaces, then 2,1
1: 4 spaces, then 1
As you can see, the number of spaces increases, while the number of digits decreases.
so you want to print # of digits lines, decreasing, each with the number of spaces increasing:

for (int numdigits = 5;numdigits >0;numdigits --)
{
   // each line we want to print 5-numdigits spaces!
   for (int spaces = 0;spaces < (5-numdigits); spaces++)
      printf(" ");
   // now print the digits themselves, starting at the highest (numdigits) and ending with 1:
   for (int outdigit=numdigits; outdigit>0;outdigit--)
      printf("%d",outdigit);
   // finally a newline, so the next line starts at the beginning:
   printf("\n");
}
// DONE!



View Postno2pencil, on 26 February 2010 - 07:44 PM, said:

I like using the string array :(

Your stringarray approach is very nice as well! especially if you want to print something like:
pencil
 encil
  ncil
   cil
    il
     l

instead of sequential numbers or characters!

This post has been edited by Ferencn: 26 February 2010 - 12:49 PM

Was This Post Helpful? 0
  • +
  • -

#11 no2pencil  Icon User is online

  • Dic Head
  • member icon

Reputation: 5174
  • View blog
  • Posts: 26,866
  • Joined: 10-May 07

Re: nested for loop triangle in c

Posted 26 February 2010 - 12:54 PM

Yeah, through the loop you could just set the array element to space, rather than fussing with another loop & variable counter.
Was This Post Helpful? 0
  • +
  • -

#12 Ferencn  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 71
  • View blog
  • Posts: 322
  • Joined: 01-February 10

Re: nested for loop triangle in c

Posted 26 February 2010 - 12:58 PM

View Postno2pencil, on 26 February 2010 - 07:54 PM, said:

Yeah, through the loop you could just set the array element to space, rather than fussing with another loop & variable counter.

Thus proving that there is no 'best' algorithm :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#13 aleeza  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 26-February 10

Re: nested for loop triangle in c

Posted 26 February 2010 - 12:58 PM

yeah thanxxx alot n i got a little bit
i need 2 ask u something the second loop which is
for(j=5-i;j>0;--j)
is for the decrement of spaces am i right???
and for(j=1;j<i+1;++j)
is for printing j from 1 to 5 right??

This post has been edited by aleeza: 26 February 2010 - 01:02 PM

Was This Post Helpful? 0
  • +
  • -

#14 Ferencn  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 71
  • View blog
  • Posts: 322
  • Joined: 01-February 10

Re: nested for loop triangle in c

Posted 26 February 2010 - 01:05 PM

Correct. But, for the next line it will print one more space and then 1 to 4... etc.

This post has been edited by Ferencn: 26 February 2010 - 01:07 PM

Was This Post Helpful? 0
  • +
  • -

#15 aleeza  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 26-February 10

Re: nested for loop triangle in c

Posted 26 February 2010 - 01:22 PM

View PostFerencn, on 26 February 2010 - 12:05 PM, said:

Correct. But, for the next line it will print one more space and then 1 to 4... etc.




print one more space???
sorrryyy bt didn get u little more explanation pleeez :)

View PostNickDMax, on 26 February 2010 - 11:41 AM, said:

Here is an example that is *almost* what you need. I intentionally left it off a little so that you can do *something*

#include<stdio.h>

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


here the output is:
12345
.1234
..123
...12
....1


not quite what you need but you should be able to see how to finish it up.



THANX:)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2