65 lines
1.4 KiB
C
65 lines
1.4 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
void print_array(int *arr){
|
|
int *ptr = arr;
|
|
while ( *ptr != '\0' ){
|
|
printf("%d ", *ptr);
|
|
ptr++;
|
|
}
|
|
printf("\n");
|
|
}
|
|
|
|
// starting at index 1, decrement by one until index size - 1
|
|
// then move starting index up by one and have the stop address move back one and repeat
|
|
void first_half(int *arr, int n, int arr_size){
|
|
int i;
|
|
for ( i = 0; i < (n - 1); i++ ){
|
|
int *ptr = &arr[i];
|
|
while ( ptr != &arr[(arr_size - 2) - i] ){
|
|
ptr++;
|
|
(*ptr)--;
|
|
}
|
|
print_array(arr);
|
|
}
|
|
}
|
|
|
|
// starting at the center, increment by one
|
|
// then expand the contraints by moving the start index back one and end index up one
|
|
// increment and repeat
|
|
void second_half(int *arr, int n, int arr_size){
|
|
int i;
|
|
for ( i = (n - 1); i > 0; i-- ){
|
|
int *ptr = &arr[i];
|
|
while ( ptr != &arr[(arr_size) - i] ){
|
|
(*ptr)++;
|
|
ptr++;
|
|
}
|
|
print_array(arr);
|
|
}
|
|
}
|
|
|
|
int main(){
|
|
int n, i, arr_size;
|
|
scanf("%d", &n);
|
|
// Complete the code to print the pattern.
|
|
|
|
// Set array size to be able to count to 1 then back up to passed number
|
|
arr_size = (2 * n) - 1;
|
|
int *arr = malloc(arr_size * sizeof *arr);
|
|
|
|
// Fill the array with the passed number and print array
|
|
for ( i = 0; i < arr_size; i++ ){
|
|
arr[i] = n;
|
|
}
|
|
print_array(arr);
|
|
|
|
// complete the first half of pattern
|
|
first_half(arr, n, arr_size);
|
|
second_half(arr, n, arr_size);
|
|
|
|
return 0;
|
|
|
|
|
|
}
|