Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cs50.h>
- #include <stdio.h>
- #include <stdlib.h>
- //Daniel Lawson
- int main(int argc, string argv[]){ //create parameters for command line input
- if(argc != 2){ //return an error and write invalid input if the user hasn't inputted 2 arguments
- printf("invalid input!, please enter one integer argument\n");
- return 1;
- }
- int height = atoi(argv[1]); //convert the string to a number
- if(!(height >0 && height <=23)){
- /*
- return an error code and write invalid input if the integer was not between (0,23].
- 0 was not allowed as a valid input because if they enter characters (a string), atoi will evaluate it to 0. Thus, allowing us to check for numerical input and input in the range of numbers at the same time
- */
- printf("invalid input!, please enter one integer argument great than 0, and less than or equal to 23\n");
- return 1;
- }
- for(int i = 1; i <= height;i++){//use two nested loops to print the half pyramid (same logic as initial program)
- for(int s = 1; s <= i*-1 + height;s++){
- printf(" ");
- }
- for(int t = 1; t <= i + 1; t++){
- printf("#");
- }
- printf("\n");
- }
- return 0;
- }
- /*
- provided sample output:
- username:~/workspace/pset1 $ ./mario
- height: 8
- ##
- ###
- ####
- #####
- ######
- #######
- ########
- #########
- input must be 1-23
- */
Advertisement
Add Comment
Please, Sign In to add comment