840 likes | 974 Views
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:
E N D
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: • Answer sheet: SCANTRON (NCS MP90051 or 0-101607-TAMU) (You can find it in MSC Bookstore) • No. 2 pencils, eraser • Student ID
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
Topic 1: Introduction to Computer Science • Part I: an overview of computer science. • Part II: computer hardware and software. • Part III: computer languages.
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
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.
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
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
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 c) 16 d) 32 e) none of the above b
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 d) 17 e) none of the above a
Computer Organization • Data Representation (contd.) • How a signed number is represented in binary form? • In a binary representation, the leftmost bit is a sign bitfollowed 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.
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
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.
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
Control and coordinate the computer’s operations. performs all arithmetic computations and logic operations. 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 Arithmetic/Logic Unit (ALU)
contains the instruction currently being processed. contains the memory address of the instruction that is currently being executed. Computer Organization • Central Processing Unit (CPU) (contd.) Program Counter (PC) Instruction Counter (IC)
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: • Fetch, Decode, Execute • Machine Cycle is repeated until completion.
Computer Organization • Central Processing Unit (CPU) (contd.) • In each Machine Cycle: • Fetch: • The control unit loads the instruction pointed to by PC to IC. • 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.
Computer Organization • Central Processing Unit (CPU) (contd.) • Hardware characteristics: • The speed of a computer can be quoted as: • Clock speed, e.g. 100MHz means 100 million cycles per second. • MIPS, a million instructions per second.
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.
Computer Organization • Input • Keyboard, mouse etc • Output: • Monitor, printer etc
Computer Organization • How CPU, Memory, Storage, Input and Output work together? • IPOS Cycle
Program output Output Device Input Device O I Data/ Program O S I Program/ Program output Computer Organization P - Process O - Output S - Storage I - Input Binary Representation P CPU Memory Secondary Storage
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
Solving problems on computers. • System Design • Top-down break the problem into smaller and smaller sub-problems 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
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
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.
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.
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.
Introduction to Computer SciencePart 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.
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.
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.
End of Review of Topic 1 Introduction of Computer Science
Chapter 1 • Review Outline • A brief history of C • Features of C • Get Ready to Program • General form of a simple program • main function • variable declarations
Chapter 1 • Review Outline (contd) • initialization • preprocessing directive • include • define • printf and scanf • printf • \n • scanf • while statement
Chapter 1 • A brief history of C • 1960s, CPL (Combined Programming Language) • capable of both high level machine independentprogramming 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.
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.
compile execute 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 pico / vi gcc / cc
Chapter 1 • 4. General Form of a simple program The general form of a program preprocessing directives int main () { declarations statements }
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.
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
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.
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.
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
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
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
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.
Chapter 1 • 7. printf and scanf (cont’d) • printf: How to specify format using conversion specification? • %field_widthconversion_character • %field_width.precisionconversion_character • Example: • printf(“%7.2f”, 1.221); • printf(“%7d”, 1221);
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? • c: character • d: decimal integer • f: floating-point number • s, e, g table of conversion character, page 16