Lab 3 Introduction to PASS, and Exercises on Operators and Basic I/O

advertisement
CS2311 Computer Programming
Lab 3
Introduction to PASS, and Exercises on Operators and
Basic I/O
1. Programming Assignment aSessement System [PASS]
In this course, you will use the PASS system for program testing and assignment submission.
You may access PASS via the link in Blackboard, or directly via https://pass3.cs.cityu.edu.hk
(You’ll be using your CityU EID)
Select our course – CS2311_CA1
Click the link "Problem list".
Click the "Test/Submit" icon for the question you want to solve.
You may specify source code (.cpp file) upload with the “Browse” button (default), or you
may paste the source code into the space provided. (Need to select from radio button)
To test the program (tutorial or assignment), click the Test button.
Note: Your program should follow the input and output format EXACTLY
(i.e. identical spacing, new lines and letter case).
Otherwise the PASS system will say that your program's output is incorrect.
To submit for an assignment, click the Submit button. (Only available for assignments, it
does not exist for lab questions). Please be reminded that only the submissions via the
Submit button are counted for grading. We do not consider the code in Test for grading.
Note: After submission, PASS will report the output of your program versus the
“expected output”. Note that for assignments, the test cases for “Test” may not be
the same as what we use for grading. (Test cases for “Test”, which you can see
when you click the Test button, are usually a subset of the complete test cases we
use for grading.)
In this lab, you are required to test your program in Q.2 using PASS. Yet instead of solving
Q2 directly, you may wish to work on Q1 first and enhance the program step-by-step.
2. Exercises on Operators and Basic I/O
NOTE: In all the following exercises, the input entered by the user is highlighted by
underline. It is not part of the output from the program.
Q1a. Download the program (area.cpp). The user inputs the width and height of a
rectangle. The program computes and output the area of the rectangle. Compile and correct
the syntax/logical errors.
Is the program easy to understand? Why?
-1-
CS2311 Computer Programming
Q1b. Enhance the program so that the input and output of the program are as follows.
(Note: The underlined words are user input. You don’t need to print it)
Please enter the width
3
Please enter the height
5
The area is 3*5=15
You should test your program using PASS.
Q1c. Improve the programming style of the program. This includes using:
(i) More meaningful variable name to reflect the purpose of the variable;
(ii) Indentation (use the tab character to indent and shift-tab to move back by 1 level);
(iii) Comments.
Q2a. Write a C++ program bmi.cpp to get the weight (in kilograms) and height (in meters)
of a person. Then calculate and print the Body Mass Index (BMI) (to 2 decimal places)
according to the formula:
BMI = weight / (height*height);
Enter your height in m: 1.8
Enter your weight in kg: 60
Your BMI is 18.52
Note: there should be a space after the colon ':'.
Remember to follow good programming style in your program.
You should test your program using PASS.
Tips:
- Identify how many variables are required. We need a variable for BMI, weight and
height. We need to assign a meaningful variable name and determine the type of each
variable.
- Consider the data type needed, is it int, or double? (see sample input above)
- To print to 2 decimal places, you may use setprecision(2)
- Important: to use setprecision(), you need #include <iomanip>
Q2b. Modify the program in Q2a, such that it accepts the user to input the weight in pounds.
We have the following conversion formula.
1 kg = 2.21 pounds
Name the file bmi2.cpp.
Example input and output:
Enter your height in m: 1.8
-2-
CS2311 Computer Programming
Enter your weight in pounds: 150
Your BMI is 20.95
You should test your program using PASS.
Q3. Write a program, convertTime.cpp, that reads in the number of seconds and
converts it to minutes and seconds. A sample output of your program is as follows.
Please enter the number of seconds: 5000
5000 second(s) = 83 minute(s) 20 second(s)
Hint 1: Use combinations of integer division (/) and modulo (remainder) operator (%)
Hint 2: Think of the followings: Given the number 12
- What’s the divisor you use to extract the tenth place (i.e. 1)?
- How can you extract the unit place using modulus (i.e. 2)?
Check the correctness of your program with different input, e.g. 0, 5, 65, 120, 5000, ….
Try at least 6 test cases to test your program.
You should test your program using PASS.
Q4. Extend the program in Q3 such that the program converts the input to hours, minutes and
seconds. A sample output is as follows.
Please enter the number of seconds: 5000
5000 second(s) = 1 hour(s) 23 minute(s) 20 second(s)
Hint3: Comparing part 1) and 2), the second field is identical and therefore requires
no change. All you need to do is to break down 83 minutes to 1 hour and 23 minutes.
Again, this could be done with the division (/) and modulo operators (%)
-3-
Download