Uploaded by Lo Ming Yeung

L01-Intro

advertisement
CS2311 Lecture One:
Introduction
W.K. Chan
City University of Hong Kong
O: G7309
T: 3442 9684
E: wkchan@cityu.edu.hk
1
Outline

CS2311 Computer Programming

5W+1H: Why? Who? What? Where? When? How?

Computing ABC
The famous HelloWorld program
Program output via cout

Syntax, and syntax error


2
Why?

Programming skill is no longer an option.

In 2013, Obama asked every American to learn
programming (even for an hour)



https://code.org/
Programming can be learned at every level of
education (including preschool?)
Facts:


CS courses are built on top of programming skills.
Internet-Of-Things, Big Data, non-trivial Business
Intelligence, new AI models, new Apps, Games
3
Who?

Course Instructor

W.K. (Ricky) CHAN




Email: wkchan@cityu.edu.hk
Office: G7309@Yeung
Tel: 3442 9684
Teaching Assistants






LIU Yuchen
MOU Ningping
PENG Sen
WANG Haipeng
ZANG Yichen
ZHOU Qilin
4
Where and When?

Lecture

Every Tuesday C01, CA1, CB1


0900-1150 at LT-9 (Yeung)
Tutorial

Go to your enrolled tutorial. All tutorials are full

Monday morning (1100-1150) L01, LA1, LB1 @MMW2450
Monday afternoon 1 (1300-1350) L04, LB4 @MMW2462
Monday afternoon 2 (1400-1450) L03, LB3 @MMW2462
Monday afternoon 3 (1500-1550) L02, LA2, LB2
@MMW2462



5
What?
C++ Learning Resources

Textbook
The course covers the basic and standard C++. Any
textbook on C++ works!
Web resources: keyword is C++.
 Youtube: https://www.youtube.com
 Q&A Stackoverflow: http://stackoverflow.com
 More practices: http://www.cprogramming.com
 http://www.cplusplus.com/doc/tutorial/


6
What?
C++ Learning Resources

If you need Reference Books. Any edition is good enough.


Numerous C++ books in the CityU library waiting
for you to loan them. They are lonely.
Nell Dale and Chip Weems. Programming and
Problem Solving with C++



It has a brief version
Walter Savitch. Problem Solving with C++
Chung Keung Poon, Matthew C.M. Chang.
Concepts and Techniques in C++ Programming
7
What?
Course Outcomes in Brief
1.
2.
3.
4.
Explain the structure of an object-oriented
computer program.
Analyze, test, and debug computer programs.
Solve a task by applying effective.
programming techniques.
Design and construct well-structured programs
with good programming practices.
8
How?
Course Schedule
Week
Lecture
Mon Tutorials
Assignment
1
Basic concept of programming
No tutorial
2
Data type, operators, I/O
Visual Studio (VS)
3
Selection (if-then-else)
Simple program
4
Iteration (loop)
Selection
5
Function
Iteration
6
Quiz 1 (up to Iteration)
Function
7
Array (1D, 2D)
VS Debugger
Ass 1 due
8
File I/O
Array
Ass 2 released
9
String
File I/O
10
Pointer (with Tutorial on Pointer)
String
11
Recursion
No tutorial (see Week 10’s lecture plan)
12
Class and Object
Recursion
13
Quiz 2 (up to Recursion)
Class and Object
Ass 1 released
Ass 2 due
How?
Assessment

Coursework 40%

Quiz 1 (10%)


Quiz 2 (10%)


Cover topics in Week 2 to Week 5 (up to Function)
Assignment 2 (10%)


Cover topics in Week 1 to Week 11 (up to Recursion)
Assignment 1 (10%)


Cover topics in Week 1 to Week 5 (up to Iteration)
Cover all topics
Examination 60%
10
What?
Plagiarism Examples






Share your code with your classmates for them to
modify to become their code.
Glance the other persons’ code closely and write your
own code by following these “examples”.
Group discussion on an assignment until almost
everyone can work out their own solutions.
Copy other person’s code. Change some variable
names, change the order of program statements,
change for-loop to while-loop, create, …
Ask AI tools such as ChatGPT or Bing
11
Why not “Be Original”?
How to avoid plagiarism?
My good friend calls me for help?!

Understand their difficulties, and help them to
clarify their misconceptions
Same for





your friend
to help you
Don’t show your solution to them.
Don’t sketch your (partial) solution on paper.
Don’t replace them in solving their problems.
Use toy examples (as we are going to see in our
lecture notes) for illustrating their misconceptions.
Show your suggestions to rectify the mistakes
committed in the simplified examples
Help them to crystalize the problem to be solved. 12
How?

Attend all lectures




Attend all tutorial/lab sessions



Acquire the skills through practice.
Discuss and ask questions in lectures and tutorials


Study (not browse) notes before coming to each lecture?
Verify your understanding against the lecture notes
Raise questions immediately
Cross-check with your classmates.
Consult the teaching team and use Programming Clinic!
Coding, coding, coding. The only successful formula!
13
The Really First Lecture in C++
14
Computing

Execution trace: A sequence of instructions for a
computer hardware to execute one by one

Some instructions store data into certain memory locations




The original data stored in these memory locations are overwritten.
Some instructions read the data from the memory locations
After the data are read in the program, some instructions can
compute new data
Program: a list of program statements that can
generate one or more execution traces

An execution trace does not go through this list from top to
bottom; rather it jumps here and there and can go through
15
the same portion of the list again and again
What is a Computer Program?

A collection of statements to instruct processors (e.g.,
CPUs) to compute results (i.e., program outputs)
Meaning: define two
placeholders x and y
to keep 1 and 2,
respectively. Then
add x to y. Finally,
output the current
values kept by x and
y to the console.
16
We try this process in the Tutorial
Building a C++ program

Writing source code in C++.


Preprocessing


Processes the source code for
compilation.
Compilation



e.g., hello.cpp
Checks the grammatical rules
(syntax).
Source code is converted to
object code in machine.
language (e.g., hello.obj)
Linking


Combines object code and
libraries to create an executable
(e.g., hello.exe).
Library: common functions
(input, output, math, etc) shipped
by C++ vendors
17
Order of Statements Executed

Programmers define the order of execution in the source code.

Only logically reasonable sequences will produce desirable outcomes
that match the expectation!

Sequence 1
1. Insert Key
2. Unlock door
3. Open door
4. Remove Key
5. Enter room
6. Close door

Sequence 2
1. Insert Key
2. Unlock door
3.
4. Remove Key
5. Enter room
6. Close door
7. Open door
Expectation: Open and close a locked door and get in
Sequence 3

1. Insert Key
2. Unlock door
3. Close door
4. Remove Key
5. Enter room
6.
7. Open door 18
Visual Studio .cpp template
using namespace std;
int main()
{
return 0;
}
Keep all these
lines in our
programs.
19
Syntactically-correct program
HelloWorld.cpp
// This is a comment. The compiler ignores comments.
// File iostream contains operators "cout" and "endl"
// Namespace std contains "cout" and "endl"
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!" << endl;
cout << "Hello World Again!" << endl;
// Exit program. // this is a comment line
return 0;
}
This function main()
contains 3 statements
with a pair of curly
bracket { }. It should
be interpreted from top
to bottom, statement by
statement with the
function body.
Note that in Microsoft Word, the double quotes “” in a font not
recognized by a C++ compiler.
20
Compiling and Running
HelloWorld.cpp
Console:
21
Rule #1
The order of statements executed is important
// This is a comment.
The compiler ignores comments.
// File iostream contains "cout" and "endl"
// Namespace std contains "cout" and "endl"
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World Again!" << endl;
cout << "Hello World!" << endl;
// Exit program.
return 0;
}
22
Rule #2
C++ is Case-Sensitive
≠
C++ is case-sensitive
≠
c++ is case-sensitive
23
The main Function
int main()
{
// write statements in between the pair of { and }.
// each statement must end with a semi-colon(;)
return 0;
}



The main() function is where every C++ program
will start execution. We have to write this function.
After the word main(), it should start with a pair of
curly brackets{ }
Don’t write int main;() or int main();
24
include

#include is known a C++ preprocessor directive.
#include <iostream>
Each #include specifies to include a specific file, e.g.,
iostream, at the head position before our code.


For beginners like us, always add this line to the program.
With this inclusion, our HelloWorld.cpp can use
the cout object (which is defined in iostream).
25
namespace

using namespace std;
tells the compiler to look in the namespace std
(standard namespace) for some popularly used
objects (functions, classes, etc.).


cout is in the namespace std
After the statement “using namespace std;”
 std::cout is the same as cout
26
Producing outputs via cout
…
cout << "Hello World!" << endl;
cout << "Hello World Again!" << endl;
…


cout “Console OUTput” allows our code to output values to
the standard output stream (monitor display).
The value on the right-hand side of << in the first statement
above is a string "Hello World!" to be sent to cout.

Any literal (character string) that is to be output must be in between a
pair of double quotes.
27
cout


escape sequence: the character next to the character \ is not
interpreted in the normal text character by C++
\n


\\


represents a newline character: its effect is to advance the
cursor on the screen to the beginning of the next line
backslash: Insert the backslash character \ in a string
\"

double quote: Insert the double quote character " in a string


We need it in our Tutorial Two when developing the Hangman game.
endl is an object. Don’t use it like "endl"



Has the same effect as the string "\n“.
Note that there is no \ before endl
It is “end L” not “end ONE”
28
Syntactically-correct program
HelloWorld2.cpp
#include <iostream>
using namespace std;
int main()
{
// statements can be on multiple lines
cout << "Hello World!"
<< endl;
// note that there is still one semi-colon.
cout << "Hello World Again!"
<< endl; // comments can be here
return 0;
// exit program
}
29
Syntactically-correct program
HelloWorld3.cpp
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!";
cout << endl;
// there is a semi-colon
// this is a new program statement
cout << "Hello World Again!"
<< endl;
return 0;
// exit program
}
30
Syntactically-incorrect program
HelloWorld_NoInclude.cpp
1.
// Example of syntax error.
2.
3.
4.
// Forgot "#include <iostream>"
using namespace std;
5.
6.
7.
8.
9.
int main()
{
cout << "Hello World!" << endl;
cout << " Hello World Again!" << endl;
10.
return 0;
11.
12.
}
// exit program
Fix the bug:
1. Add back the line #include <iostream>
2. Select [Build]->[Build Solution]
Without the step 2, the error list is still there.
31
Syntactically-incorrect program
HelloWorld_NoNamespace.cpp
1.
// Example of syntax error.
2.
3.
4.
5.
6.
7.
8.
#include <iostream>
// Forgot "using namespace std;"
int main()
{
cout << "Hello World!" << endl;
cout << "Hello World Again!" << endl;
return 0;
9.
10.
}
// exit program
Compare to the error list on the last
slide.
Same error (e.g., cout is undeclared)
can be due to different bugs.
32
Syntactically-incorrect program
HelloWorld_Error1.cpp
// Example of syntax error.
#include <iostream>
using namespace std;
int main()
{
cout << Hello World!<< endl;
cout << Hello World Again!<< endl;
return 0;
}
// exit program
How to fix the
syntax errors?
33
Syntactically-incorrect program
HelloWorld_Error1.cpp
1.
// Example of syntax error.
2.
3.
4.
#include <iostream>
using namespace std;
5.
6.
7.
8.
9.
int main()
{
cout << Hello World! << endl;
cout << Hello World Again! << endl;
The texts to be
output should
outputted
should
be
placed
be
placed
in ainpair
a
of double
pair
of double
quotes
quotes.“ texts”.
10.
return 0;
11.
12.
// exit program
}
34
Syntactically-incorrect program
HelloWorld_Error2.cpp
1.
// Example of syntax error.
2.
3.
4.
5.
6.
7.
8.
9.
#include <iostream>
using namespace std;
int main()
{
cout < "Hello World!" < endl;
cout < "Hello World Again!"
< endl;
return 0;
10.
11.
}
// exit program
How to fix the
syntax errors?
35
Syntactically-incorrect program
HelloWorld_Error2.cpp
1.
// Example of syntax error.
2.
3.
4.
#include <iostream>
using namespace std;
5.
6.
7.
8.
9.
int main()
{
cout < "Hello World!" < endl;
cout < "Hello World Again! “ < endl;
< is not an
operator of
cout. We need
to use <<.
10.
return 0;
11.
12.
// exit program
}
36
Syntactically-incorrect program
HelloWorld_Error3.cpp
1.
// Example of syntax error.
2.
3.
4.
#include <iostream>
using namespace std
5.
6.
7.
8.
9.
int main()
{
cout << "Hello World!" << endl;
cout < "Hello World Again!" < endl;
10.
return 0;
11.
12.
}
// exit program
How to fix the
syntax errors?
37
Syntactically-incorrect program
HelloWorld_Error4.cpp
1.
// Example of syntax error
2.
3.
4.
#include <iostream>
using namespace std;
5.
6.
7.
8.
9.
int main()
{
cout << "Hello World!" << endl
cout << "Hello World Again!" << endl
10.
return 0;
11.
12.
}
// exit program
How to fix the
syntax errors?
38
Syntactically-incorrect program
HelloWorld_Error5.cpp
1.
// Example of syntax error
2.
3.
4.
#include <iostream>
using namespace std;
5.
6.
7.
8.
9.
int main()
{
cout << 'Hello World!' << endl;
cout << 'Hello World Again!' << endl;
10.
return 0;
11.
12.
// exit program
}
Use double quotation
marks
How to fix the
syntax errors?
39
Syntactically-correct program
HelloWorld_extraSemiColon.cpp
1.
2.
3.
4.
#include <iostream> ;
using namespace std;
5.
6.
7.
8.
9.
int main()
{
cout << "Hello World!" << endl;
cout << "Hello World Again!" << endl;
10.
return 0;
11.
12.
}
// exit program
No error.
40
Syntactically-correct program
HelloWorld_extraSemiColon.cpp
No error.
;; means that there is
no statement in
between the two
occurrences of ;.
41
Yet_Another_HelloWorld.cpp
/* This is also a comment. The compiler ignores this comment. */
/* This is a multiline comment.
The compiler also ignores comments.
*/
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!\n";
cout << "Hello World Again!\n";
return 0;
// exit program
}
42
Comments and Programming Style

Having a good programming style is required.

Indent when appropriate. You will develop a look-andfeel on this as you read more program code listing.

Place comments to explain your code. Use them to
describe what the program / statements / functions
does, etc.
// ... is for single line comments
/* ... */ are for multi-line comments

Comments are unseen by the compiler. They have no
43
effect to the behavior of your program.
C++ Program Construction (so far)
C++ Program.cpp =
Declaration
Examples
#include <iostream>
…
using namespace std;
FunctionDefintion
…
FunctionDefintion =
Heading
Block
int main()
44
C++ Program Construction (so far)
Examples
Block =
{ Statement
cout << “Hello Class“;
cout << “Timeout!“;
……
}
{
}
cout << “Hello Class“;
Statement =
Null statement
Return statement
Output statement
Block
One of them
;
return 0;
cout << “Hello World“;
{ cout << “Hi and Bye“;}
45
Positions of C++ Comments
C++ Program.cpp =
…
// comment here possible
Declaration
// comment here possible
Block =
// comment here possible
{
// comment here possible
// comment here possible
Statement
// comment here possible
…
// comment here possible
// comment here possible
FunctionDefintion
// comment here possible
Statement
// comment here possible
// comment here possible
…
} // comment here possible
// comment here possible
46
Download