Upload
cyrus-livingston
View
23
Download
0
Embed Size (px)
DESCRIPTION
Welcome to CPSC 206. Structured Programming in C. Lecture Information. http://people.cs.tamu.edu/ychen/Teaching/CPSC206. 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: - PowerPoint PPT Presentation
Citation preview
1
Structured Programming in CStructured Programming in C
Welcome to CPSC 206Welcome to CPSC 206
2
Lecture Information
http://people.cs.tamu.edu/ychen/Teaching/CPSC206
3
Midterm 1
Time: Sept 30, Thur., 8:00-9:15AM
Location: HRBB 124Closed book
No textbook, No notes, No calculator
You are required to bring: Answer sheet: SCANTRON (NCS MP90051
or 0-101607-TAMU) (You can find it in MSC Bookstore)
No. 2 pencils, eraser Student ID
4
Review 1:
Topic 1: Introduction to Computer ScienceTopic 2: Overview of C Language
Chapter 1Section 1.1 – 1.8
Chapter 2Section 2.1 – 2.12
5
Topic 1: Introduction to Computer Science
Part I: an overview of computer science.Part II: computer hardware and software.Part III: computer languages.
6
Introduction to Computer SciencePart 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
7
Introduction to Computer SciencePart 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.
8
Introduction to Computer SciencePart I: an overview of computer science.
Sample Question
Software engineering focuses on how to develop applications for networks.
True of False False
9
Introduction to Computer SciencePart 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
10
Computer Organization
Data Representation How data is stored and processed?
All data is stored and processed in binarybinary form, that is, as a series of 00s and 11s.
The definition of Bit, Byte, Word
Sample Question
The number of bits in one Bytes is
a) 4 b) 8 c) 16 d) 32 e) none of the above
b
11
Computer Organization
Data RepresentationBinary number
Convert a decimal number to a binary number
What is the decimal value of a binary numberSample Question
The decimal value of a binary number 100001 is
a) 33 b) 1 c) 100001 d) 17 e) none of the above
a
12
Computer Organization
Data Representation (contd.) How a signed number is represented in binary
form?In a binary representation, the leftmost bit is
a sign bitsign bit followed by the magnitude bitsmagnitude 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.
13
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 c) 7 d) 15 e) none of the above c
14
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.
15
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 speedMIPS
16
Computer Organization
Central Processing Unit (CPU)
Arithmetic/Logic Unit (ALU)
Control unit
CPU
Control and coordinate the computer’s operations.
performs all arithmetic computations and logic operations.
CPU reads program instructions from main memory. CPU executes one instruction at a time until completion.
17
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.
18
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.
19
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.
20
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.
21
Computer Organization
Secondary Storage: Mass storage devices
Sequential storage: tape. Random storage: floppy disk, zip disk,
CD-ROM, etc. Static storage.
it doesn't require power to retain stored data.
Slower speed.
22
Computer Organization
Input Keyboard, mouse etc
Output: Monitor, printer etc
23
Computer Organization
How CPU, Memory, Storage, Input and Output work together? IPOS Cycle
24
Computer OrganizationI - Input
InputDevice
P - Process
CPU
O - Output S - Storage
Memory Output Device
Secondary Storage
Data/Progra
m
II
II
Program
output
OO
OO
PP
SSProgram/ Program output
Binary Representation
25
Introduction to Computer SciencePart 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
26
Solving problems on computers.
System Design Top-down
break the problem into smaller and smaller sub-problems until they can be solved trivially.
Bottom-upstart by designing the low-level details,
and then decide how these will be put together to create the entire system
27
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 CodeDecision TreesFlow Charts
28
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.
29
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.
30
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.
31
Introduction to Computer SciencePart III: Computer Language
Why Computer needs language? Computer requires an unambiguousunambiguous
language Computer Language are designed to be
unambiguousunambiguous.Precedence and associativity determine
precisely how expressions are evaluated.
32
Introduction to Computer SciencePart 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.
33
Introduction to Computer SciencePart 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.
34
End of Review of Topic 1Introduction of Computer Science
35
Chapter 1
Review Outline1. A brief history of C2. Features of C3. Get Ready to Program4. General form of a simple program
main function variable declarations
36
Chapter 1
Review Outline (contd)5. initialization6. preprocessing directive
include define
7. printf and scanf printf \n scanf
8. while statement
37
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.
38
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.
39
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 execute
pico / vi gcc / cc
40
Chapter 1
4. General Form of a simple program
The general form of a programThe general form of a programpreprocessing directivesint main (){
declarations statements
}
41
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.
42
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) errord
43
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.
44
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.
45
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);
}
What is the ouput?
a) 6 b) 4 c) sumc d) error
d
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);
}
What is the ouput?
a) 6 b) 4 c) sumc d) error
a
47
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) 5 b) 3 c) 4 d) 2
a
48
Chapter 17. printf and scanf
Arguments: control_string and other_arguments control_string contains formats, called
conversion specifications, which are matched with other arguments.
49
Chapter 17. 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);
50
Chapter 17. 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 c: charactero d: decimal integer o f: floating-point numbero s, e, g
table of conversion character, page 16
51
Chapter 1
7. printf and scanf (cont’d) printf
Sample Question
What is the output?
pirntf(“%8.2f”, 1.234);
a)_ _ _ 1.23 b) 1.234 c) _ _ _1.234 d) _ _ _ _ 1.23
d
52
Chapter 1
7. printf and scanf (cont’d) \n: newline
Sample Question#include <stdio.n>
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
c
53
Chapter 17. 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 d: decimal integer f: floating-point number lf: floating-point number (double) Lf: floating-point number (long double) s: string
other_arguments are addresses
table of conversion, page 18
54
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 valueo the value of the decimal integer is stored at
the address of x.
55
Chapter 17. printf and scanf (cont’d)
scanf
Sample Question
Which format should be used to read a long double value?
a) %Lf b)%lf c) %f d) %L
a
56
Chapter 1
8. whilewhile (expression)
statementstatement is executed as long as the value
of expression is true true: any non-zero value
57
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;
while (++e <= 10) printf("%5d", power_of_two *= 2); printf("\n"); return 0;}
How many times the body of the loop is executed?
58
End of Review of
Chapter 1
59
Chapter 2
Review Outline1. Comments2. Keywords3. Identifiers4. Constants
floating numbers integer constants
60
Chapter 2
Review Outline (cont’d)5. String Constants6. Operators and Punctuators
Precedence and Associativity of Operators
Division Modulus Increment and Decrement Operators
Assignment Operators7. C System
61
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.
62
Chapter 2
1. Comments (cont’d)
Sample Question
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 */ */
a
63
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
64
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. composed of letters, digits, and underscore _ .2. The first character must be a letter or
underscore.3. case-sensitive4. would not be defined as the special identifiers:
65
Chapter 2
3. Identifier (cont’d)
Sample Question
Which of the following is incorrect?
a) int comment;
b) int _int;
c) int int_;
d) int 4int;
d
66
Chapter 2
4. Constants1. Floating constants
float: an F suffix o 1.22F
double: unsuffixed floating constanto 1.22
long double: an L suffixo 1.22L
2. Character constants1. ‘A’
67
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
68
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
69
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
70
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
71
Chapter 2
4. Constants (cont’d)
Sample Question
Which of the decimal value of an octal integer 17?
a) 15 b) 17 c) 23 d) 8
a
Sample Question
What is the output?
print(“%d”, 017);
a) 15 b) 17 c) 23 d) 017
a
72
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.
73
Chapter 2
6. Operator and Punctuator Precedence and Associativity
determine precisely how expressions are evaluated.
Precedence of operators indicates when they will be evaluatedwhen they will be evaluated.
Associativity o “left to right”: Operations are performed from
left to right Examples: +,-,/,%
o “right to left”: Operations are performed from right to left Examples: ++(prefix), --(prefix)
74
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.
75
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 io i++
the expression takes as its value the current stored value of i
the stored value of i is incremented
76
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 io i--
the expression takes as its value the current stored valued of i
the stored value of i is decremented by 1
77
Chapter 2
6. Operator and Punctuator Increment and Decrement Operator
Ruleso Applied to variables but not
to constants or ordinary expressions
78
Chapter 2
6. Operator and Punctuator Increment and Decrement Operator
Sample Question
What is the output?
int i=0x11;
print(“%d”, i++);
a) 15 b) 17 c) 23 d) 017
b
79
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 * expro a *= 3 a = a * 3
if a was 4 before the assignment, then a = 12 after that.
80
Chapter 2
6. Operator and Punctuator Assignment Operator
Sample Question
What is the output?
int i=0x11;
print(“%d”, i+= 0x11);
a) 15 b) 17 c) 34 d) 017
c
81
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.
82
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.
83
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.
84
End of Review of
Chapter 2