Help I'am stuck in C

I have an assingment for my school but i'm stuck in my own code, i'm still a beginner so not every function is clear to me. the assignment is as follows

Write an application that will output a triangle identical to Figure 1. The layout should be exactly like
in the figure. Stop generating the triangle when the line length exceeds 80 characters.

...........1
.........1 2 1
.......1 2 4 2 1
....1 2 4 8 4 2 1
remark the points are not part of the triangle


i have written 2 pices of code first:

#include <stdio.h>
#include <math.h>

int main()
{
int a;
int b;
int c;
int d;
int e;
int f;
int g;
int h;
int i;
int start = 1;

printf("%36d\n",start);//print de eerste 1regel

while (start < 512)
{
start = start*2;
if (start == 2)
{
a = start/2;
printf("%32d%4d%4d\n",a,start,a);// print de 2de regel
}
else if(start == 4)
{
b = a*2;
printf("%28d%4d%4d%4d%4d\n",a,b,start,b,a);// print de 3de regel
}
else if(start == 8)
{
c = b*2;
printf("%24d%4d%4d%4d%4d%4d%4d\n",a,b,c,start,c,b,a);// print de 4de regel
}
else if(start == 16)
{
d = c*2;
printf("%20d%4d%4d%4d%4d%4d%4d%4d%4d\n",a,b,c,d,start,d,c,b,a);// print de 5de regel
}
else if(start == 32)
{
e = d*2;
printf("%16d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d\n",a,b,c,d,e,start,e,d,c,b,a);// print de 6de regel
}
else if(start == 64)
{
f = e*2;
printf("%12d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d\n",a,b,c,d,e,f,start,f,e,d,c,b,a);// print de 7de regel
}
else if(start == 128)
{
g = f*2;
printf("%8d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d\n",a,b,c,d,e,f,g,start,g,f,e,d,c,b,a);// print de 8de regel
}
else if(start == 256)
{
h = g*2;
printf("%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d\n",a,b,c,d,e,f,g,h,start,h,g,f,e,d,c,b,a);// print de 9de regel
}
else if(start == 512)
{
i = h*2;
printf("%d%3d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d\n",a,b,c,d,e,f,g,h,i,start,i,h,g,f,e,d,c,b,a);// print de 10de regel
}
}

return 0;
}


or the next pice of code:


#include <stdio.h>
#include <math.h>

int main()
{
int start = 1;
int regel2;
int repeat=1;
int nieuw;
int delen;

if(start == 1)
{
//printf("\t\t");
printf("%40d\n",start);
}

while (repeat <= 256 )
{
repeat = repeat*2;
printf("%40d\n",repeat);

}
//loop maken tot rest 1 is en dan stoppen

while (repeat != 1)
{
repeat = repeat/2;
printf("%d\n",repeat);
}

int n,
reverse = 0;



while (repeat != 1)
{
reverse = reverse * 10;
reverse = reverse + repeat%10;
repeat = repeat/10;
}

printf("Reverse of entered number is = %d\n", repeat);

return 0;
}


for the first pice of code i can print the triangle as asked but i dont know the number of characters printed including spaces. so no go on the assignment this wil result in a F.

for the second pice of code i'm not getting anywhere in getting it in the right format or even counting the characters.

I need some guidance, who can help me ?

Greetings Vincent
To be clear i'm programming in C not in C++, i'm not allowed to use statements from C++.
*edit*
I see 2 ways to do it.

For each char printed, add count++;
if count++; is C++ code, then count=count+1;

you'll need a check

if (count >=80)

remember to set it back to zero when you finish a line.

another trick I do, is when I'm trying to line things up, it's nice in development stages to have a visual ruler so you don't have to try and count spaces each time you run it.

For your project, you might want to use something like this;

1
2
3
//           1         2         3         4         5         6         7         8
// 012345678901234567890123456789012345678901234567890123456789012345678901234567890
// 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 



---2nd way and what I would do---
Do the math, that's a joke...
What I mean is, each line you print will be a set of odd numbers
1 2 4 2 1 has 5 numbers, because it has 5 numbers, the spaces will be 4.
5+4 = 9
80 - 9 =71
71/2 = 35.5

to start printing 1 2 4 2 1 you should start with 35 spaces. The first printed number will be at location 36.

You wouldn't actually count to 80, so that may or may not work for this project's goals. You could put in a statement that would stop the program if what your printing and the spaces are greater than 80, then print the exit statement.

Last edited on
I'm just wondering, why would your first code print the correct triangle? I think this is the last line of the triangle according to how you posed the question:

1 2 4 8 16 32 64 128 256 512 1024 2048 1024 512 256 128 64 32 16 8 4 2 1


Since 2048 = 2^11, that would mean there should be 12 rows. You only have 10 rows for that code.

Also, you are printing them each with a width of 4. Is this how it's supposed to look like and you just couldn't show the proper format in your example?

                       1
                   1   2   1
               1   2   4   2   1
           1   2   4   8   4   2   1
       1   2   4   8  16   8   4   2   1
   1   2   4   8  16  32  16   8   4   2   1
My first code shows the correct format and spacing, your where correct in assuming i was not abel to schow the proper format in the example.

Samuel,

post increment isn't only C++ it is allowed in C, how would i implement your suggestion of counting the char printed?

because i have only type int used for my calculations, i will check if i can change the declaration of the type's without loosing my current function.

i have been programming on this the past four days so i'm getting a bit blind for even the most simpel remarks you guys make.
Guys,

I have changed the type from int to char and now the program will run until line 6 (64) before blocking.

the number of characters printed is 82 (if have done the math).
but it's not by my hand that the program ends.

here is the code:
#include <stdio.h>
#include <math.h>

int main()
{
char a;
char b;
char c;
char d;
char e;
char f;
char g;
char h;
char i;
char start = 1;

printf("%36d\n",start);//print de eerste 1regel

while (start < 512)
{
start = start*2;
if (start == 2)
{
a = start/2;
printf("%32d%4d%4d\n",a,start,a);// print de 2de regel
}
else if(start == 4)
{
b = a*2;
printf("%28d%4d%4d%4d%4d\n",a,b,start,b,a);// print de 3de regel
}
else if(start == 8)
{
c = b*2;
printf("%24d%4d%4d%4d%4d%4d%4d\n",a,b,c,start,c,b,a);// print de 4de regel
}
else if(start == 16)
{
d = c*2;
printf("%20d%4d%4d%4d%4d%4d%4d%4d%4d\n",a,b,c,d,start,d,c,b,a);// print de 5de regel
}
else if(start == 32)
{
e = d*2;
printf("%16d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d\n",a,b,c,d,e,start,e,d,c,b,a);// print de 6de regel
}
else if(start == 64)
{
f = e*2;
printf("%12d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d\n",a,b,c,d,e,f,start,f,e,d,c,b,a);// print de 7de regel
}
else if(start == 128)
{
g = f*2;
printf("%8d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d\n",a,b,c,d,e,f,g,start,g,f,e,d,c,b,a);// print de 8de regel
}
else if(start == 256)
{
h = g*2;
printf("%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d\n",a,b,c,d,e,f,g,h,start,h,g,f,e,d,c,b,a);// print de 9de regel
}
else if(start == 512)
{
i = h*2;
printf("%d%3d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d\n",a,b,c,d,e,f,g,h,i,start,i,h,g,f,e,d,c,b,a);// print de 10de regel
}
}

return 0;
}


If that is the proper format, I guess there really are only 10 rows. This is a very easy problem if you know each number takes up exactly 4 characters.

Let's say that 1 "slot" = 4 characters, so each number takes up 1 slot. Each row of the triangle is made up of x slots of spaces and y slots of numbers.

The first row of the triangle will have 9 slots of spaces. Every row, the number of slots will decrease by 1, until you have 0 slots of spaces at row 10. That means there are (10 - RowNumber) slots of spaces for each row.

As for the numbers, there is 1 number in row 1, 3 numbers in row 2, 5 numbers in row 3, etc. In other words there are (2 * RowNumber - 1) numbers in each row. The highest number in each row is 2 ^ (RowNumber - 1).

Now that we understand all that, it's easy to write the code. The way I would do it is to have 1 outer loop, and 3 inner loops.

The outer loop would run 10 times, once for each of the rows of the triangle. So it would look like this: for (int row = 0; row < 10; row++).

The first inner loop is going to run 9 - row times (since I started with row = 0 and not row = 1) and print 4 spaces each time it loops. for (int i = 0; i < 9 - row; i++)

The second loop is going to print the number, starting from number = 1, until number is 2 ^ row. Each time it loops, the number will double. for (int num = 1; num <= pow(2, row); num *= 2)

The third loop is going to print the number, starting from number = 2 ^ (row -1 ), until number = 1. Each time it loops, the number will halve. for (int num = pow(2, row - 1); num >= 1; num /= 2).
I'm getting there, i stil have a format problem with the output from the code here below, i have used the suggestion from fg109.

i'm going to try and get the aligment correct as wel as the number of lines in between.

any comments are welcome,

#include <stdio.h>
#include <math.h>

int main()
{

int row;
int i;
int num;

for (row = 0; row < 10; row++)
{
for (i = 0; i < 9 - row; i++)
printf("\n ");
{
for (num = 1; num <= pow(2, row); num *= 2)
printf("%d",num);
{
for (num = pow(2, row - 1); num >= 1; num /= 2)
printf("%d",num);
}
}
}


return 0;
}
i have tried to solve the formating problem but i'm not getting closer than this.

#include <stdio.h>
#include <math.h>

int main()
{

int row;
int i;
int num;

for (row = 0; row < 10; row++)
{
for (i = 0; i < 9 - row; i++)
printf("\n ");
{
for (num = 1; num <= pow(2, row); num *= 2)
printf("%4d",num);
{
for (num = pow(2, row - 1); num >= 1; num /= 2)
printf("%4d",num);
}
}
}



return 0;
}
Last edited on
What am i not seeing ? did i write the code wrong ?

Any more help is greatly valued.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>
#include <math.h>

int main()
{
    for (int row = 0; row < 10; row++)
    {
        for (int i = 0; i < 9 - row; i++)
            printf("    ");
        for (int num = 1; num <= pow(2, row); num *= 2)
            printf("%4d", num);
        for (int num = pow(2, row - 1); num >= 1; num /= 2)
            printf("%4d", num);
        printf("\n");
    }
    
    return 0;
}
I needed to make an small adjustment, because my compiler does not accept declaring integers into the function.

thank you very much for your help,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

#include <stdio.h>
#include <math.h>

int main()
{
    int row;
    int i;
    int num;

    for ( row = 0; row < 10; row++)
    {
        for ( i = 0; i < 9 - row; i++)
            printf("    ");
        for ( num = 1; num <= pow(2, row); num *= 2)
            printf("%4d", num);
        for ( num = pow(2, row - 1); num >= 1; num /= 2)
            printf("%4d", num);
        printf("\n");
    }

    return 0;
}
Last edited on
Topic archived. No new replies allowed.