cosc 120 computer programming

of 29 /29
1 COSC 120 Computer Programming Instructor: Dr. Enyue (Annie) Lu Office hours: click here Office room: HS114 Email: [email protected] Course information: Website: 0.html .

Upload: kinsey

Post on 24-Jan-2016




0 download

Embed Size (px)


COSC 120 Computer Programming. Instructor: Dr. Enyue (Annie) Lu Office hours: click here Office room: HS114 Email: [email protected] Course information: Website: Course overview. Introduction to Programming Introduction to C++ - PowerPoint PPT Presentation


Page 1: COSC 120 Computer Programming


COSC 120 Computer Programming

Instructor: Dr. Enyue (Annie) Lu Office hours: click here Office room: HS114 Email: [email protected]

Course information: Website:

Page 2: COSC 120 Computer Programming


Course overview Introduction to Programming Introduction to C++ Data types Control structures Functions Scope, overloading functions, files Arrays Searching & Sorting Arrays Characters, Strings, and the string Class Structured Data Introduction to class Pointers Operator overloading Optional topics

Page 3: COSC 120 Computer Programming


Chapter 1

Introduction to Computers and Programming

Page 4: COSC 120 Computer Programming



Why Program? Computer Systems: Hardware and Software Programs and Programming Languages What Is a Program Made of? Input, Processing, and Output The Programming Process Procedural and Object-Oriented


Page 5: COSC 120 Computer Programming


Why Program:

Program: a set of instructions to a computer to perform a task

Computer can do many different jobs because of its programmability.Programmer->software->computer

Page 6: COSC 120 Computer Programming


Hardware CPU (central processing unit): CU, ALUFetch/decode/execute

Main Memory: RAM Byte, bits Memory address volatile

Secondary storage Programs are stored in secondary memory and loaded

into main memory Hard disks, floppy disks, Zip disks and CD

Input/Output devices


Elements of a Computer System

Page 7: COSC 120 Computer Programming


Software – Programs That Run on a Computer

Categories of software:

Operating system:

programs that manage the computer hardware and the programs that run on them. Ex: Windows, UNIX, Linux

- Single tasking, multitasking- Single user, multiuser

Application software:

programs that provide services to the user. Ex: word processing, games, programs to solve specific problems

Page 8: COSC 120 Computer Programming


Programs and Programming Languages

Program: a set of instructions to a computer to perform a task

Programming Language: a language used to write programs

Page 9: COSC 120 Computer Programming


Programs and Programming Languages Types of languages:

Low-level (machine): used for communication with computer hardware directly. Often written in binary machine code (0’s/1’s) directly.

High-level: closer to human language A compiler or interpreter are programs that

make the translations from high-level to low-level languages.

Page 10: COSC 120 Computer Programming


Table 1-1

Language Description

C A structured, general purpose language developed at Bell Labs. C offers both high-level and low-level features.

C++ Based on the C language, C++ offers object-oriented features not found in C. Also invented at Bell Laboratories.

Java An object-oriented language invented at Sun Microsystems. Java may be used to develop programs that run over the Internet, in a web browser.

BASIC Beginners All-purpose Symbolic Instruction Code. A general programming language originally designed to be simple enough for beginners to learn.

COBOL Common Business-Oriented Language. A language designed for business applications.

RPG A programming language for business applications used primarily on IBM midrange and mainframe computers.

FORTRAN Formula Translator. A language designed for programming complex mathematical algorithms.

Pascal A structured, general purpose language designed primarily for teaching programming.

Page 11: COSC 120 Computer Programming


From a High-level Program to an Executable File

Source Code


ModifiedSource Code


Object Code


Executable Code

convert source file directives to source code program statements

convert source program statements into machine language instructions

connect hardware-specific code (library routines) to machine instructions, producing an executable file

Page 12: COSC 120 Computer Programming


What Is a Program Made Of?

Common elements in programming languages: Key Words Programmer-Defined Symbols Operators Punctuation Syntax

Page 13: COSC 120 Computer Programming


Language Elements, Table 1-2

Language Element


Keywords Words that have a special meaning. Keywords may only be used for their intended purpose. AKA reserved words.

Programmer-Defined Symbols

Words or names defined by the programmer. They are symbolic names (identifiers) that refer to variables or programming routines.

Operators Operators perform operations on one or more operands. An operand is a piece of data, like a number.

Punctuation Punctuation characters that mark the beginning or ending of a statement, or separate items in a list.


Rules that must be followed when construction a program. Syntax dictates how key words and operators may be used, and where punctuation symbols must appear.

Page 14: COSC 120 Computer Programming


Example Program

#include <iostream>#include <string>using namespace std;

int main() {

string name;

cout << "What is your name? "; cin >> name;cout << "Hello there, " << name;

return 0;}

Page 15: COSC 120 Computer Programming


Program 1-1

// This program calculates gross pay.

#include <iostream>using namespace std;

int main(){

float hours, rate, pay;

cout << “How many hours did you work? ”;cin >> hours;

cout << “How much do you get paid per hour? ”;cin >> rate;

pay = hours * rate;cout << “You have earned $” << pay << endl;

return 0;}

Page 16: COSC 120 Computer Programming


Key Words

Also known as reserved words Have a special meaning in C++ Can not be used for another purpose Examples in program:

using, namespace, int, main

Page 17: COSC 120 Computer Programming


Programmer-Defined Symbols( Identifiers )

Names made up by the programmer Not part of the C++ language Used to represent various things:

variables (memory locations), functions, etc. Example in program:

name, hours, rate, calcPay()

Page 18: COSC 120 Computer Programming



Used to perform operations on data Many types of operators:

Arithmetic: +, -, *, / Assignment: =

Examples in program: << stream insertion operator >> stream extraction operator

Page 19: COSC 120 Computer Programming



Characters that mark the end of a statement, or that separate items in a list

Examples in program: ; (),}

Page 20: COSC 120 Computer Programming



The rules of grammar that must be followed when writing a program

Controls the use of:

key words


programmer-defined symbols


Page 21: COSC 120 Computer Programming


Lines and Statements

cout << "How many hours did you work? ";

cout << "How many hours "

<< "did you work? ";

There are 2 statements above. The first statement uses one line the second statement spans two lines the semicolon ( ; ) is the statement terminator

Page 22: COSC 120 Computer Programming


Variable Definitions

Two types of information: numbers and characters

Numbers may be integers or floating-point

The statement below creates three variables in memory named hours, rate, and pay that each can store a floating-point number

float hours, rate, pay;

Page 23: COSC 120 Computer Programming


Input, Processing, and Output

Three steps many programs perform:1) Gather input data:

- from keyboard- from files on disk drives

2) Process the input data

3) Generate output:- send it to the screen- write it to a file

Page 24: COSC 120 Computer Programming


Input, Processing, and Output

Outputcout << "Enter your hours and rate " << "separated by a space: ";

Input:cin >> hours; cin >> rate;

Processing:pay = hours * rate;

Outputcout << “You have earned $” << pay;

Page 25: COSC 120 Computer Programming


Program Output

How many hours did you work? 10How much do you get paid per hour? 15You have earned $150

Page 26: COSC 120 Computer Programming


The Programming Process

The programming process consists of several steps, which include: Design Code Test Debug Document

Page 27: COSC 120 Computer Programming


The Programming Process

1. Clearly define what the program is to do.Purpose, Inputs /Outputs, Process

2. Visualize the program running on the computer3. Design using a Hierarchy chart, flowchart or pseudocode.4. Desk check for logical errors.5. Enter/Edit the code and compile it.6. Correct any errors found during compilation.

• Compile-time error (syntax/grammatical error)• Repeat steps 5 and 6 as many times as necessary

7. Run the program with test data for input8. Correct any errors found while running the program

• Run-time errors• Repeat steps 5 through 8 as many times as necessary.

9. Validate the results of the program.

Page 28: COSC 120 Computer Programming


Procedural and Object-Oriented Programming

Procedural programming: focus is on the process. Procedures/functions are written to process data.

Object-Oriented programming: focus is on objects, which contain data and the means to manipulate the data. Messages are sent to objects to perform operations.

Page 29: COSC 120 Computer Programming


Class Review Program

A set of instructions to a computer to perform a task Why computer can do lots of job

Programmability Elements of computer system

Hardware (CPU, main memory, second storage, I/O), software Software categories

OS, application software Programming languages

Low-level, high-level Transfer a high-level program to an executable file

Preprocess, compile, link Common elements in programming languages

Keyword, programmer-defined symbols, operators, punctuation, syntax Which 3 steps most programs perform

Gather input data, process data, generate output