Title: COMP104 Special Tutorial
1COMP104 Special Tutorial
2Outlines
- Real and Programming Environment
- A C Program
- Write a Program - HOWTO
- Divide-and-conquer
- Understand the requirements
- Analyze the logic
- Implement your program
- Test your program
- Good Practices
- Example
3Real and Programming Environment
- Programming environment is NOT the same as the
real environment - Share some similarity and dissimilarity
- C program is constructed from basic types (int,
char, etc) - gttry to map the input/output/operations to them
- Conditional and repetition statements
- ifelse, switch
- for, while, dowhile
4Real and Programming Environment
- How to solve daily life problems with
programming? - Knowledge of data types (primitive types, class,
struct, etc). Do you know how to convert the
daily life input and output to C data types? - Understanding of different syntaxes (looping,
conditional, etc) available. Could you point out
some similar scenarios in daily life? - Practice, practice and practice. The skills can
only be improved by writing your codes and
reading more examples.
5A C Program
- Generally speaking, the source code can be
divided into functional and non-functional
portions - Functional (What should the program perform?)
- Logic details (including logic design)
- Non-functional (No effect on the program
functionalities but improve the readability) - Indentation
- Spacing
- Comments
6A C Program
Libraries to be included (functions, constants,
etc)
main function (your program logic)
// A typical C program include
ltiostreamgt using namespace std int main() //
variable and constant declaration const int
MAX_NUM_INPUT 8 char userInput // prompt
user for a character cout ltlt "Enter a character
" cin gtgt userInput // display the user
input cout ltlt "You have typed \'" ltlt userInput
ltlt "\'!" ltlt endl return 0
Comments
Indentation
7Write a Program HOWTODivide-and-conquer
- A single problem is too large to be solved
- Divide the problem to sub-problems until you
could find a solution for it (conquer) - You will learn C functions later
- Example
- Write a program to compute and output the mean,
maximum and minimum scores from a set of scores - Input the scores
- Compute the mean score
- Compute the maximum score
- Compute the minimum score
- Display the mean, maximum, minimum scores
8Write a Program HOWTOUnderstand the
requirements
- A daily life problem consist of different parts
- Input requirements (Number of inputs? Type of
inputs? Input validation?) - Logic requirements (How to compute the correct
value?) - Output requirements (Number of outputs? Type of
outputs?)
9Write a Program HOWTOAnalyze the logic
- A paper and a pen is always useful
- Write down the important parts
- Draw some diagrams to illustrate the logic
- Make sure you understand the logic completely
- Usually your diagrams can be transform to
programming constructs like looping, conditional
statements
10Write a Program HOWTOImplement the program
- After an analysis of program requirements (input,
output, processing logic, etc), you should have
sufficient understanding of the program - With the diagrams on your paper, its easy to
transform them into C statements
11Write a Program HOWTOTest your program
- Very Important to validate your program
- Some sample input and output are given
- Serve as the input/output requirement AS WELL AS
some test cases - Derive some test cases
- For common cases
- For special cases
12Good Practices
- Comments
- Help you and your colleagues to understand and
maintain the codes - Indentations and spacing
- Improve the readability
- Have NO impact on compiler
- Design and THEN code
- Make sure you know what you are doing!
- Its a waste of time to design and code at the
same time
13Good Practices
- Initialization
- C compiler will not give you a compilation
errors if you use a variable which have not
initialized! - You should write and check your codes carefully
- Variable naming
- C compiler does not require you to give
meaningful name for variables - But a meaningful one will help yourself and
others to understand and maintain (and write) the
codes - numApple vs a
14Good Practices
- Clarity and efficiency
- Both are important
- But try to avoid messy codes
- Fewer number of lines DOESNT imply its better
- Opening and closing braces
- Always add a pair of for if, for, while
statements - Always add a pair of () for complex boolean
expressions - Also. always add a break-statements in switch!
- Sometimes, its not necessary to add the braces
but it could avoid certain types of errors and
improve the readability
15Example
- Leap year program in Lab 2
- Analysis and breakdown
- Display the prompt message and get the year from
user - Check if the year is leap or not (number of days
in a year) - Output the number of days and leap/non-leap for
the inputted year
16Example
- Diagram for leap year checking
Divisible by 4?
True
False
Divisible by 400?
NOT a Leap year
False
True
Divisible by 100?
Leap year
True
False
Leap year
NOT a Leap year
17Example - Implementation
// Leap year calculation program include
ltiostreamgt using namespace std void
main() int year //year for leap year
checking int day //days in a year. //
leap year input cout ltlt "Leap Year
Calculation"ltltendl cout ltlt "Enter the year
" cin gtgt year // calculate number of days in
year if(year 4 ! 0) //NOT Leap year if not
divisible by 4 day 365 else if(year 400
0) //Leap year if divisible by 400 day
366 else if(year 100 0) //NOT leap year if
divisible by both 100 4, but not 400 day
365 else //Leap year if all
previous condition not satisfied day
366 //print out if the year is a leap year or
not if (day 366) cout ltlt year ltlt " is a Leap
year." ltlt endl else cout ltlt year ltlt " is
NOT a Leap year." ltlt endl //print out the
number of days in the year. cout ltlt "The number
of days in year " ltlt year ltlt " is " ltlt day ltlt "."
ltlt endl
18Example
- Testing
- Identify some normal and special cases
- Year 2000
- Year 1900
- Year 1996
- Year 1998
19End