CS计算机代考程序代写 SQL data structure javascript c/c++ database compiler Java c++ assembly Computer Programming

Computer Programming

Introduction to Programming

About the course

2

! Lecturer & Tutor
! Dr. Kenneth Lee.

.hk

! 2 Hours of lecture session per week
! Lectures will focus on background knowledge, theories, syntax and examples

! 2 Hours of lab session per week
! You’ll have hands-on chance to practice what you’ve learnt in lectures
! You’ll upload your work online (PASS3 System) to check for correctness

About the course

3

!Coursework (40%)
! One Quiz (20%), about week 8, during lecture

! reflect on understanding of programming concepts and
problem solving techniques.

! Assignments: (15%)
! 3 assignments
! analyze more challenging problems
! implement and present solution.

! Lab Exercise: (5%)
! Deadline is one week after the lab

About the course – Assessment
! Exam (60%)

! 2 hours exam, 7-8 short questions.
! Program construction
! Program debugging
! Derive program output…

! To pass the course you must:
! Obtain at least 30% of the maximum mark on the exam.

4

About the course – Course Outcomes

5

1. Explain the structure of an object-oriented computer
program;

2. Analyze, test and debug computer programs;

3. Solve a task by applying effective programming techniques,
which involve advanced skills like using recursion and
dynamic data structures;

4. Design and construct well-structured programs with good
programming practices.

About the course – Resource

6

! Course website on Canvas
! Lecture slides
! Tutorial exercises
! Announcements, Course Info…etc.

! Microsoft Visual Studio (Windows)
! Develop environment for compiling & debugging programs
! Latest version is VS2019 (2022 Preview), but in fact any version is fine
! More information during Tutorial
! There will not be any tools-specific question in midterm/exam, so feel free

to use any tools you prefer (e.g. XCode, Dev-C++, gcc….)

! PASS (Program Assignment aSsessment System)
! Program testing and submission

! Change password after first login
! 2 ways to access: thorough main page of CS department,

or directly via https://pass3.cs.cityu.edu.hk/index.jsp

gchange program to exex us
online

Learning Material
! Textbook

! Lecture slides and tutorial exercises will be released online via Canvas

! Like other science courses, we focus on practical skills rather than moving
words across papers (memorization). Depending on your study style, lecture
slides, PLUS adequate practice, could be enough to get a good grade in this
course. If you prefer textbook, you may refer to online books like:

! Fundamentals of
Programming C++
(by Richard L. Halterman)

https://freecomputerbooks.com/Fundamentals-of-Programming-Cpp.html

About the course – Academic Honesty
! CityU has revised Rules of Academic Honesty and has required all students to complete

an online tutorial on subject and declare your understanding

! Plagiarism…
! It is serious fraud to plagiarize others’ work. (will be treated as discipline violation).

! How to prevent plagiarism…
! Finish the assignments by yourself! You can discuss, but let’s write by yourself.
! Protect your code; don’t generously give it away as “reference” copy.
! In plagiarism cases, we treat the supplier and the copier as both guilty.
! You hurt your own grades by not reporting cheating.

! As instructors…
! We have responsibility to report academic dishonesty cases so as not to

compromise the quality of education
! We take suspected plagiarism cases very seriously.

8

About the course – Academic Honesty

! Although the feature is not accessible by students, but in fact PASS3 can
automatically generate a report of code similarity after deadline:

! Renaming variables, adding comments, or exchanging block orders…etc
have no use. PASS could still spot the plagiarism groups

About the course – Really important

! Ask questions!
! Let us know your difficulties. Is the lecture too fast, or

too slow?
! We can teach better, you can learn better
! During the lecture, during the lab, and on Canvas

! Practice independently!

! Useful online resources:
! https://courses.cs.cityu.edu.hk/clinic/
! https://en.cppreference.com/w/
! http://stackoverflow.com/

10

About the course – Course Schedule (tentative)

11

Wk Lecture Topic Tutorial Topic Assessment

1 Introduction, simple programs Intro to Visual Studio

2 The C++ programming language, operators,
data Types

Simple programs & PASS

3 Simple programs & operators

4 Flow control (if, switch) Flow control (if, switch)

5 Flow control (for, while) Flow control (for, while)

6 Arrays (1D and 2D) Arrays Assign. 1 Due

7 Revision and Discussion on Assignment Intro to VS Debugger

8 Midterm Quiz Discussion on Midterm Quiz

9 Functions Functions

10 Class and Object Class and Object Assign. 2 Due

11 Class and Object Class and Object

12 Strings Strings

13 File I/O, Other topics (if time allows) File I/O Assign. 3 Due

Programming is…
! Computer is considerably different from other electronic

devices in the sense that the same device could perform
different functions depending on the software being run.

Compu
ter runs

Program
s !

Programming is…

! Program is a list of commands that instructs a computer
to do some tasks

13

Programming is…

! A Program is actually a (prepared) collection of commands /
instructions we ask the computer to follow

! Instruction in English? … No…Not Yet (maybe 50 years later?)
! Since computer cannot understand human language, we need to

program in a syntax that computer can understand
! However, different dialects (programming languages) exists..

JavaScript
alert(“Hello”);

C++
cout<<"Hello"; C printf("Hello"); Computer Japanese ̢͔̬̰̔ Chinese Ἀ⤥ English Hello Human So which language should I use? ! There are 50+ different programming languages around ! Some of them are general-purpose (C++, Java, Pascal etc.) ! Some are particularly designed for special purpose: ! SQL for database query ! JavaScript for webpage manipulation / interaction ! ActionScript for animation with Flash ! So the answer depends on the task you have in mind: Text-based console program? Window based program? or.. to write a game? C++ we use in this course is considered Third-Generation Programming Lang. History of Programming Languages 16 Machine Language Directly understood by computer (but…not by human) Assembly Language Abbreviations / symbols replacing machine lang. (somewhat understandable) High-level Language Close to human language, allows complex expressions binary code assembly language C, C++, Java, Basic Answer = a+b*c; Write(Answer); What could C++ achieve ? ! As mentioned, C++ is general purpose, it could produce program in whatever platforms and OS: ! Type: Console or Graphic, even Games! ! Platform: Intel / Apple Machine…etc, even DSP (Signal Processor)! ! OS: Window, Linux, MacOSX, even embedded platform! Console Program! Input from Keyboard (no Mouse) Output TEXT to screen or file (no graphics!) What could C++ achieve ? Input: Keyboard, mouse, camera…etc Output: Text, graphics and images...etc Window-based Program! Windows program looks so nice, however, the detail syntax varies greatly from system to system. i.e. if an experienced Windows Programmer wants to write Programs in MacOS, he need to RE-LEARN EVERYTHING !!! What could C++ achieve ? Besides direct input and output, C++ can also be used to write programs on DSP or Embedded System • Signal Processing •Remove Noise / Magnify...etc • Decision Making • Handle Communication Recent models from TI or Motorola are already providing Development platform in C++ Embedded Program! What you will learn from this course ? ! Learn important concepts of programming like variables, expressions, functions, loops, arrays. (the jargons will be explained in later weeks) ! We use C++ to demonstrate the concepts. After you finish this course, the concepts you learnt would make the study of other languages (e.g. Java / PHP...etc) easier. ! Learn how to write a text-based console program ! Learn how to perform simple calculation and decision making ! Learn the basis of Object-Oriented programming ! Learn how to manipulate memory and strings ! Learn how to work with text files (if time allows) ! Learn how to work with development tools like Compiler & Debugger What you will NOT learn from this course ? ! ALL syntax of C++ ! We teach on necessity basis, concepts / techniques with relatively low importance are skipped (e.g. Standard Template Library) ! ALL libraries of C/C++ ! Again, on necessity basis, we teach string, math, input/output, but not teaching: time, communication, system shell…etc. ! Platform Specific Syntax ! Console program works everywhere (Windows, Linux…etc), we will not teach Windows-Only syntax like DirectX or ActiveX ! ALL features of MS Visual Studio ! For the sake of presentation, we stick to Visual Studio in tutorials, but actually students may feel free to use whatever development tools, like Dev-C++, gcc or XCode. What does a C++ program look like? C++ program is actually a text file containing English-like commands (but the syntax is rigid/fixed) The program contains multiple building blocks enclosed by { } (called functions) Among which there must be a function having the name: main () Inside a block, there’re multiple commands, each must ends with semi-colon ; 1 2 3 Besides commands, there’re also statements talking about the data we manipulate (e.g. user name, hour…) Just like recipes, both steps & ingredients (and amount) are clearly written. 4Rawmaterials 509171 username.ly output something Oj KEITEEABAEFULEE How to create C++ program? ! In theories, only a text editor (e.g. Nodepad) is needed. ! But in the reality, we use better tools. Usually programmer uses a special program called IDE (Integrated Development Environment) ! An IDE, as an ideal tool, should contains: 1. A nice text editor, usually with syntax highlight / hints 2. Compiler which converts source code (*.cpp) to program (*.exe) 3. A debugger which allows you pause program and investigate data MS Visual Studio Besides Visual Studio, there are other choices, like: Eclipse, XCode, CLion…etc. I EE EEE software Étingguietons ranslater g peppy j translate program Runthe program he byline h I db l The journey of a C++ program ! Since computer can understand machine language but not English-like High Level Language like C++, translation is needed. ! The process of converting C++ source code (text file in *.cpp) to executable program (*.exe) is called compilation ! The program which performs compilation is a compiler ! As easy as ? *.exe Compilation (e.g. with Visual Studio) *.cpp Cook Rice Dinner Almost correc t! but NO T exact ly cppish I onlinegdb for online programming You cook everything yourself? ! The flow of (*.cpp)-> compile -> (*.exe) is almost correct.
(actually in small scale compiler like TCC, this is the case!)

! However for big projects in real-life, the flow is slightly different!
! There may be more than one cpp files, merged together to form exe
! Some of the program code may not be written by you.

! When you observe different programs, you’ll notice something common:
! Almost 100% console program prints to screen and read from keyboard
! The majority of programs reads file
! There are many programs doing calculation like sin, cos, tan, square root
! Many window programs need File-Open, Save and Print dialogs…etc.

! Noticing that there’re many common portions, it would be a waste of
time if you code it again and again and again

! So C++ language prepared those “common features” for you so that you
can use them directly to save time. It is called LIBRARY

Fade code

Standard Libraries

! Library is a collection of prepared program code so that you could use
directly without writing it yourself – SAVE TIME!

! The situation is just like cooked food your brought – EAT without cooking!
! Libraries came from different sources:

! By other programmers, free or charged (e.g. your teammate in software house)
! By operating system (e.g. DirectX, ActiveX by Microsoft)
! Provided by C++ language by default! (most common, we call it standard library!)

! Example of standard libraries:
! iostream: For input (from keyboard) and output (to screen)
! fstream: For reading/writing files
! string: For searching characters / substrings, copying…etc
! math: For functions like sin, cos, tan, squart root
! …etc

! Almost 99% C++ program need some libraries some way

I 1 de

rented a
dparty

or library willstateclearly can use or not

Flow revisited…

Dish1

DinnerDish2

Pre-cooked food

Cook

Cook

Flow revisited…

*.obj

*.lib

Compile

Compile
*.exe

main cpp

Possibly some
other cpp

*.obj

Standard
Library

Link

Final Program

The journey of a C++ program

! Writing source code in C++.
! e.g. hello.cpp

! Compilation
! 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).

29

Different types of possible errors
! When you compile your C++ program (e.g. with VS), it will help you to

check for (some!) program errors. (don’t over-rely on it…)
! Program Error is classified into THREE types:
! Syntax Error:

! Syntax/Structure not correct, caused by typos, wrong punctuations/case
! Example in English: I a boy is.
! Example in C++: Cout<"Hi (it should be: cout<<"Hi"; ) ! Semantic Error: ! Syntax correct but the meaning / logic is wrong ! Example in English: I ate an aeroplane. ! Example in C++: Total=Price+Quan; (it should be: Total=Price*Quan; ) ! Runtime Error: ! Unexpected situation not detectable when the program is written/compiled ! Example: you asked user to type a number, but user typed Hello (a string!) ! Compiler can detect ONLY Syntax Error. Yiseive 0 0 0 Sample Program (Framework) 31 cout int Simple Program 32 /* The traditional first program in honor of Dennis Ritchie who invented C at Bell Labs in 1972 */ #include
using namespace std;

int main()
{

cout << "Hello, world!\n"; return 0; } E FEE Y D 4 7 KRIEGER void IDELEETENE for No output Things you need to know before we start… /* The traditional first program in honor of Dennis Ritchie who invented C at Bell Labs in 1972 */ #include
using namespace std;

int main()
{

cout << "Hello, world!\n"; return 0; } 1 In C++, letter case is important! Writing Int Main() is WRONG! The main() function has to return an integer (to the operating system) upon completion. 0 means finished successfully without error 4 2 Most statements (not all) ends with Semi-colon ; Punctuations are important! Writing ‘Hello, world!\n’ (using single quotation) is WRONG! 3 Comments 34 /* The traditional first program in honor of Dennis Ritchie who invented C at Bell Labs in 1972 */ ! Enclosed by /* and */ or begin with // ! // single line comments // this is a single line comment // each line must begin with "//" sign Only for human to read (as reference) Ignored by computer É O method2 to markcomments Preprocessor directive 35 ! Give information / instruction to compiler for program creation #include

! People call this line “Preprocessor directive”
! Tells computer to load contents of a certain file / library
! In this program, we include the library iostream into the program

as it contains the definition of cout which is used to print something
to the screen.

! IO means Input + Output, you need this line when you read from
keyboard (cin), or prints to screen (cout).

! We need this line for almost ALL of our programs in this course
! If you’re not sure, ADD IT. (It does no harm if finally you don’t use I/O)
! No semi-colon at the end of the include directive
! No space inside < >
! Letter case is important. The word include is always in lower case

i ostream

I Kiri At
A

includeCiosere

Namespaces
using namespace std;
! As you’ll see soon, you can create variables to store information
! Variable is similar to the unknowns in Math (e.g. x, y…)
! You may give almost any name to variables (under some restrictions…)
! Can I call a variable cout ? Yes, why not?
! If you do so, AMBIGUITY: your cout mix with the cout in library…
! Solution: Use a prefix! (we call it namespace)

! English: My Book, Peter’s Book, Library’s Book …..
! C++: std::cout (std is standard library, :: is similar to ‘s)

! But adding prefix to everything is troublesome!

• using namespace std means, by default the prefix is std, the cout I
wrote is actually std::cout

! We need this line for ALMOST ALL programs in this course

TEA LATTE PEA using

Functions

37

! When writing program, programmer usually group
related code (instructions) into functions for easy
design and maintenance.

! You may create as many functions as you like, but at
least we need one: int main()

! We will talk about function and how to write your
own function in later lectures.

I course It a using
namespace st

Function – main

int main()
{

}
! A program MUST contain exactly one main() function.

! Location is unimportant, can be top / middle / bottom of .cpp file

! Is the entry point of the program (the first line run by computer)

main()

main()

main()

Example1.exe

Calculator.exe

texteditor.exe

38

Function – main

39

! int main()

! int means that the return value is integer (detail in later weeks)
! no semi-colon after main()
! C++ is case sensitive.
! Incorrect: int Main(), INT main(), …

! { }

! Braces: left brace begins the body of a function. The
corresponding right brace must end the function

Simple Program

40

/* The traditional first program in honor of
Dennis Ritchie who invented C at Bell Labs
in 1972 */

#include
using namespace std;
int main()
{

cout << "Hello, world!\n"; return 0; } Object - cout 41 cout << "Hello, world!\n" ; ! Object is a programming unit that stores values (attributes) and provide functions (methods) to manipulate the values (more details in future classes) ! cout: object provided by iostream library for screen (console) output ! <<: output (also called insertion) operator that output values to an output device. In this case, the output device is cout (the screen) ! The value on the right hand side of the operator is the data (string, numbers…) you want to output ! Common mistakes: ! Typo or wrong cases: count, cuot, Cout….etc. ! Wrong arrow direction: cout >> “Hello”;

! Note that C++ is NOT English! marks will be deduced upon typo / wrong case

Meanings of quotation marks

! Content enclosed by double quotation mark is a string
(collection of characters)

! A String can has zero, one, or up to millions of characters

! “ABC”: ‘ABC’ (single quote) is WRONG!!

! Don’t mix it up with single quotation, which means character
(i.e. length of which MUST be one, cannot be zero or two+)

! C++ don’t really care what you put in a string (inside double
quotation), simply use any spacing / case you need.

! Numbers used for calculation should NOT be quoted
(so, x = ‘3’ + ‘5’ will NOT give 8 as result)

A B C ↵

At
Tr TEEtext

cout – console output

! Content after cout<< will be displayed, so cout<<"Hello, world!"; shows: (That "Press any key" is added by Visual Studio, simply ignore it…) ! You may also print number: cout<<123; ! Result of cout immediately follows the previous output, it would not print on a new line by default. ! So cout<<"Hello" << 123; gives the same output as the two lines: cout<<"Hello"; cout<<123; Summary 44 ! Basic components of a computer program are: ! Instructions ! Logic Flow ! Variable and Constant ! A correct logic is important in programming ! Programmer usually reuse code written by the others and the code is commonly in form of library ! cout is an object provided by iostream package for screen output Appendix : How to print “Special characters” ? 45 ! Question 1: How to print new line? ! Question 2: String is enclosed by double quote, then how to print the double quotation mark " itself? ! Solution: Escape sequence ! Escape sequence is: ! Used to print special characters (, , quotation mark…)
! Always starts with \, followed by another character
! This \ and the next character is combined AS A SINGLE CHARACTER

! \n : New Line (i.e. )
! \” : double quotation mark
! \t : , used to align data tidily
! \\ : backslash (since \ is borrowed to escape sequence…)