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 thefigure. 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;
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.
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;
---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.
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:
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?
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
}
}
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).
#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;
}
#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;
}