Name:________________________ CS 210 ­ Fundamentals of Programming I Spring 2015 ­ In­class Exercise 5 for 02/16/2015 & 02/17/2015

advertisement
Name:________________________
CS 210 ­ Fundamentals of Programming I
Spring 2015 ­ In­class Exercise 5 for 02/16/2015 & 02/17/2015
(10 points) This exercise consists of both a coding part and a written part. The purpose of this exercise is to work with functions that have passed back parameters (called output parameters in the text book). Create a new console project in CodeBlocks.
Problem Statement
Write a program that asks the user for 3 numbers and prints them out in ascending order Your program should interact with the user in the following manner (user input in bold). Note that there is a blank line preceding the output.
Enter the first number: 23.5
Enter the second number: 6.1
Enter the third number: 12.7
The numbers in ascending order are 6.1, 12.7, and 23.5.
Analysis & Design of Main Program
To be covered in lecture
Analysis & Design of Functions get_input, exchange_doubles, and order_doubles
To be covered in lecture.
Assignment 1. (6 points) Answer the following questions regarding this program. a) What is the prototype for function order_doubles? b) How many function calls are there to function order_doubles in the main program? Give the first one. c) Why is void the return type of function order_doubles? 02/14/2015
Page 1 of 2
D. Hwang
d) Why do the parameter declarations of function order_doubles have * symbols? e) Why are the uses of the parameters in the body of function order_doubles prefixed with * symbols?
f) Why are the arguments in the calls to function order_doubles prefixed with & symbols?
2. (4 points) Modify your program so that it reads in four integers and orders them so the values are displayed in ascending order. The get_input function should ask for a fourth number and pass it back to the main program:
Enter
Enter
Enter
Enter
the
the
the
the
first number: 23.5
second number: 6.1
third number: 12.7
fourth number: 19.3
The numbers in ascending order are 6.1, 12.7, 19.3, and 23.5.
Hint: generalize the program from three values into what you need to do get the smallest value into the first variable, then the second smallest value into the second variable, etc. Build and run this program. Test it until you are satisfied it works. When you have completed this exercise, submit it to the submission system as demonstrated under assignment 09­
IN5. You may resubmit to correct errors. Turn in this sheet with your answers.
REMINDER: once you have finished this exercise, you should not leave class early unless you also have submitted Programming Assignment 3 successfully. 02/14/2015
Page 2 of 2
D. Hwang
Download