# Help me making a weird triangle ( this question never been on this forum)

• 11-09-2005
haniunited
Help me making a weird triangle ( this question never been on this forum)
The users enters a number
which will spicify the number of rows and the value of the middle number

like
lets say the user entered the number '7'
the shape will be like this

Code:

```                        7                                12 spaces from the left                     6_7_6                            10 spaces from the left                 5_6_7_6_5                        8 spaces from the left             4_5_6_7_6_5_4                    6 spaces from the left         3_4_5_6_7_6_5_4_3                4 spaces from the left     2_3_4_5_6_7_6_5_4_3_2            2 spaces from the left 1_2_3_4_5_6_7_6_5_4_3_2_1        0 spaces from the left```
Note: the symbol '_' means space

we must use 'for' functions three or two ones
• 11-09-2005
Harbinger
So what - can't you make a loop which counts from 7 down to 1 ?
• 11-09-2005
haniunited
is it shame that i dont know? :(

i just learned the 'for' tag myslef
i bought a book

i dont get it how to solve these questions they give

this was part of the question

if the number is even. we scan a number and print print this triangle

i solved the question i still need this part?
anyone can help ? :confused:
• 11-09-2005
Harbinger
Well if
i = i + 1
count's forwards, do you imagine that
i = i - 1
might count backwards?
• 11-09-2005
haniunited
look, my book is in spanish
am not good in english
need some help there
• 11-09-2005
hk_mp5kpdw
One possible way to start off (assume user entered their input in a var called 'choice'):

Code:

```int i; for( i = 0; i < choice; ++i ) {     /* You will need another loop (or two) in here to do the rest.       Start perhaps by printing out (6-i)*2 leading spaces using a       loop... first time through, 'i' is 0 so we print (6-0)*2 = 12       spaces; second time through, 'i' is 1 so we print (6-1)*2 = 10       spaces.  Or, you can do this other ways.  You might then use       another loop to print the numbers... there are different ways       to go about doing this. */ }```
Just an example of how you might start.
• 11-09-2005
haniunited
thanks very much
even i did not understand almost anything

but i will try
• 11-09-2005
haniunited
any1 knows ?
• 11-09-2005
quzah
Yes. Lots of us know the answer. Here, let me let you in on a few "secrets":
Quote:

Originally Posted by kermi3
Welcome to the boards. If you haven't already done so then please take some time to familiarise yourself with the faq:
http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Make sure you have a look at the the posting guidelines:
http://cboard.cprogramming.com/annou...ouncementid=51
Following the rules will ensure you get a prompt answer to your question.

Remember, too, that the board has a search facility, a link is at the top of your screen:
http://cboard.cprogramming.com/search.php
It will often get you a quicker answer to your questions than waiting for a response to one you have posted.

It appears that you are posting a homework assignment or other project.

Please don't ask people to do all your work for you, See the announcement on Homework at the top of all forums to see what is acceptable or PM me. Basically people are happy to help, but they're not going to do it all for you.

Show us what you've got, show your code (using code tags), or where you're confused and someone will be happy to help you I'm sure. If it's something that you absolutely don't understand how it works, like you have no clue how qsort works, then ask a general question about the function and I'm sure someone will explain it. Though they may not give you all of the code for it, but someone will explain the concept.

On obivous homework questions especially, I like to remind people of the board's ninth guildeline, while this board is very helpful to people, make sure you have your instructor's permission before seeking help on assignments. While people on these boards are more than happy to help, we discourage people from asking for help on graded work without the instructor's permission, and we claim no repsonsibilty for any cheating or honor violations.

Feel free to PM me with any questions.

Good Luck,

Kermi3

Quzah.
• 11-09-2005
haniunited
man u all missed understanding me
i am thinking
i just not reaching the correct thing

here is what i arrived until now
plaese any1 post the right code if its not

Code:

```#include<stdio.h> void main () { int i,j,t,n; printf("Please enter a number\n"); scanf("%d",&n); x=(n-i)*2 for(i=1;i<=n;i++) { for(j=1;j<=n;j++) printf(" "); for(t=1;t<n;t++) printf("%d,t); for(t=n;t>=1;t--) { if(t!=1) printf("SPACE HERE THEN %d",n); else printf(%d"t) { printf("\t"); } }```
• 11-09-2005
spydoor
This much was already given to you in pseudo code...
There is still some work left to do
Code:

```#include<stdio.h> int main() {     int i,j,number;     printf("Please enter a number: ");     scanf("%d",&number);     for(i=number; i > 0; i--) {         /* print leading spaces */         for(j=0; j < (i-1) * 2; j++) {             putchar(' ');         }         /* need a loop or 2 loops or something here */         printf("%d \n", i);     }     return 0; }```
• 11-09-2005
haniunited
am sorry i dont get it

this is only my 2nd day programming
• 11-09-2005
spydoor
did you even, compile, run and try to understand the code?

considering the time between posts I doubt it.

I'll try to explain what's going on:
the outer loop simply counts down from the entered number to 1

if you remove the inner loop you'll see it prints a count down

the next step is to print the leading spaces for each row. so we add a loop that prints from 0 to (i-1) * 2 spaces. where i is the current row.

all that's left is to print the rest of the triangle.

how about instead of just a printf you add a loop that prints from i (the current row) to number. something like
Code:

```for(k=i; k <= number; k++) {   printf("%d ", k); } printf("\n");```

now all that's left is to print the right half of the triangle. (another loop before the newline)
• 11-09-2005
haniunited
any1?
• 11-09-2005
Dave_Sinkula
```#include<stdio.h> void foo(int target) {   int i, j;   for ( i = 0; i < target; ++i )   {       /*       * Leading spaces:       */       for ( j = 1; j < target - i; ++j )       {         printf("  "); /* two spaces */       }       /*       * Initial sequence:       */       for ( /* leave j where it was, at (target - i) */; j <= target; ++j )       {         printf("%d ", j);       }       /* more code */       putchar('\n');   } } int main() {   foo(7);   return 0; } /* my output             7           6 7         5 6 7       4 5 6 7     3 4 5 6 7   2 3 4 5 6 7 1 2 3 4 5 6 7 */```