So basically, I want to make a pyramid pattern using "#" just like normal, but this time the amount of pyramid isn't only one, but the same as the input. How can I do that? This is the example of input and output
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, row, space, hashtag, n;
scanf("%d", &i);
if(i>0 && i<100){
for(n=1; n<=i; n ){
for(row = 1; row<=i; row ){
int mathspace = i-row;
int mathhashtag = (2*row)-1;
//print spaces
for(space=1; space<=mathspace; space ){
printf(" ");
}
//print hashtag
for(hashtag=1; hashtag<=mathhashtag; hashtag )
printf("#");
//print newline
printf("\n");
}
}
}
return 0;
}
What i tried This is my code, i managed to print it, but it makes a newline everytime its making another pyramid, how can i make it so that it goes sideways? thanks!
CodePudding user response:
If you're willing to work with trees limited to about 30 (or 62???) characters wide (the largest would require ~4000 characters on a single line) there's simple way to achieve this. Use a bit mask to control outputting either ' ' or '#', and repeat and repeat. After each line is printed, shifting & or'ing the mask with itself adds width to the '#'s that will be printed.
#include <stdio.h>
#include <stdint.h>
void fun( int n ) {
uint32_t mask = 1 << n;
for( int r = 1; r <= n; r ) {
for( int d = 0; d < n; d ) {
for( int c = 1; c < 2*n; c )
putchar( "# "[!((1<<c) & mask)] );
}
putchar( '\n' );
mask |= (mask<<1) | (mask>>1);
}
}
int main() {
for( int i = 1; i <= 7; i = 2 ) {
printf( "\n%d:\n", i );
fun( i );
}
return 0;
}
1:
#
3:
# # #
### ### ###
###############
5:
# # # # #
### ### ### ### ###
##### ##### ##### ##### #####
####### ####### ####### ####### #######
#############################################
7:
# # # # # # #
### ### ### ### ### ### ###
##### ##### ##### ##### ##### ##### #####
####### ####### ####### ####### ####### ####### #######
######### ######### ######### ######### ######### ######### #########
########### ########### ########### ########### ########### ########### ###########
###########################################################################################
CodePudding user response:
Try printing one row at a time and within a single row print all the n
occurrences of the strides of hashtags you need.
You need to separate each stride by the same amount of spaces you separate a row in your code from the left side of the screen;
#include <stdio.h>
#include <stdlib.h>
void printPyramids(const int i) {
int row, space, hashtag, n;
for (row = 1; row <= i; row ) {
for (n = 0; n < i; n ) {
const int mathspace = i - row;
const int mathhashtag = (2 * row) - 1;
// print spaces
for (space = 1; space <= mathspace; space ) {
printf(" ");
}
// print hashtag
for (hashtag = 1; hashtag <= mathhashtag; hashtag )
printf("#");
//Instead of printing new line, separate the next stride by mathspace spaces
for (space = 1; space <= mathspace; space ) {
printf(" ");
}
}
printf("\n");
}
}
int main() {
int i;
scanf("%d", &i);
if (i <= 0 || i >= 100) {
printf("Invalid input");
return 0;
}
printPyramids(i);
return 0;
}
For n = 5
it outputs:
# # # # #
### ### ### ### ###
##### ##### ##### ##### #####
####### ####### ####### ####### #######
#############################################