This is my code for the CS50 Mario (more comfortable). Answers can be found all over the net however I wrote this myself.
I would like to refactor to its maximum potential (just for my own improvement I'm not enrolled or anything).
I have chosen to create 2 functions and called them in the main function.
Can this be be improved or refactored anymore or is this as good as it gets?
A quick outline of the code:
Firstly it asks for a valid input of an amount of rows to be built (an integer between 0 and 550) and then builds a Mario wall like this: # #
#include <stdio.h>
#include <stdlib.h>
void build_wall(int num_rows);
void get_rows(int n);
int rows = 0;
int spaces = 0;
int bricks = 0;
int main(void)
{
get_rows(rows);
build_wall(rows);
}
void get_rows(int n)
{
do
{
printf("How many rows would you like? ");
scanf("%d", &rows);
printf("\n");
printf("You would like %d rows.\n", rows);
}
while (rows <= 0 || rows >=550);
}
void build_wall(int num_rows)
{
for (int i = 0; i < num_rows; i++) ///number of rows
{
for(spaces = 0; spaces < num_rows-i-1; spaces++ ) // first spaces
{
printf(" ");
}
for(bricks = 0; bricks < i+1; bricks++) // first wall bricks
{
printf("#");
}
{
printf(" "); //Middle spaces
}
for(bricks = 0; bricks < i+1; bricks++) // Second wall bricks
{
printf("#");
}
printf("\n");
}
}