讲解Sequence Control、辅导CProblem Solving and ProgrammingC/C++编程
- 首页 >> C/C++编程Problem Solving and Programming I
Practical Session 3: Programming with Sequence Control Structure
This practical class is about using sequential controls and C language constructs to
implement such controls. The class consists of three parts: (a) writing a simple program with
a sequence of assignment statements; (b) writing a program for a little more complex
problem, and (c) write a number of more programs that only require sequential processing.
I. Escape Sequence
a. Read the following codes, then explain the output.
#include <iostream>
using namespace std;
int main()
{
cout << "\nThis is\t a string\n\t\t"
" with \"many\" escape sequences!\n";
return 0;
}
b. Write a program to display the quote to the screen
“Without requirements or design, programming is the art of
adding bugs to an empty text file”.
The output should contain “ (double quote symbol).
II. Writing a Simple Program with Only Sequence. What is the output?
#include <iostream>
using namespace std;
int main ()
{
int a=5;
int b = 2;
int result;
a = a + 3;
result = a - b;
cout << result;
return 0;
}
Note: Do not under-estimate the importance of this small exercise. It requires you to use
assignment statements carefully in a certain order; otherwise, the program will not work.
III. Writing a Program for Solving a Familiar Problem
In our first workshop, we discussed the problem of a coach company considering opening a
new route between two cities. Today, try to write a program to solve a large part of that
problem, i.e. estimating the total cost of running the route and calculating the total income
from the ticket sales for the new service. So write a program that inputs the distance
between the two cities, the cruise speed for the coach, the unit fuel cost, the fuel
consumption rate, the hourly rate for the driver, the ticket price, and the estimated number
of passengers using the new service. The program then displays the total income from ticket
PSPI/Practical Session/3 Tuan Nguyen
sales and the total cost of running the service (including only the fuel cost and labour cost).
Before you start adding new statements into the program, you should try to write down the
solution in pseudo code in the program header.
You should be able to do this exercise around 30 - 40 minutes. However, if you move a bit
slower, it does not matter. Take your time and do this exercise steadily and properly. You
may work in pairs if you wish to do so.
IV. Further Individual Exercises
Write a program for each of the following small problems:
1. Calculate the value of the expression 3x3 + 4x2 + 7x + 10 and display the result.
2. Like the question 1, write a program to evaluate the below equation:
If you finish this question, now ask yourself what if the value of x is 0? And How to avoid
it?
3. Take a 6-digit positive integer representing a date in the compressed format ddmmyy,
and then extract from the input integer the day number, the month number and the
year number.
4. Take a real number as input and perform the rounding up or rounding down operation
depending on the value of the input real number. For instance, the input real number
3.14 becomes 3, but the input real number 3.61 becomes 4.
5. Take an amount of cash deposit and a fixed annual interest rate, calculate and display
the total amount of saving after 3 years.
If you reach exercise 5 above, please consider how to calculate the total amount of saving
after 10 years or even 100 years? Is the use of sequential control enough for solving this
problem?
Homework
In this exercise, you don’t need to implement it in C++. You just need to develop an
algorithm in a pseudo code or flowchart.
The question is that counting how many positive 3-digit numbers are odd number and do
not contain the digit 5?
You might use some extra things that I ask you in question IV. 2 and IV.5. Of course,
following weeks, I will show you more details.