For the fifth code: #include #include #include int main(int argc, char *argv[]) { int target; if(argc == 2) { if(sscanf(argv[1], "%d", &target) == 1 && target >= 0 && target

For the fifth code:





#include

#include

#include













int main(int argc, char *argv[]) {


int target;










if(argc == 2) {


if(sscanf(argv[1], "%d", &target) == 1 &&


target >= 0 && target <=>


printf("USING COMMAND-LINE SPECIFIED TARGET %d\n\n", target);


}


else {


printf("BAD COMMAND-LINE VALUE. GOODBYE\n");


return 0;


}


}


else {


// your code here to set the target to a random value in 0..99


//


}






// when we arrive here, target has been set either via the command


// line or via your randomization code and we are ready to go.










return 0;

}








hw1 Programming Assignment 1: Five Small C Programs DUE: Tuesday, Sept 20 by 11:59PM NOTE: for purposes of testing/grading, it is important that output from your programs follows exactly the formats and examples given. Yes, this might be a little annoying, but we have tried to keep things relatively simple. RESOURCES: you will be using the scanf library function for this assignment. You will find a slide deck with some discussion and examples of scanf in this folder. SUBMISSION: Submission of your five programs will be done via gradescope (as will autograding - multiple submissions will be allowed). Stay tuned for details. Program: median3 Filename: median3.c Behavior: This program reads three integers from the input and then prints the values read to the display and then reports the median of the three. The last part is the important part of course! Do you remember what a median is? Examples: The median of 12, 7, 13 is 12 The median of 2, 2, 2 is 2 The median of 1, 3, 2 is 2 The median of 8, 1, 8 is 8 Example runs ("$" is simply the terminal prompt): $ ./median3 Enter an integer: 12 Enter an integer: 7 Enter an integer: 13 median: 12 $ ./median3 Enter an integer: -12 Enter an integer: seven INVALID INPUT $ Error Handling: if any of the three user inputs cannot be parsed as an integer, the program simply prints "INVALID INPUT" and terminates immediately (it does not re-prompt the user or anything "fancy" like that). Program: stairs Filename: stairs.c Behavior: ● The program first reads a non-negative integer n from the input. ● It then displays an “up” staircase with n steps ● Then it displays a “down” staircase with n steps ● And finally, it displays an “up-down” staircase composed of n levels: up followed by down (see example below). Two examples below (one for n=3 and one for n=0): $ ./stairs Enter number of stairs: 3 up: X XX XXX down: X XX XXX up-down: X XXX XXXXX $ ./stairs Enter number of stairs: 0 up: down: up-down: $ Error Handling: if the user input cannot be parsed as an non-negative integer, the program simply prints "INVALID INPUT" and terminates immediately (it does not re-prompt the user or anything like that). Program: histogramA Filename: histogramA.c Behavior: This is the easier of two histogram programs you will write. This program will “draw” an ASCII histogram for 4 non-negative data values A, B, C and D (read from the input) in horizontal orientation. Example: if A=3, B=7, C=2 and D=5, the output should look like this: A: XXX B: XXXXXXX C: XX D: XXXXX Behavior/Specifications/Details: When run, the program prompts the user for four non-negative (A-D) one by one (example below). ○ If any of the values is negative, the program prints an error message and terminates. ○ Otherwise, the program prints a nice ASCII histogram of the data and terminates. Sample run with inputs 3 7 2 5 $ ./histogramA A: 3 B: 7 C: 2 D: 5 A: XXX B: XXXXXXX C: XX D: XXXXX Sample run with input error $ ./histogramA A: 9 B: -9 INVALID INPUT $ Note 1: You may NOT use arrays for this program!!! Note 2: If the user enters a negative number for any of A-D (or a non-integer), the program should simply terminate with an error message as in the example above. Program: histogramB Filename: histogramB.c Behavior: This program will “draw” an ASCII histogram for 4 data values A, B, C and D (read from the input)in the usual "vertical" orientation. Example: if A=3, B=7, C=2 and D=5, the output should look like this: X X X X X X X X X X X X X X X X X --------- A B C D Behavior/Specifications/Details: When run, the program prompts the user for four non-negative (A-D) one by one (example below). ○ If any of the values is negative, the program prints an error message and terminates. ○ Otherwise, the program prints a nice ASCII histogram of the data and terminates. Sample run with inputs 3 7 2 5 $ ./histogramB A: 3 B: 7 C: 2 D: 5 X X X X X X X X X X X X X X X X X --------- A B C D Sample run with input error $ ./histogramB A: 9 B: -9 INVALID INPUT $ Note 1: the histogram must be “vertical” as in the example above. The following would be considered wrong: WRONG!!!! A: XXX B: XXXXXXX C: XX D: XXXXX Note 2: You may NOT use arrays for this program!!! Note 3: If the user enters a negative number for any of A-D (or a non-integer), the program should simply terminate with an error message as in the example above. Program: guess Filename: guess.c Behavior: This program will use randomization to play a guessing game with the user. First, here is a sample run: $ ./guess I am thinking of a number between 0 and 99. your guess: 5 TOO SMALL your guess: 90 TOO BIG your guess: 2 COME ON, THAT'S IMPOSSIBLE your guess: 50 TOO BIG your guess: 25 TOO BIG your guess: 109 OOPS - NOT IN RANGE 0..99. NO GUESS CHARGED your guess: 15 TOO SMALL your guess: 20 TOO BIG your guess: 17 YOU GOT IT! NUMBER OF GUESSES: 8 Details: ● if the user enters text that cannot be parsed as an integer (i.e., scanf fails), the program simply prints BAD INPUT and terminates. ● you have been provided a bit of starter code which lets us run the program with a pre-specified target value. Do not modify this portion of the code (at the beginning of main()) and follow the comments.
Sep 16, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here