Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- pattern_10_10.c
- https://www.facebook.com/photo/?fbid=1368408023607660&set=gm.5173435132677435
- Task pattern:
- n = 4 , number of rows.
- 1
- 10 10
- 100 100 100
- 1000 1000 1000 1000
- In the first row we have those 1 (10 ^ 0)
- In the second row we have two 10 (10 ^ 1)
- In the third row we have three 100 (10 ^ 2)
- . . .
- And in the nth row we have n 10 ^ n
- The problem is that the numbers are aligned with the right margin and
- therefore for each line we have to calculate how many blank characters
- precede the beginning of the number printing.
- n = 4 , number of rows.
- ------------------1 , row = 0 has 18 blanks in front of the first digit
- --------------10 10 , row = 1 has 14 blanks in front of the first digit
- --------100 100 100 , row = 2 has 8 blanks in front of the first digit
- 1000 1000 1000 1000 , row = 3 has 0 blanks in front of the first digit
- The length of the longest row is n*n+n-1 .
- In each row the numbers take up (row+1)*(row+1)+row places.
- The rest up to the length of the longest row are blank characters.
- So, number of blank characters in each row is
- blanks = n*n+n-1 - ( (row+1)*(row+1)+row ) .
- You can find all my C programs at Dragan Milicev's pastebin:
- https://pastebin.com/u/dmilicev
- */
- #include <stdio.h>
- #include <math.h> // due to the function of pow()
- // implementation of function pow() only for integers
- int my_pow_for_integers(int base, int exponent){
- int result = 1;
- for (exponent; exponent>0; exponent--)
- result = result * base;
- return result;
- }
- // print a pattern along the left edge
- void print_pattern1(int n){
- int i, j;
- for(i=0;i<n;i++){ // i count rows
- for(j=0;j<=i;j++) // j counts 10^i
- printf("%.f ", pow(10,i) );
- printf("\n\n");
- }
- }
- // print a pattern aligned along the right edge
- void print_pattern2(int n){
- int i, row, blanks;
- for(row=0;row<n;row++){ // print rows, row count rows
- blanks = n*n+n-1 - ( (row+1)*(row+1)+row ); // calculate how many blanks
- for(i=0;i<blanks;i++) // print blanks, i counts how many
- printf("-");
- for(i=0;i<=row;i++) // print the numbers 10^row, i counts how many
- printf("%.f ", pow(10,row) );
- printf("\n\n");
- }
- }
- int main(void){
- int n=4; // n is number of rows
- print_pattern1(n); // print a pattern along the left edge
- printf("\n\n");
- print_pattern2(n); // print pattern aligned along the right edge
- // printf("\n 10 ^ %d = %d\n", n, my_pow_for_integers(10,n) );
- printf("\n\n");
- return 0;
- } // main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement