Hi all, i was having trouble solving this triangle loop , the printed triangle should look like this:
can anyone tell me the loops for it ?Code:1 212 32123 4321234 543212345 65432123456 7654321234567
Hi all, i was having trouble solving this triangle loop , the printed triangle should look like this:
can anyone tell me the loops for it ?Code:1 212 32123 4321234 543212345 65432123456 7654321234567
Sure:Code:int i, j; for (i = 1; i < 8; i++) for (j = i; j > 0; j++) for (j = 1; j < i; j++)
Last edited by Kennedy; 05-06-2010 at 11:31 AM. Reason: Left off a couple.
for the first loop u print \n rite ? and what do u print for the second and third loops ?
Personally, I'd write this puppy to a string, then I would right justify the string at printf time.
EDIT:
SHOW SOME CODE. . .
FOR CRYING OUT LOUD READ THE FORUM RULES!!!!
im not allowed to use include <string>
<string> is not a standard C header, so that is not a concern. You do not need to #include <string.h> to implement Kennedy's suggestion.Originally Posted by everyone0
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
i dont understand what a string is , the instructor didn't teach us that yet
No you don't.
#include <stdio.h>
is the ONLY thing you'll need to do this task. That and a couple of strings.
Strings are also known as arrays of char. If you don't know arrays yet, then it makes the project more interesting.
Please show some code so we may help you. Also, it is a good idea to post the EXACT project verbiage so we know what you are supposed to be doing.
mm k
here is the question:
3-Write a C program that displays a triangle of numbers in the form:
(the above form i already posted it)
here is my work so far:
this is printing something a bit close to what i want so im getting closer can anyone tell me what to do ?Code:#include <stdio.h> #include <conio.h> main() { int i,j; for(i=1;i<=7;i++) { for(j=1;j<=i;j++) { printf("%d\t",j); } printf("\n"); } getch(); }
Last edited by everyone0; 05-06-2010 at 12:09 PM.
Indentation!
1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
3. Get rid of conio.h and other antiquated DOS crap headers.
4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.
Okay, so you have it printing the RIGHT side of the lines. . . and, what's the \t for?
Start out by printing the LEFT side. Also, since you know how many positions this requires per line, and you know how many positions each data segment requires, you can put in a loop that prints spaces for the left hand side of the lines.
i dont know how do i print the left side , i know its supposed to be with using a space but i dont know how can u plz tell me ?
Think about the problem. Each line is how long? For each line, how long is the data? How many spaces should this be? How do I figure out the number of "real" data per line?
After you have these questions answered, ask yourself this: How would I put what I just computed on paper into code such that I do it in a loop?
can't u just tell me please ? , thats why im here to ask this question because my instructor is an ugly person who don't want to help anyone , not even with the simplest questions.