Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Challenge Questions / Exercises In this part of the Clab you will have to write three C programs based on what has been learned in
Challenge Questions / Exercises In this part of the Clab you will have to write three C programs based on what has been learned in the lectures and labs up to this point. In these questions in this lab and later labs, marks are given for code which compiles and runs (even if it doesn't meet the specification), good comments and layout, and also for code which performs the required tasks (for the more complex tasks in later labs, partially working code may get partial marks in this category). Exercise 1-Calculator Write a simple calculator program that can perform the 4 basic arithmetic operations (addition, subtraction, multiplica- tion, division). Specifically: . when run, the program should display a text prompt asking for input (two numbers and an operation) . you may take these separately, or as a single line, as long as your code appropriately asks. The program should perform the correct operation on the numbers, via some appropriate conditional branching, and should then print the result to the screen, and exit. The resulting (source code) file, with comments, is your submission. 30% of the marks for this exercise are for appropriate layout and comments. (3) Exercise 2N Green Bottles 10 Green Bottles, sitting on the wall The popular English counting song 10 Green Bottles consists of 10 verses, beginning as shown in the boxout, and so on for sub- 10 Green Bottles, sitting on the wall sequent verses (until we reach the special case of 1 Green Bottle, and if 1 Green Bottle should accidentally fall... which is the final verse, resulting in no Green Bottles). (The concept is similar to other counting songs in other languages - ..there'll be 9 Green Bottles, sitting on the wall. the number of bottles changes each verse until the end.) 9 Green Bottles, sitting on the wall... A. Using an appropriate loop, write code to print out the entirety of the 10 Green Bottles song (you may need to use an if statement to handle the special case for 1, or place it outside the loop (which is less elegant, but potentially faster")). By adding appropriate input handling, prompt the user to enter a number of bottles to start from. Check that the number is greater than 0, and is not larger than 99. Alter the loop to count starting from the number the user enters, if it is valid, and print out every verse. B. Two source code files (one for A and one for B) are your submission for this exercise (you may start with a copy of your solution to A when attempting B). 30% of the marks for this exercise are for appropriate layout and comments. (3) Exercise 3Squarefree Numbers Trial division is the process of repeatedly trying to A "squarefree" number is one which is not divisible by any of divide the number by all candidate factors the square numbers (4,9,16...). (numbers less than it). Write a program which uses the basic method of trial If it is divisible by at least one, then it cannot be division to determine if a provided number is squarefree squarefree. or not. The code should: (Continued on page 4) (Continued from page 3) prompt (with text output) for a number read it in, perform the squarefree test, print a suitable response, depending on the result. After printing the result, the code should ask if you want to perform another test if so, it should prompt for another number and perform tests. If not, it should exit. (You may use any particular input to test for these options - we suggest 'Y' and 'N'!) A flowchart for the logic needed for this example is included below; map the design to appropriate structures in C (the grey box surrounds the core logic for the squarefree number test itself). One source code file is your submission for this exercise. 30% of the marks for this exercise are for appropriate layout and comments. (3) start print "Please Enter a number" read in number (n) i = 2 True n %i*i= 0? True False True print "Not Squarefree" add 1 to i i*i<>
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started