Please save the program with the name ‘triangle.c’
Write a nested for loop that prints the following output:
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
1 2 4 8 16 32 64 32 16 8 4 2 1
1 2 4 8 16 32 64 128 64 32 16 8 4 2 1
Hint: Here is the psudocode solution:
for row from 0 to 7 {
pad leading blanks in a row like this
for the column from 1 to 7-row:
print a set of spaces
print left half of row
for column from 0 to row:
print spaces followed by 2 raised to the power of the column
print right half of row
for column from row-1 to 0:
print spaces followed by 2 raised to the power of the column
print a new line
You need to figure out how many spaces to print before the number. The simple way to
compute powers of 2 is with the pow function; can you do this without it?

Respuesta :

Answer:

1. #include <stdio.h>

2. int main()

3. {

4.     int k;

5.     int j;

6.     int i;

7.     int array[7];

8.     array[0] = 1;

9.     for (i = 1; i < 9; ++i)

10.     {

11.        array[i] = array[i-1]*2;

12.        for (j=0; j < i; ++j)

13.        {

14.          printf("%d ",array[j]);

15.        }      

16.        for (k=i-2; k > -1; --k)

17.        {

18.          printf("%d ", array[k]);  

19.        }

20.        printf("\n");      

21.     }  

22.     return 0;

23. }

Explanation:

  • From line 1 to 3 we start the main function
  • From line 4 to 7 we declare the variables that we are going to be using throughout the program including an array of 7 positions
  • On line 8 we initialize the array with one to match the sequence
  • From line 9 to 10 we create a for loop to create the 9 sequences of numbers
  • On line 11 we assign values to the array by taking the previous value and multiplying it by 2, this way we can create the sequence 1,2,4,8,32...
  • From line 12 to 15 we print the ordered array
  • From line 16 to 19 we print the inverse array minus one  
  • On line 20 we print an enter

Ver imagen mateolara11

Answer:

On line 20 we print an enter

Explanation:

ACCESS MORE