Why isn't there a space in the middle of my pyramid? - c

So I am trying to solve https://cs50.harvard.edu/x/2021/psets/1/mario/more/ this problem set from cs50.
It's taken me way too long to get this far (5-6 hours WITH help from youtube).
#include <cs50.h>
#include <stdio.h>
int get_positive_int(void);
int main(void)
{
int n = get_positive_int();
// Run a below loop until i is greater than n
for(int i = 0; i < n; i++)
{
// Run if statement on the same row until j is greater than n*2 + i+1
for(int j=0; j < n*2 + i+1; j++)
{
// Create a blank space in exact center of the pyramid
if(j + i - n == 0 || j + i - n == 1)
printf(" ");
//If the value of n-1 is less than the combined value of j and i, or
//if the value of i and n minus j is less than or equal to zero print #
else if ( j + i > n - 1 || i + n - j <= 0)
printf("#");
else
//otherwise print " "
printf(" ");
}
printf("\n");
}
}
Why doesn't the below create a two " " in the center of my pyramid?
if(j + i - n == 0 || j + i - n == 1)
printf(" ");
And why when I choose a value of 1 for n does the program not print anything?
What is this specific type of thinking/math called? I am having difficulty grasping it and I am not sure how to find information online because I don't know what it is called.
My output is below.
When N = 8
#######
#########
###########
#############
###############
#################
###################
#####################
The output I want is.
When N = 8
# #
## ##
### ###
#### ####
##### #####
###### ######
####### #######
######## ########
When I input a value of 1 I get.
What I want when I input 1 is,
# #
I'd like to know what specifically I did wrong in order to fix the issue myself, rather than the direct answer if possible.

First of all observe that your two for loops both start from 0. Thus at the beginning both i and j are equal to 0. Therefore, since n is a positive integer, both your if and else if clauses are evaluated as false and only the final else gets executed, which is printf(" ");
Now look at your pyramid. Your "pyramid" fills a space with n rows and 2*n + 2 columns. The first loop for (i = 0; i < n; i++) is for the rows, and it's correct, since there are n rows. Your second for loop, however, is faulty. It should be for (int j=0; j < n*2 + 2; j++).
Now when we look at the the i-th row we see that (remembering rows start from 0 because we started counting i from 0), you want to print n-1-i spaces, followed by i+1 # characters followed by 2 spaces, followed by i+1 # characters and finally followed by n-1-i spaces. So the "spaces" are when j < n-1-i (remember j also starts from 0), when j == n and j == n+1 and when j > n+2+i
This can be written as follows using if-else:
if ( j < (n-1-i) || j == n || j == n+1 || j > n+2+i) printf(" ");
else printf("#");
So the final code will look like:
#include <cs50.h>
#include <stdio.h>
int get_positive_int(void);
int main(void)
{
int n = get_positive_int();
for(int i = 0; i < n; i++)
{
for(int j=0; j < n*2 + 2; j++)
{
if ( j < (n-1-i) || j == n || j == n+1 || j > n+2+i) printf(" ");
else printf("#");
}
printf("\n");
}
}

Think about this mathematically. You have slope of -1, so we have j = -i + k. Our formula becomes k = i + j. If we have n = 5:
i j k
0 4 4
1 3 4
2 2 4
3 1 4
5 0 4
n = 6
i j k
0 5 5
1 4 5
2 3 5
3 2 5
4 1 5
5 0 5
so k = n - 1
That is first half, second is even easier, j = i + k, so k = j - i. It is positive slope moved by k. So for n=5
i j k
0 4 4
1 5 4
2 6 4
3 7 4
5 8 4
n = 6
i j k
0 5 5
1 6 5
2 7 5
3 8 5
4 9 5
5 10 5
look at that we have a match in both cases k = n - 1. So our formulas are
j = -i + n - 1 and j = i + n - 1.
Second in both cases we have maximum j at max(i) + k or in other words n - 1 + n - 1 so 2 * n - 2
so:
We got your lines.
Look closes and you will see how far your j has to go.
Here is the code:
// int get_positive_int(void);
int main(void)
{
int n = 5; //get_positive_int();
// Run a below loop until i is greater than n
for(int i = 0; i < n; i++)
{
// Run if statement on the same row until j is greater than n*2 + i+1
for(int j = 0; j < n * 2 - 1; j++)
{
// Create a blank space in exact center of the pyramid
if (j == n - i - 1 || j == n + i - 1)
printf("#");
else
//otherwise print " "
printf(".");
}
printf("\n");
}
}
Output:
....#....
...#.#...
..#...#..
.#.....#.
#.......#
Now you can optimize this to trim down number of inner loop execution by 25%, by looking at this. I do not need to go beyond the point where I print my second #, meaning beyond max(j) = j + 1 value of my second formula + 1 is because I still want to be able to go into loop, so we get maximum max(j) = (n + i - 1) + 1.
#include <stdio.h>
// int get_positive_int(void);
int main(void)
{
int n = 5; //get_positive_int();
// Run a below loop until i is greater than n
for(int i = 0; i < n; i++)
{
// Run if statement on the same row until j is greater than n*2 + i+1
for(int j = 0; j < n + i; j++)
{
// Create a blank space in exact center of the pyramid
if (j == n - i - 1 || j == n + i - 1)
printf("#");
else
//otherwise print " "
printf(".");
}
printf("\n");
}
}
output:
....#
...#.#
..#...#
.#.....#
#.......#
And if you notice, by thinking mathematically about this you save a lot of operations. For example in second for() loop, OP and other answer have more than 1 operations, where this has 1, in if/else both have more than 2 equality statements having, this one has 2.
NOTE: I used . instead of for demonstration.

Related

How to calculate the number of triangles that can be formed

The code below should have counted the number of triangles that can be formed out of every triplet of 3 distinct integers from the given range 1...N. However, when I input 5, it gives me 34, while the right answer is 3: the only possible triangles are (2, 3, 4), (2, 4, 5) and (3, 4, 5).
// C code to count the number of possible triangles using
#include <stdio.h>
int main()
{ int N, count=0;
setvbuf(stdout, NULL, _IONBF, 0);
printf("Please input the value of N: \n");
scanf("%d", &N );
for (int i = 1; i < N; i++) {
for (int j = 1; j < N; j++) {
// The innermost loop checks for the triangle
// property
for (int k = 1; k < N; k++) {
// Sum of two sides is greater than the
// third
if (i + j > k && i + k > j && k + j > i)
{
count++;
}
}
}
}
printf ("Total number of triangles possible is %d ",count);
return 0;
}
You do not ensure that the numbers are distinct.
You can do this be chosing your loop limits correctly:
for (int i = 1; i <= N-2; i++) {
for (int j = i+1; j <= N-1; j++) {
for (int k = j+1; k <= N; k++) {
Start each inner loop one higher than current counter of outer loop. It also does not make any sense to run each loop up to N. If they must be distinct, you can stop at N-2, N-1, N
This creates triples where numbers are increasing.
If you consider triangles (3,4,5) and (4,3,5) to be different, we must also account for permuations of these triples.
As all values are distinct, we have 6 possible permutations for each triple that was found in the inner loop.
I'm sorry, I can't go for a comment so let's go for an answer.
I don't really get what you wish to do. As I am understanding it, you wish to print this :
1, 2, 3, 4, 5-> [2, 3, 4], [2, 4, 5], [3, 4, 5] -> 3
Except, with your code, you'll never check your N since you go out of your loop when i turns into N.
Also, your "j" and "k" don't have to move starting 1 since you already tried that position with "i", so you'll only get doublons doing that.
EDIT : some changes for a smarter code (I removed my +1 but go check for "<=", which I personnaly dislike :) ):
// since [1, 2, 3] can't bring any triangle
if (N < 4) return 0;
// since there is no possible triangle with 1 as a border, start at 2
for (int i = 2; i <= N-2; i++) {
for (int j = i+1; j <= N-1; j++) {
// The innermost loop checks for the triangle
// property
for (int k = j+1; k <= N; k++) {
// Sum of two sides is greater than the
// third
// simplified as suggested by S M Samnoon Abrar
if (i + j > k)
{
count++;
}
}
}
You need to do the following:
run first loop through 1 to N, i.e.: 1 <= i <= N
don't start each nested loop from index 1. So, you need to run first nested loop in range i+1 <= j <= N and second nested loop in range j+1 <= k <=N.
Explanation
First, if you run all 3 loops from 1 to N, then you are not doing distinct counting because all numbers in the range will be iterated 3 times. So it would give an incorrect result.
Secondly, since we need to count distinct numbers only, it is efficient to count +1 from the previous outer loop each time. In this way, we are ensuring that we are not iterating over any number twice.
Check the following code:
// C code to count the number of possible triangles using
#include <stdio.h>
int main()
{ int N, count=0;
setvbuf(stdout, NULL, _IONBF, 0);
printf("Please input the value of N: \n");
scanf("%d", &N );
for (int i = 1; i <= N; i++) {
for (int j = i+1; j <= N; j++) {
// The innermost loop checks for the triangle
// property
for (int k = j+1; k <= N; k++) {
// Sum of two sides is greater than the
// third
if (i + j > k && i + k > j && k + j > i)
{
count++;
}
}
}
}
printf ("Total number of triangles possible is %d ",count);
return 0;
}
Spot the extra line of code that enforces the constraint that the 3 numbers are "distinct" (read "unique"). Funny what a little "print debugging" can turn up...
printf("Please input the value of N: ");
scanf("%d", &N );
for (int i = 1; i < N; i++) {
for (int j = 1; j < N; j++) {
for (int k = 1; k < N; k++) {
if (i + j > k && i + k > j && k + j > i) {
if( i != j && j != k && k != i ) {
printf( "%d %d %d\n", i, j, k );
count++;
}
}
}
}
}
printf ("Total number of triangles possible is %d ",count);
Output
Please input the value of N: 5
2 3 4
2 4 3
3 2 4
3 4 2
4 2 3
4 3 2
Total number of triangles possible is 6
The OP code was counting (1,1,1) or (2,3,3) in contravention of "distinct" digits.
AND, there is now ambiguity from the OP person as to whether, for instance, (4,2,3) and (4,3,2) are distinct.
printf() - the coder's friend when things don't make sense...

Printing A Pattern in C

I am a beginner in programming, I studied all about C, I started to solve problems from hackerrank.com, there I faced a problem to print a pattern like given below
(the output of problem program):
4 4 4 4 4 4 4
4 3 3 3 3 3 4
4 3 2 2 2 3 4
4 3 2 1 2 3 4
4 3 2 2 2 3 4
4 3 3 3 3 3 4
4 4 4 4 4 4 4
the input will be an integer which will provide the data for the length of pattern square, here it is 4 in image,
I tried a lot to type a proper logic and I end up with this useless code bellow:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main()
{
int n;
scanf("%d", &n);
int array[n- 1 + n][n - 1 + n];
array[(n - 1 + n) / 2][(n - 1 + n) / 2] = 1;
int f[n];
for(int i = 0; i < n; i++) {
f[i] = i;
}
for(int i = 0; i < n - 1 + n; i++) {
for(int j = 0; j < n - 1 + n; j++) {
array[j][i] = n - f[j]; //top
array[i][j] = n - f[j]; //left
array[(2 * n - 1 - 1) - i][j] = n - f[i]; //bottem
array[j][(2 * n - 1 - 1) - i] = n - f[i]; //rigth
}
}
for(int i = 0; i < n - 1 + n; i++) {
for(int j = 0; j < n - 1 + n; j++) {
printf("%d ", array[i][j]);
}
printf("\n");
}
return 0;
}
my logic was to make all four borders correct in for loop which will end at center, but its not working, I want a new logic or to improve my logic, if you want to help me out then please give me the way to solve problem instead of giving me a direct code.
It is observable that the pattern consists of n stacked squares:
Square #0 is drawn with ns.
Square #1 is drawn with n-1s.
...
Square #n-1 is drawn with 1s.
Implementing the above:
void draw_pattern(const size_t n)
{
const size_t dim = 2*n-1;
int array[dim][dim];
for (size_t i = 0; i < n; ++i) { // Outer square #i
// Row #0 of the outer square #i
for (size_t j = i; j < dim-i; ++j)
array[i][j] = n-i;
// Row #n-1 of the outer square #i
for (size_t j = i; j < dim-i; ++j)
array[dim-i-1][j] = n-i;
// Col #0 of the outer square #i
for (size_t j = i; j < dim-i; ++j)
array[j][i] = n-i;
// Col #n-1 of the outer square #i
for (size_t j = i; j < dim-i; ++j)
array[j][dim-i-1] = n-i;
}
print_array(dim, array);
}
This is print_array():
void print_array(const size_t dim, int array[dim][dim])
{
for (size_t i = 0; i < dim; ++i) {
for(size_t j = 0; j < dim; ++j)
printf("%d ", array[i][j]);
printf("\n");
}
}
Output:
4 4 4 4 4 4 4
4 3 3 3 3 3 4
4 3 2 2 2 3 4
4 3 2 1 2 3 4
4 3 2 2 2 3 4
4 3 3 3 3 3 4
4 4 4 4 4 4 4
The worst case time complexity is O(n2).
When you get a problem like this, try to dumb it down as much a possible. This square can be separated into 8 same, just rotated "slices" that look like:
4 | 4444 | 4444 | 4
43 | 333 | 333 | 34
432 | 22 | 22 | 234
4321 | 1 | 1 | 1234
... and the same for the bottom half, just flipped.
You can see this in the code bellow, to check what line is writing what part of the square, comment it and you will see what section shows zeroes.
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n;
scanf("%d", &n);
int array[2 * n - 1][2 * n - 1];
for(int i = 0; i < 2 * n - 1; i++){
for(int j = 0; j < 2 * n - 1; j++){
array[i][j] = 0;
}
}
int f[n];
for(int i = 0; i < n; i++)
{
f[i] = i;
}
for(int i = 0; i < n; i++)
{
for(int j = i; j < n; j++)
{
array[i][j] = n - i;
array[j][i] = n - i;//top left
array[j][2*n - i - 2] = n - i;
array[i][2*n - j - 2] = n - i;//bottom left
array[2*n - j - 2][i] = n - i;
array[2*n - i - 2][j] = n - i;//top right
array[2*n - i - 2][2*n - j - 2] = n - i;
array[2*n - j - 2][2*n - i - 2] = n - i;//bottom right
}
}
for(int i = 0; i < n - 1 + n; i++)
{
for(int j = 0; j < n - 1 + n; j++)
{
printf("%d ", array[i][j]);
}
printf("\n");
}
return 0;
}

How could I align my output to the center?

#include<stdio.h>
int main()
{
int n ;
printf("Input the number of rows: ");
scanf("%d", &n);
for(int i = 1; i <= n ; i++)
{
for(int j = 1 ; j <= i ; j++)
{
if(j == i + 1)
{
break;
}
printf("%3d", j);
}
for(int j = i - 1 ; j > 0; j--)
{
if(j == 0)
{
break;
}
printf("%3d", j);
}
printf("\n");
}
}
Program session
Number of rows for this output n = 3
My output:
1
1 2 1
1 2 3 2 1
Preferred output:
1
1 2 1
1 2 3 2 1
This here is an exercise where I have to print a pyramid of numbers where the central number of the pyramid is the number of the row. I understand the logic but as you can see I have not been able to fulfill the task successfully. Any tips?
As pointed out by #WeatherWane, you need to add logic to add extra spaces. If you notice carefully, number of spaces on each line(excluding padding you add with %3d is equal to 3 * (n - i)). You can create a simple method like this to add spacing:
void addSpaces(int N, int currentIndex, int padding) {
for (int index = currentIndex; index < N; index++)
for (int spaces = 0; spaces < padding; spaces++)
printf(" ");
}
then you can call it from your first for loop like this:
for(int i = 1; i <= n ; i++)
{
addSpaces(n, i, 3);
for(int j = 1 ; j <= i ; j++)
{
// ...
I tested it and it seems to align it correctly:
c-posts : $ ./a.out
Input the number of rows: 5
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1

C Pattern Code: Why this code for printing Star-Pyramid is not working? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I am not able to figure out why below code for printing Star-Pyramid is not giving required output? Can anyone tell me where I go wrong?
Code:
#include <stdio.h>
void main(void)
{
int n, i, j;
printf("Input height: ");
scanf("%d", &n);
for (i = 1; i <= n; i++);
{
for (j = 1; j <= 2 * n - 1; j++)
{
if ((j >= n - i + 1) && (j <= n + i - 1))
{
printf("*");
}
else
{
printf("");
}
}
printf("\n");
}
}
Output:
Input height:
After specifying height, e.g., 5.
Input height: 5
Console Output:
Input height: 5
*********
Expected Output:
*
***
*****
*******
*********
Your code with comments:
#include <stdio.h>
int main(void) // main returns int. always *)
{
int n, i, j;
printf("Input height: ");
scanf("%d", &n); // lets assume we input 5
for (i = 1; i <= n; i++); // i gets incremented until it is greater than n --> i = 6, n = 5
// following lines not indented cause not part of for-loop above.
{ // <-- just a new block beginning, nothing special
for (j = 1; j <= 2 * n - 1; j++) // will increment j till it reaches 2*n-1 = 9 with n = 5
{
// n - i = -1, -1 + 1 = 0, j starts at 1 --> (j >= n - i + 1) always true.
// n + i = 11, 11 + 1 = 12, j runs till 9 --> (j <= n + i - 1) always true.
if ((j >= n - i + 1) && (j <= n + i - 1))
{
printf("*"); // will run 9 times
}
else
{
printf(""); // never.
}
}
printf("\n");
}
}
*) Except on freestanding environments, which should be of no concern for you in the near feature.
Short version:
#include <stdio.h>
int main(void)
{
int height = 0;
printf("Input height: ");
scanf("%d", &height);
for (int h = height; h > 0; --h) {
for (int i = 1; i <= 2 * height - h; ++i) {
putchar("* "[i < h]);
}
putchar('\n');
}
}
Explanation:
height = 8
h = height ... 1 2 x
h - 1 height - h
h = 8: 1234567* 7 spaces, 1 star, 8 total = 2 * 8 - 8
h = 7: 123456*** 6 spaces, 3 stars, 9 total = 2 * 8 - 7
h = 6: 12345***** 5 spaces, 5 stars, 10 total = 2 * 8 - 6
h = 5: 1234******* 4 spaces, 7 stars, 11 total = 2 * 8 - 5
h = 4: 123********* 3 spaces, 9 stars, 12 total = 2 * 8 - 4
h = 3: 12*********** 2 spaces, 11 stars, 13 total = 2 * 8 - 3
h = 2: 1************* 1 space, 13 stars, 14 total = 2 * 8 - 2
h = 1: *************** 0 spaces, 15 stars, 15 total = 2 * 8 - 1
As you see the count of spaces is simply h - 1.
The total number of characters for each line is 2 * height - h.
So in the inner for loop, we count from 1 to 2 * height - h ... the total number of chars. For i < h we print spaces, for higher values of i we print stars.
"* "[i < h] could also be written as i < h ? ' ' : '*'
or even more verbose:
if(i < h) {
putchar(' ');
} else {
putchar('*');
}
Clean solution for pattern printing
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
for (int j = n - i - 1; j > 0; j--) {
printf(" ");
}
for (int k = 0; k <= i; k++) {
printf("*");
}
for (int l = 0; l < i; l++) {
printf("*");
}
printf("\n");
}
return 0;
}
Input:
6
Output:
*
***
*****
*******
*********
***********

How to read nested for-loops?

I cannot understand what this for-loop does by reading the code. I know how a for-loop works though. By reading this code I literally gain no insight into what the program could be doing.
I'm coming from python to C, if that matters.
#include <stdio.h>
int main (void)
{
int numbers[10] = {1,0,0,0,0,0,0,0,0,0};
int i, j;
for (j = 0; j < 10; ++j)
for (i = 0; i < j; ++i)
numbers[j] += numbers[i];
return 0;
}
for (j = 0; j < 10; ++j)
This just means iterate through each element of the array. It is easy to understand.
for (i = 0; i < j; ++i)
numbers[j] += numbers[i];
This is much harder! I think it is because I cannot figure out in my head what j would be equal to. I cannot follow the two loops properly.
I would specifically like to know how I can read and understand this nested for-loop in C.
(I know what this snippet does because I compiled and ran the code.)
First of all, let's translate it into Python:
numbers = [1,0,0,0,0,0,0,0,0,0]
for j in range(10):
for i in range(j):
numbers[j] += numbers[i]
The outer loop iterates through all ten elements of numbers and, for each item, adds all previous elements in numbers to the current one. It's easier to follow if you add a few print statements.
for (j = 0; j < 10; ++j) doesn't mean "iterate through each element of the array". It means "for each value of j from 0 to 9, execute the code within the loop".
The code within the loop includes another loop:
for (i = 0; i < j; ++i)
numbers[j] += numbers[i];
So, for each value of j from 0 to 9, that inner loop will be executed. In effect, it would be like executing the inner loop sequentially 10 times:
for (i = 0; i < 0; ++i)
numbers[0] += numbers[i];
for (i = 0; i < 1; ++i)
numbers[1] += numbers[i];
for (i = 0; i < 2; ++i)
numbers[2] += numbers[i];
... and so on
(As a side note, the first execution of the inner loop does nothing, since 0 is not less than 0. So the initial value of the outer loop might as well be 1.)
To return to your original phrasing, if the outer loop essentially iterates over all elements of the array, the inner loop does a second iteration over all elements of the array prior to the current element in the outer loop.
Some times it is helpful to just write out what is happening along the way. Also, adding braces might help a little bit as well:
for (j = 0; j < 10; ++j)
{
for (i = 0; i < j; ++i)
{
numbers[j] += numbers[i];
}
}
(A) The first time through the outer loop, j will be zero, thus the inner loop will read:
for (i=0; i < 0; ++i)
because 0 is not less than 0, we skip the body of the inner loop (and numbers remains unchanged).
(B) The second time through the outer loop, j will be one, thus the inner loop will read:
for (i=0; i < 1; ++i)
So we will go through the body of the inner-loop once with i being zero, thus we will execute this
command; numbers[1] = numbers[1] + numbers[0]; (I expanded += for added clarity) which will now make the numbers array look like this: 1,1,0,0,0,0,0,0,0,0
(C) The third time through the outer loop, j will be two, thus the inner loop will read:
for(i=0; i < 2; ++i)
SO we will go through the body of the inner-loop twice with i being first zero, and then one. Thus
we will execute the following commands:
numbers[2] = numbers[2] + numbers[0];
numbers[2] = numbers[2] + numbers[1];
which will transform the numbers array into 1,1,2,0,0,0,0,0,0,0
and so on.
Hope this helps.
The jth element in the numbers array is the sum of all preceding elements in the array. Technically, the preceding elements gets added to the jth element, but numbers[j] is 0 to begin with.
When j equals 1,
numbers[1] = numbers[1] + numbers[0]
which makes numbers[1] == 1. (because i can only be 0 and numbers[0] is 1)
When j equals 2,
numbers[2] = numbers[2] + numbers[1] + numbers[0]
which makes numbers[2] == 2. (because i can be 0 and 1, and numbers[0] and numbers[1] are both 1)
When j equals 3,
numbers[3] = numbers[3] + numbers[2] + numbers[1] + numbers[0]
which makes numbers[3] == 4. (because i can be 0, 1 and 2, and numbers[2] is 2)
As such, any j - 1th (j > 1) element equals the sum of all of its preceding elements. Hence, as the loop goes on, the jth element would end up becoming the double of the j - 1th element.
At the end of the program, each value in the array would be 1 1 2 4 8 16 32 64 128 256
here is the value of your array after each outer loop
1 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0
1 1 0 0 0 0 0 0 0 0
1 1 2 0 0 0 0 0 0 0
1 1 2 4 0 0 0 0 0 0
1 1 2 4 8 0 0 0 0 0
1 1 2 4 8 16 0 0 0 0
1 1 2 4 8 16 32 0 0 0
1 1 2 4 8 16 32 64 0 0
1 1 2 4 8 16 32 64 128 0
1 1 2 4 8 16 32 64 128 256
as generated by this
#include <stdio.h>
#define SIZE_ARRAY 10
void show_numbers(int size_array, int index, int given_array[]) {
int i = 0;
for (; i < size_array; i++) {
printf("%3d ", given_array[i]);
}
printf("\n");
}
int main (void) {
// static const int size_array = 10;
int numbers[SIZE_ARRAY] = {1,0,0,0,0,0,0,0,0,0};
int i, j;
show_numbers(SIZE_ARRAY, 0, numbers);
for (j = 0; j < SIZE_ARRAY; ++j) {
for (i = 0; i < j; ++i) {
numbers[j] += numbers[i];
}
show_numbers(SIZE_ARRAY, j, numbers);
}
return 0;
}

Resources