Skip to content

Instantly share code, notes, and snippets.

@waggertron
Created October 1, 2018 22:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save waggertron/3e05c9893b8060219e41427e3201ff16 to your computer and use it in GitHub Desktop.
Save waggertron/3e05c9893b8060219e41427e3201ff16 to your computer and use it in GitHub Desktop.
build pyramid one loop construction
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char * char_repeat( int n, char c ) {
char * dest = malloc(n+2);
memset(dest, c, n);
dest[n] = '\n';
dest[n + 1] = '\0';
return dest;
}
int main()
{
int i, j, rows;
char character = '*';
printf("Enter number of rows: ");
scanf("%d",&rows);
char arr[rows * 2 -1][rows + 2];
for(i = 0; i < rows; ++i)
{
strcpy(arr[i], char_repeat(rows - i, character));
strcpy(arr[i+rows], char_repeat(i + 1, character));
}
for (j=0; j < rows * 2; j++) {
printf("%s", arr[j]);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment