Chapter 1 Introduction to Computers and C Programming

  • Slides: 44
Download presentation
Chapter 1 – Introduction to Computers and C++ Programming Introduction • Hardware – Physical

Chapter 1 – Introduction to Computers and C++ Programming Introduction • Hardware – Physical components that make up your computer • Software – Instructions to command computer to perform actions and make decisions 2003 Prentice Hall, Inc. All rights reserved.

What is a Computer? • Computer – Device capable of performing computations and making

What is a Computer? • Computer – Device capable of performing computations and making logical decisions – Characteristics ( Speed – Reliability – Storage Capacity) 2003 Prentice Hall, Inc. All rights reserved.

Computer Organization 1. Input unit • • “Receiving” section Obtains information from input devices

Computer Organization 1. Input unit • • “Receiving” section Obtains information from input devices – Keyboard, mouse, microphone, scanner, … 2. Output unit • • • “Shipping” section Takes information processed by computer Places information on output devices – Screen, printer, networks, … – Information can be used to control other devices 2003 Prentice Hall, Inc. All rights reserved.

Computer Organization 3. Memory unit • • • Rapid access, relatively low capacity “warehouse”

Computer Organization 3. Memory unit • • • Rapid access, relatively low capacity “warehouse” section Retains information from input unit – Immediately available for processing Retains processed information – Until placed on output devices Also known as primary memory or primary storage RAM, ROM, PROM, EEPROM 4. CPU = Control Unit + Arithmetic and logic unit Control Unit (CU) – “Administrative” section – Supervises and coordinates other sections of the computer 2003 Prentice Hall, Inc. All rights reserved.

Computer Organization 4. CPU = Control Unit + Arithmetic and logic unit Arithmetic and

Computer Organization 4. CPU = Control Unit + Arithmetic and logic unit Arithmetic and Logic Unit (ALU) – “Manufacturing” section – Performs arithmetic calculations and logic decisions 5. Secondary storage unit • • • Long-term, high-capacity “warehouse” section Storage – Inactive programs or data Secondary storage devices – Disks, Tapes, CDs, DVDs…. Longer to access than primary memory Less expensive per unit than primary memory 2003 Prentice Hall, Inc. All rights reserved.

Classification Of Computers • Supercomputer • Mainframe • Minicomputer Accessed through a TERMINAL •

Classification Of Computers • Supercomputer • Mainframe • Minicomputer Accessed through a TERMINAL • Microcomputer (IBM PC – APPLE MAC – Notebooks – Personal Digital Assistants) • Network Computers (NC) 2003 Prentice Hall, Inc. All rights reserved.

Peripheral Equipments • Definition – devices which are connected to a computer via a

Peripheral Equipments • Definition – devices which are connected to a computer via a local interface such as displays, printers, scanners, and so on. 2003 Prentice Hall, Inc. All rights reserved.

Computer System Configuration • Centralized • Decentralized • Distributed Data Processing • Network 2003

Computer System Configuration • Centralized • Decentralized • Distributed Data Processing • Network 2003 Prentice Hall, Inc. All rights reserved.

Software Types • Firmware (Bootstrap, BIOS) – Low Level Programs in ROM • Operating

Software Types • Firmware (Bootstrap, BIOS) – Low Level Programs in ROM • Operating systems software – The operating system is a set of programs that loads automatically when you start your computer – Functions : Controlling I/O devices, Memory and file storage management, CPU scheduling and controlling processes, Executing and servicing applications software, Handling errors and restarting, User interface – Examples: DOS – UNIX – Win 95/98/2000/XP/NT – OS/2… • Applications software – An application program is the type of program that you use once the operating system has been loaded. • Programming Languages 2003 Prentice Hall, Inc. All rights reserved.

Software Types Hardware User Operating System Applications Software 2003 Prentice Hall, Inc. All rights

Software Types Hardware User Operating System Applications Software 2003 Prentice Hall, Inc. All rights reserved.

Levels of Programming Languages • Described as low level or high level • It

Levels of Programming Languages • Described as low level or high level • It depends on how close they are to the computer language • If close their level is low • If closer to the English-like language their level is high • We have 3 levels: – Machine Language – Assembly Language – High level Language 2003 Prentice Hall, Inc. All rights reserved.

Programming Languages • Machine language • • • The only language that a computer

Programming Languages • Machine language • • • The only language that a computer directly understands Machine-dependent Generally consist of strings of numbers – Ultimately 0 s and 1 s Instruct computers to perform elementary operations – One at a time Cumbersome for humans Example: +1300042774 +1400593419 +1200274027 2003 Prentice Hall, Inc. All rights reserved.

Programming Languages • Assembly language • • English-like abbreviations representing elementary computer operations Clearer

Programming Languages • Assembly language • • English-like abbreviations representing elementary computer operations Clearer to humans Incomprehensible to computers – Translator programs (assemblers) • Convert to machine language Example: LOAD BASEPAY ADD OVERPAY STORE GROSSPAY 2003 Prentice Hall, Inc. All rights reserved.

Programming Languages • High-level languages • Similar to everyday English, use common mathematical notations

Programming Languages • High-level languages • Similar to everyday English, use common mathematical notations • Single statements accomplish substantial tasks – Assembly language requires many instructions to accomplish simple tasks • Need Translator programs (compilers) – Convert whole program to machine language or Interpreter programs – Directly execute high-level language programs • Example: gross. Pay = base. Pay + over. Time. Pay 2003 Prentice Hall, Inc. All rights reserved.

Examples of High-level Languages • FORTRAN – FORmula TRANslator – 1954 -1957: IBM –

Examples of High-level Languages • FORTRAN – FORmula TRANslator – 1954 -1957: IBM – Complex mathematical computations • Scientific and engineering applications • COBOL – COmmon Business Oriented Language – 1959: computer manufacturers, government and industrial computer users – Precise and efficient manipulation of large amounts of data • Commercial applications 2003 Prentice Hall, Inc. All rights reserved.

Examples of High-Level Languages • BASIC – Beginner’s All-Purpose Symbolic Instruction Code – Mid-1960

Examples of High-Level Languages • BASIC – Beginner’s All-Purpose Symbolic Instruction Code – Mid-1960 s for academic use • Visual Basic – Developed late 1980 s, early 1990 s – Powerful features • GUI, event handling, object-oriented programming, error handling …. • Visual Basic. NET – It is the next generation of Visual Basic – Designed to be the easiest and most productive tool for creating . NET applications, including Windows applications, Web Services, and Web applications 2003 Prentice Hall, Inc. All rights reserved.

Examples of High-Level Languages • Pascal – Introduced in 1971 – Named for Blaise

Examples of High-Level Languages • Pascal – Introduced in 1971 – Named for Blaise Pascal the 17 th century French mathematician – For academic use – Less Wordy with few features 2003 Prentice Hall, Inc. All rights reserved.

Examples of High-Level Languages • C – Evolved from two other programming languages •

Examples of High-Level Languages • C – Evolved from two other programming languages • BCPL and B – “Typeless” languages, for writing OS and compilers. – C was created by Dennis Ritchie (Bell Laboratories) • Added data typing, other features compared to B language – Development language of UNIX – Hardware independent • Portable programs 2003 Prentice Hall, Inc. All rights reserved.

Examples of High-Level Languages • C++ – – – Extension of C Early 1980

Examples of High-Level Languages • C++ – – – Extension of C Early 1980 s: Bjarne Stroustrup (Bell Laboratories) “Spruces up” C Provides capabilities for object-oriented programming Hybrid language • C-like style • Object-oriented style • Both – C++ standard library • Rich collections of existing classes and functions 2003 Prentice Hall, Inc. All rights reserved.

Examples of High-Level Languages • Visual C++ – Microsoft’s implementation of C++ • Includes

Examples of High-Level Languages • Visual C++ – Microsoft’s implementation of C++ • Includes extensions • Microsoft Foundation Classes (MFC) • Common library – GUI, graphics, networking, multithreading, … – Shared among Visual Basic, Visual C++, C# • Visual C ++ under the. NET platform – Web-based applications • Distributed to great variety of devices – Cell phones, desktop computers – Applications in disparate languages can communicate 2003 Prentice Hall, Inc. All rights reserved.

Examples of High-Level Languages • C# (. NET) – Anders Hejlsberg and Scott Wiltamuth

Examples of High-Level Languages • C# (. NET) – Anders Hejlsberg and Scott Wiltamuth (Microsoft) – Designed specifically for. NET platform – Roots in C, C++ and Java • Easy migration to. NET – Event-driven, fully object-oriented, visual programming language – Integrated Development Environment (IDE) • Create, run, test and debug C# programs – Language interoperability 2003 Prentice Hall, Inc. All rights reserved.

Examples of High-Level Languages • Java – – – – Developped by: Sun Microsystems

Examples of High-Level Languages • Java – – – – Developped by: Sun Microsystems Derived from C++ Runs on many platforms Used for web pages with dynamic and interactive content Also used to develop large-scale enterprise applications Enhance functionality of web servers Provide applications for consumer devices • Cell phones, pagers, personal digital assistants, … 2003 Prentice Hall, Inc. All rights reserved.

Structured Programming • Structured programming (1960 s) – – Disciplined approach to writing programs

Structured Programming • Structured programming (1960 s) – – Disciplined approach to writing programs Clear, easy to test and debug, and easy to modify Also known as Procedural Programming Major task is divided into smaller tasks 2003 Prentice Hall, Inc. All rights reserved.

Object Oriented Programming • Objects – Reusable software components that model real world items

Object Oriented Programming • Objects – Reusable software components that model real world items (Student object, Class object, Course object). These objects have attributes and behaviors. • In OOP, the code is more understandable, better organized and easier to maintain than procedural programming • OOP favors modularity (Software reuse) 2003 Prentice Hall, Inc. All rights reserved.

Basics of a Typical C++ Environment • C++ systems consists of : – Program-development

Basics of a Typical C++ Environment • C++ systems consists of : – Program-development environment – Language – C++ Standard Library 2003 Prentice Hall, Inc. All rights reserved.

Basics of a Typical C++ Environment Phases of C++ Programs: 1. Edit 2. Preprocess

Basics of a Typical C++ Environment Phases of C++ Programs: 1. Edit 2. Preprocess 3. Compile Editor Preprocessor Compiler Linker 4. Link Loader 5. Load Disk 6. Execute Disk Program is created in the editor and stored on disk. Disk Preprocessor program processes the code. Disk Compiler creates object code and stores it on disk. Disk Primary Memory Loader puts program in memory. . . . Primary Memory CPU . . . 2003 Prentice Hall, Inc. All rights reserved. Linker links the object code with the libraries, creates a. out and stores it on disk CPU takes each instruction and executes it, possibly storing new data values as the program executes.

// A first program in C++. Outline Single-line comments. #include <iostream> using namespace std;

// A first program in C++. Outline Single-line comments. #include <iostream> using namespace std; Preprocessor directive to Function main returns an include input/output stream Left brace { begins function integer value. header file <iostream>. Statements end with a // function main begins execution Function main appears body. program int main() exactly once in every C++ semicolon ; . { program. . cout << "Welcome Corresponding right brace } to C++!n"; return 0; // indicate that program ended successfully ends function body. Stream insertion operator. } // end function main Name cout belongs to Keyword return is one of namespace std. several means to exit function; Welcome to C++! value 0 indicates program terminated successfully. 2003 Prentice Hall, Inc. All rights reserved.

 2003 Prentice Hall, Inc. All rights reserved.

2003 Prentice Hall, Inc. All rights reserved.

// Printing a line with multiple statements. #include <iostream> using namespace std; // function

// Printing a line with multiple statements. #include <iostream> using namespace std; // function main begins program execution int main() { cout << "Welcome "; cout << "to C++!n"; return 0; Outline Multiple stream insertion statements produce one line of output. // indicate that program ended successfully } // end function main Welcome to C++! 2003 Prentice Hall, Inc. All rights reserved.

// Printing multiple lines with a single statement #include <iostream> using namespace std; Outline

// Printing multiple lines with a single statement #include <iostream> using namespace std; Outline Using newline characters to execution print on multiple lines. // function main begins program int main() { cout << "Welcomentonn. C++!n"; return 0; // indicate that program ended successfully } // end function main Welcome to C++! 2003 Prentice Hall, Inc. All rights reserved.

Storage Locations and Addresses • Each memory locations is identified by a number called

Storage Locations and Addresses • Each memory locations is identified by a number called Memory Address • The address doesn’t change but its contents may change • A memory address holds a fixed amount of data 2003 Prentice Hall, Inc. All rights reserved.

Variables • Variables – Location in memory where value can be stored – For

Variables • Variables – Location in memory where value can be stored – For a programmer a variable is referred to by a name called a Symbolic Address – A variable is declared by a name and a data type before being used data_type varaiable list; – Common data types • int - integer numbers • char - characters • double - floating point numbers – Declaring variables integer 1; char title; – Can declare several variables of same type in one declaration • Comma-separated list integer 1, integer 2, sum; – You cannot use C++ keywords as variable names. 2003 Prentice Hall, Inc. All rights reserved.

Variables • Variables – Variable names • Valid identifier – Series of characters (letters,

Variables • Variables – Variable names • Valid identifier – Series of characters (letters, digits, underscores) – Cannot begin with digit – Case sensitive 2003 Prentice Hall, Inc. All rights reserved.

// Addition program. #include <iostream> using namespace std; Outline // function main begins program

// Addition program. #include <iostream> using namespace std; Outline // function main begins program execution int main() Declare integer variables. { integer 1; // first number to be input by user integer 2; // second number to be input by user int sum; // variable in Use stream extraction operator which sum will be stored cout << "Enter first cin >> integer 1; with standard input stream to obtain user input. integern"; // prompt // read an integer Calculations can also be performed in output statements cout << "Enter second integern" ; // prompt cin >> integer 2; // read an integer cout << "Sum is " Stream manipulator endl << integer 1 + integer 2 << endl; sum = integer 1 + integer 2; outputs a newline, then “flushes output buffer. ” // assign result to sum cout << "Sum is " << sum << endl; // print sum return 0; // indicate that program ended successfully } // end function main Concatenating, chaining or cascading stream insertion operations. 2003 Prentice Hall, Inc. All rights reserved.

Enter first integer 45 Enter second integer 72 Sum is 117 Outline 2003 Prentice

Enter first integer 45 Enter second integer 72 Sum is 117 Outline 2003 Prentice Hall, Inc. All rights reserved.

Memory Concepts • Variable names – Correspond to actual locations in computer's memory –

Memory Concepts • Variable names – Correspond to actual locations in computer's memory – Every variable has name, type, size and value – When new value placed into variable, overwrites previous value – Reading variables from memory nondestructive 2003 Prentice Hall, Inc. All rights reserved.

Memory Concepts cin >> integer 1; integer 1 45 integer 2 72 – Assume

Memory Concepts cin >> integer 1; integer 1 45 integer 2 72 – Assume user entered 45 cin >> integer 2; – Assume user entered 72 sum = integer 1 + integer 2; sum 2003 Prentice Hall, Inc. All rights reserved. 117

Arithmetic • Arithmetic calculations in C++ use arithmetic operators (binary operators) like – *

Arithmetic • Arithmetic calculations in C++ use arithmetic operators (binary operators) like – * Multiplication – / Division (truncates the remainder if the 2 operands are integer for example 7 / 5 evaluates to 1) – % Modulus operator (returns the remainder of an integer division for example 7 % 5 evaluates to 2. If the operands are not integer we get a syntax error) Addition/Subtraction Sets the order of evaluation – + / - – ( ) 2003 Prentice Hall, Inc. All rights reserved.

Arithmetic • Rules of operator precedence – Operators in parentheses evaluates first • Nested/embedded

Arithmetic • Rules of operator precedence – Operators in parentheses evaluates first • Nested/embedded parentheses – Operators in innermost pair first – Multiplication, division, modulus comes next • Operators apply from left to right – Addition, subtraction applies last • Operators apply from left to right 2003 Prentice Hall, Inc. All rights reserved.

Decision Making: Equality and Relational Operators • if structure – Make decision based on

Decision Making: Equality and Relational Operators • if structure – Make decision based on truth or falsity of condition • If condition met, body executed • Else, body not executed • Equality and relational operators – Equality operators • Same level of precedence – Relational operators • Same level of precedence – Associate left to right 2003 Prentice Hall, Inc. All rights reserved.

1. 25 Decision Making: Equality and Relational Operators 2003 Prentice Hall, Inc. All rights

1. 25 Decision Making: Equality and Relational Operators 2003 Prentice Hall, Inc. All rights reserved.

// Using if statements, relational // operators, and equality operators. #include <iostream> using namespace

// Using if statements, relational // operators, and equality operators. #include <iostream> using namespace std; Outline Declare variables. // function main begins program execution int main() { int num 1; // first number to be read from user int num 2; // second number to be read from. If condition is true (i. e. , values user are equal), execute this cout << "Enter two integers, and I will tell youn" statement. if structure compares values << "the relationships they satisfy: " ; of num 1 and num 2 to test for cin >> num 1 >> num 2; // read two integers if ( num 1 == num 2 ) cout << num 1 << " is equality. if structure compares values equal of num 1 and num 2 to test for to " << num 2 If condition is true (i. e. , values << endl; are not equal), execute this inequality. statement. if ( num 1 != num 2 ) cout << num 1 << " is not equal to " << num 2 << endl; 2003 Prentice Hall, Inc. All rights reserved.

if ( num 1 < num 2 ) cout << num 1 << "

if ( num 1 < num 2 ) cout << num 1 << " is less than " << num 2 << endl; if ( num 1 > num 2 ) cout << num 1 << " is greater than " << num 2 << endl; if ( num 1 <= num 2 ) cout << num 1 << " is less than or equal to " << num 2 << endl; Outline Statements may be split over several lines. if ( num 1 >= num 2 ) cout << num 1 << " is greater than or equal to " << num 2 << endl; return 0; // indicate that program ended successfully } // end function main Enter two integers, and I will tell you the relationships they satisfy: 22 12 22 is not equal to 12 22 is greater than or equal to 12 2003 Prentice Hall, Inc. All rights reserved.

Precedence and Associativity of Operations 2003 Prentice Hall, Inc. All rights reserved.

Precedence and Associativity of Operations 2003 Prentice Hall, Inc. All rights reserved.