Download CPSC 111

Document related concepts

Structured programming wikipedia , lookup

Go (programming language) wikipedia , lookup

Computer cluster wikipedia , lookup

Abstraction (computer science) wikipedia , lookup

Parallel computing wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

C syntax wikipedia , lookup

ILLIAC IV wikipedia , lookup

Transcript
Welcome to CPSC 206
Structured Programming in C
1
Lecture Information
http://people.cs.tamu.edu/ychen/Teaching/CPSC206
2
Midterm 1
Time: Sept 30, Thur., 8:00-9:15AM
Location: HRBB 124
Closed book
 No textbook, No notes, No calculator
You are required to bring:
 Answer sheet: SCANTRON (NCS MP90051 or 0101607-TAMU) (You can find it in MSC
Bookstore)
 No. 2 pencils, eraser
 Student ID
3
Review 1:
Topic 1: Introduction to Computer Science
Topic 2: Overview of C Language
Chapter 1
Section 1.1 – 1.8
Chapter 2
Section 2.1 – 2.12
4
Topic 1: Introduction to Computer Science
Part I: an overview of computer science.
Part II: computer hardware and software.
Part III: computer languages.
5
Introduction to Computer Science
Part I: an overview of computer science.
 What is computer Science?
 Basic idea about research areas in computer science.
 Architecture
 Operating System
 Computability theory
 Algorithm design
 Programming Language
 Compiler
 Complexity theory
6
Introduction to Computer Science
Part I: an overview of computer science.
Basic idea about research areas in computer
science. (contd.)
 Human-computer interaction
 Artificial Intelligence
 Software engineering
 Networking, Parallel and Distributed System.
7
Introduction to Computer Science
Part I: an overview of computer science.
Sample Question
Software engineering focuses on how to
develop applications for networks.
True of False
False
8
Introduction to Computer Science
Part II: computer hardware and software
 Computer organization.





Data representation
Main Memory
Central Processing Unit (CPU)
Secondary Storage
Input / Output
 Solving problems on computers.



System Design
Algorithm Development and Representation
Programming
9
Computer Organization
Data Representation
 How data is stored and processed?
All data is stored and processed in binary form,
that is, as a series of 0s and 1s.
The definition of Bit, Byte, Word
Sample Question
The number of bits in one Bytes is
a) 4 b) 8
above
c) 16 d) 32 e) none of the
b
10
Computer Organization
Data Representation
 Binary number
Convert a decimal number to a binary number
What is the decimal value of a binary number
Sample Question
The decimal value of a binary number 100001 is
a) 33 b) 1
c) 100001
a
d) 17 e) none of the above
11
Computer Organization
Data Representation (contd.)
 How a signed number is represented in binary
form?
In a binary representation, the leftmost bit is a
sign bit followed by the magnitude bits.
Sign-bit: 0 implies a positive number, 1 implies
a negative number.
 How character is represented? (ASCII coding
system)
Each character is represented in a 7-bit format.
12
Computer Organization
Sample Question
Consider the data representation of signed integers
using 4 bits, in which one bit is used as the sign
bit. The maximum decimal integer that can be
represented is ___
a) 1111
b) 111
the above
c) 7
d) 15 e) none of
c
13
Computer Organization
Memory:
 What is memory?
It is part of a computer’s electronic circuitry
holding the binary data which is processed by
the computer’s program.
14
Computer Organization
CPU:
 What does CPU stand for?
Central Processing Unit (CPU)
 What is
Control Unit, ALU, Program Counter,
Instruction Counter?
 What is program? How a program is executed?
Machine Cycle
 Hardware characteristics:
Clock speed
MIPS
15
Computer Organization
Central Processing Unit (CPU)
CPU reads program instructions from main memory.
CPU executes one instruction at a time until completion.
CPU
Control unit
Control and
coordinate the
computer’s
operations.
Arithmetic/Logic Unit (ALU)
performs all arithmetic
computations and logic
operations.
16
Computer Organization
Central Processing Unit (CPU) (contd.)
Program Counter (PC)
Instruction Counter (IC)
contains the memory
address of the
instruction that is
currently being
executed.
contains the instruction
currently being
processed.
17
Computer Organization
Central Processing Unit (CPU) (contd.)
 What is program?
Program is a set of instructions
 How a program is executed?
Program is loaded into memory.
In each Machine Cycle:
o Fetch, Decode, Execute
Machine Cycle is repeated until completion.
18
Computer Organization
 Central Processing Unit (CPU) (contd.)
 In each Machine Cycle:
Fetch:
o The control unit loads the instruction pointed
to by PC to IC.
o PC is updated to the address of the next
instruction.
Decode: control unit decodes the fetched
instruction and determines the required action to
take.
Execute: control unit activates the appropriate
circuitry.
19
Computer Organization
Central Processing Unit (CPU) (contd.)
 Hardware characteristics:
The speed of a computer can be quoted as:
o Clock speed, e.g. 100MHz means 100 million
cycles per second.
o MIPS, a million instructions per second.
20
Computer Organization
Secondary Storage:
 Mass storage devices
Sequential storage: tape.
Random storage: floppy disk, zip disk, CDROM, etc.
 Static storage.
it doesn't require power to retain stored data.
 Slower speed.
21
Computer Organization
Input
 Keyboard, mouse etc
Output:
 Monitor, printer etc
22
Computer Organization
How CPU, Memory, Storage, Input and
Output work together?
 IPOS Cycle
23
Computer Organization
I - Input
P - Process
O - Output
CPU
Input
Device I
P
S - Storage
Binary Representation
Memory
Data/
Program
I
Program/
Program
S output
O
Output
Device
Program
output
O
Secondary Storage
24
Introduction to Computer Science
Part II: computer hardware and software
 Computer organization.





Data representation
Main Memory
Central Processing Unit (CPU)
Secondary Storage
Input / Output
 Solving problems on computers.
 System Design
 Algorithm Development and Representation
 Programming
25
Solving problems on computers.
System Design
 Top-down
break the problem into smaller and smaller subproblems until they can be solved trivially.
 Bottom-up
start by designing the low-level details, and then
decide how these will be put together to create
the entire system
26
Solving problems on computers.
Algorithm Development and Representation
 What is algorithm?
a specification of the series of steps which
must be followed in order to solve a problem or
accomplish a task.
 Algorithm Representation:
Pseudo Code
Decision Trees
Flow Charts
27
Solving problems on computers.
Programming
 What is programming?
Programming is the process of translating a
problem’s solution into instructions that a
computer can process.
28
Solving problems on computers.
Programming Paradigms
Procedural Programming is based upon the concept of
the modularity.
A main procedural program is composed of one
or more modules. Each module is composed of
one or more subprograms.
Declarative programming
describes to the computer a set of conditions
and
lets the computer figure out how to satisfy
them.
29
Solving problems on computers.
Programming Paradigms
 Object oriented programming
A computer program is composed of a
collection of individual units, called objects.
Operations are provided for each class of
objects.
Operations change the state of an object.
To make the overall computation happen, the
objects interact through their own operations
and their own data.
30
Introduction to Computer Science
Part III: Computer Language
Why Computer needs language?
 Computer requires an unambiguous language
 Computer Language are designed to be
unambiguous.
Precedence and associativity determine
precisely how expressions are evaluated.
31
Introduction to Computer Science
Part III: Computer Language
C language
 C is a general purpose programming language.
 C is a middle level language.
It combines the elements of high-level
language with the functionality of low-level
language.
 C is a structured language.
It allows programmer to divide program into
modules.
32
Introduction to Computer Science
Part III: Computer Language
There is only one programming language that a
computer can actually understand and execute: its
own native binary machine code.
Languages are classified as
low level if they are close to machine code
and
high level if each language statement
corresponds to many machine code instructions.
33
End of Review of Topic 1
Introduction of Computer Science
34
Chapter 1
 Review Outline
1. A brief history of C
2. Features of C
3. Get Ready to Program
4. General form of a simple program
 main function
 variable declarations
35
Chapter 1
 Review Outline (contd)
5. initialization
6. preprocessing directive
 include
 define
7. printf and scanf
 printf
 \n
 scanf
8. while statement
36
Chapter 1
1. A brief history of C




1960s, CPL (Combined Programming Language)
 capable of both high level machine independent
programming and would still allow the programmer to
control the behavior of individual bits of data.
 too large for use in many applications.
1967, BCPL (Basic CPL): a scaled down version of CPL.
In 1970, B: a scaled down version of BCPL
 written specifically for use in systems programming.
In 1972, C
 Dennis Ritchie returned some of features of BCPL to
the B language in the process of developing C.
37
Chapter 1
2. Features of C
 Small
 The core language of C is small;
 Non-essential functionality, such as math functions or file
handling, is provided by a standardized set of library
routines.
 Portable:
 easily moved from machine to machine.
 Powerful:
 it is able to access low level commands
 Useful in writing system software
 Procedural Programming paradigm
 Allows programmer to divide program into modules.
38
Chapter 1
 3. Get Ready to Program
 The Programming Process
 Specify the task
 Discover an algorithm for its solution
 Code the algorithm in C
 Test the code
 The cycle of the programming process
edit
compile
pico / vi
gcc / cc
execute
39
Chapter 1
 4. General Form of a simple program
The general form of a program
preprocessing directives
int main ()
{
declarations
statements
}
40
Chapter 1
 4. General Form of a simple program (Cont’d).
 main function
 Every program has a main function.
 The execution starts at main function.
 Declaration
 All variables in a program must be declared
before they can be used.
 The data type of a variable specifies what kind
of data can be stored in the variable.
41
Chapter 1
 4. General Form of a simple program (Cont’d).
Sample Question
int main(){
int c1=4, c2=2;
printf(“Sum: %d\n”, sumc);
}
What is the output?
a) 6
b) 4
c) sumc
d) error
d
42
Chapter 1
 5. Initialization
 A variable can be initialized when it is declared.
 Constants or constant expressions can be used to
initialize a variable.
 Declared variables can be used to initialize a
variable.
 a variable cannot be used before it has been
declared.
43
Chapter 1
 6. preprocessing directive
 #include “filename”
 The preprocessor replaces the line with a copy
of the named file.
 #define A B
 It affects only those lines in the file that come
after it.
 All occurrences (after this line) of the identifier
A, except in quoted string, will be changed to B.
44
Chapter 1
 6. preprocessing directive (Cont’d)
Sample Question
#include <stdio.h>
#define Sumc c1+c2+c3
int main(){
int c1=4, c2=2;
printf(“Ave: %d\n”, Sumc);
}
d
What is the ouput?
a) 6
b) 4
c) sumc
d) error
45
Chapter 1
 6. preprocessing directive (Cont’d)
Sample Question
#include <stdio.h>
#define Sumc c1+c2
int main(){
int c1=4, c2=2;
printf(“Ave: %d\n”, Sumc);
}
a
What is the ouput?
a) 6
b) 4
c) sumc
d) error
46
Chapter 1
 6. preprocessing directive (Cont’d)
Sample Question
#include <stdio.h>
#define Sumc c1+c2
int main(){
int c1=4, c2=2;
printf(“Ave: %d\n”, Sumc/2);
a
}
a) 5
b) 3
c) 4
d) 2
47
Chapter 1
 7. printf and scanf
 Arguments: control_string and other_arguments
 control_string contains formats, called
conversion specifications, which are
matched with other arguments.
48
Chapter 1
 7. printf and scanf (cont’d)
 printf: How to specify format using conversion
specification?
 %field_widthconversion_character
 %field_width.precisionconversion_character
 Example:
o printf(“%7.2f”, 1.221);
o printf(“%7d”, 1221);
49
Chapter 1
 7. printf and scanf (cont’d)
 printf: How to specify format using conversion
specification?
 %field_widthconversion_character
 %field_width.precisionconversion_character
 conversion_character: how the data is printed?
o
o
o
o
c: character
d: decimal integer
f: floating-point number
s, e, g table of conversion character, page 16
50
Chapter 1
 7. printf and scanf (cont’d)
 printf
Sample Question
What is the output?
pirntf(“%8.2f”, 1.234);
d
a)_ _ _ 1.23 b) 1.234 c) _ _ _1.234 d) _ _ _ _ 1.23
51
Chapter 1
 7. printf and scanf (cont’d)
 \n: newline
Sample Question
#include <stdio.n>
c
int main(void)
printf(“%8.2f\n\n\n”, 1.234);
printf(“%8.2f\n\n\n”, 1);
printf(“ \n%d ”, 2);
return 0;
}
if 1.234 is printed at line 1, which line is 2 written?
a)5
b) 7
c) 8
d)6
52
Chapter 1
 7. printf and scanf (cont’d)
 scanf:
 Arguments: control_string and other_arguments
 control_string: conversion specifications
o conversion_character: how the data is read?
 c: character
table of conversion, page 18
 d: decimal integer
 f: floating-point number
 lf: floating-point number (double)
 Lf: floating-point number (long double)
 s: string
 other_arguments are addresses
53
Chapter 1
 7. printf and scanf (cont’d)
 scanf
 Example: scanf(“%d”, &x);
 &: address operator
 %d — how to interpret the input stream
o the input characters typed at the keyboard is
interpreted as a decimal integer
 &x — where to store the value
o the value of the decimal integer is stored at the
address of x.
54
Chapter 1
 7. printf and scanf (cont’d)
 scanf
Sample Question
a
Which format should be used to read a long double
value?
a) %Lf
b)%lf
c) %f
d) %L
55
Chapter 1
 8. while
while (expression)
statement
statement is executed as long as the value of
expression is true
 true: any non-zero value
56
Chapter 1
 8. while (cont’d)
/* Some powers of 2 are printed. */
#include <stdio.h>
int main(void)
{
int e = 0, power_of_two = 1;
}
How many times the body of
the loop is executed?
while (++e <= 10)
printf("%5d", power_of_two *= 2);
printf("\n");
return 0;
57
End of Review of
Chapter 1
58
Chapter 2
 Review Outline
1.
2.
3.
4.
Comments
Keywords
Identifiers
Constants
 floating numbers
 integer constants
59
Chapter 2
 Review Outline (cont’d)
5. String Constants
6. Operators and Punctuators
 Precedence and Associativity of Operators
 Division
 Modulus
 Increment and Decrement Operators
 Assignment Operators
7. C System
60
Chapter 2
1. Comments
 What is comment?
 Arbitrary strings of symbols placed between
the delimiters /* and */.
 Single line comment: // text
 The compiler changes each comment into a
single blank character.
 Rules:
 Multi-line comments cannot be placed in the
middle of a keyword or identifier.
 Multi-line comments may not be nested.
61
Chapter 2
1. Comments (cont’d)
Sample Question
a
Which of the following is correct?
a) /* comment //a single line comment */
b) int main(v/* comment*/oid)
c) int var/*comment*/1;
d) /* comment /* comment */ */
62
Chapter 2
2. Keyword
 What is Keywords?
 Keywords are explicitly reserved words that
have a strict meaning as individual tokens in
C.
 Examples of Keywords
 Data Type: int, char, long, short
 Keywords cannot be redefined or used in other
contexts.
The table of keywords, page 46
63
Chapter 2
3. Identifier
 What is identifier?

The names of variables, functions, labels and other
user-defined items are called identifier.
 Special identifier

Keywords, names of functions in C library, main
 Rules:
1.
2.
3.
4.
composed of letters, digits, and underscore _ .
The first character must be a letter or underscore.
case-sensitive
would not be defined as the special identifiers:
64
Chapter 2
3. Identifier (cont’d)
Sample Question
d
Which of the following is incorrect?
a) int comment;
b) int _int;
c) int int_;
d) int 4int;
65
Chapter 2
4. Constants
1. Floating constants
 float: an F suffix
o 1.22F
 double: unsuffixed floating constant
o 1.22
 long double: an L suffix
o 1.22L
2. Character constants
1. ‘A’
66
Chapter 2
4. Constants (cont’d)
 integer constants: decimal, octal, hexadecimal
 Representation of integer constants:
 Decimal integer: 17
 Octal integer: 017
 Hexadecimal integer: 0x17
 Conversion between different types:
 an octal integer <-> a decimal integer
 a hexadecimal integer <-> a decimal
integer
67
Chapter 2
4. Constants (cont’d)
 A review of binary number
 What is the decimal value of a binary integer?
 A binary integer a= ik-1 ik-2 …..i0
o The decimal value is
 ik-1* 2k-1 + ik-2 * 2k-2 + …..+ i0 * 20
 Example:
o The decimal value of a binary integer
a = i3i2i1i0=1101 is
 1 * 23 + 1 * 22 + 0* 21 + 1 * 20 =13
68
Chapter 2
4. Constants (cont’d)
 Octal Integer:
 What is the decimal value of an octal integer?
 An octal integer a= ik-1 ik-2 …..i0
o The decimal value is
 ik-1* 8k-1 + ik-2 * 8k-2 + …..+ i0 * 80
 Example:
o The decimal value of an octal integer
a = i3i2i1i0=7121 is
 7 * 83 + 1 * 82 + 2* 81 + 1 * 80 =3665
69
Chapter 2
4. Constants (cont’d)
 Hexadecimal Integer:
 What is the decimal value of a hexadecimal
integer?
 A hexadecimal integer a= ik-1 ik-2 …..i0
o The decimal value is
 ik-1* 16k-1 + ik-2 * 16k-2 + …..+ i0 * 160
 Example:
o The decimal value of a hexadecimal integer
a = i3i2i1i0=7121 is
 7 * 163 + 1 * 162 + 2* 161 + 1 * 160
=28961
70
Chapter 2
4. Constants (cont’d)
Sample Question
a
Which of the decimal value of an octal integer 17?
a) 15 b) 17 c) 23 d) 8
Sample Question
What is the output?
a
print(“%d”, 017);
a) 15 b) 17 c) 23 d) 017
71
Chapter 2
5. String Constants
 String constant is a sequence of characters
enclosed in a pair of double quote marks.
 Special characters: \”, \\
 You mustn't split a string constant across lines
 Two string constants that are separated only by
white space are concatenated by the compiler
into a single string.
72
Chapter 2
6. Operator and Punctuator
 Precedence and Associativity
 determine precisely how expressions are
evaluated.
 Precedence of operators indicates when
they will be evaluated.
 Associativity
o
o
“left to right”: Operations are performed from left to
right
 Examples: +,-,/,%
“right to left”: Operations are performed from right to
left
 Examples: ++(prefix), --(prefix)
73
Chapter 2
6. Operator and Punctuator
 Division and Modulus
 Division a/b: an integer expression divided by
another integer expression yields an integer
value.
o Any fractional part is discarded.
 Modulus a%b: the remainder after a is divided
by b.
 If a or b is negative, the results of division and
modulus are system-dependent
 In a/b and a%b, the value of b cannot be
zero.
74
Chapter 2
6. Operator and Punctuator
 Increment Operator ++i, i++
 Each of the expressions ++i and i++ has a
value.
o ++i
 the stored value of i is incremented first
 the expression takes as its value the new stored
value of i
o i++
 the expression takes as its value the current
stored value of i
 the stored value of i is incremented
75
Chapter 2
6. Operator and Punctuator
 Decrement Operator i-- and --i
 Each expression has a value.
o --i
 the stored value of i is decremented by 1
 the expression takes as its value the new stored
valued of i
o i- the expression takes as its value the current
stored valued of i
 the stored value of i is decremented by 1
76
Chapter 2
6. Operator and Punctuator
 Increment and Decrement Operator
 Rules
o Applied to variables but not
 to constants or
 ordinary expressions
77
Chapter 2
6. Operator and Punctuator
 Increment and Decrement Operator
Sample Question
What is the output?
b
int i=0x11;
print(“%d”, i++);
a) 15 b) 17 c) 23 d) 017
78
Chapter 2
6. Operator and Punctuator
 Assignment Operator
 =
 op=: +=, -=, *=, / =, %=, ……
 Semantics:
 variable op= expression
o equivalent to variable = variable op (expression)
 Example:
o var*= expr  var=var * expr
o a *= 3  a = a * 3
 if a was 4 before the assignment, then a = 12
after that.
79
Chapter 2
6. Operator and Punctuator
 Assignment Operator
Sample Question
What is the output?
c
int i=0x11;
print(“%d”, i+= 0x11);
a) 15 b) 17 c) 34 d) 017
80
Chapter 2
7. C System
 The C system consists of
 C language,
 The preprocessor
 The compiler
 The library
 Other tools useful to the programmer, such as
editors and debugger.
81
Chapter 2
7. C System (cont’d)
 C Standard library: a collection of header files and
library files.
 Header file:
o The names and characteristics of functions are included
into computer files called header file.
 Library file:
o The actual implementation of functions are separated
into a library file.
82
Chapter 2
7. C System (cont’d)
 How to use a function in the standard library?
 The programmer needs to provide the function
prototype.
o Including appropriate header files.
 Do we need to locate the function in the library
file? No.
o The system knows where to find the code that
corresponds to functions from the standard library.
83
End of Review of
Chapter 2
84