chapter 1 introduction to computers and c++ programming

12
Chapter 1 Chapter 1 Introduction to Computers and C++ Introduction to Computers and C++ Programming Programming Goals: Goals: To introduce the fundamental hardware and software To introduce the fundamental hardware and software components of a computer system components of a computer system To describe the role of compilers in high-level programming To describe the role of compilers in high-level programming To examine the use of algorithms in program design To examine the use of algorithms in program design To define the software life cycle To define the software life cycle To introduce the C++ programming language To introduce the C++ programming language

Upload: kane-cobb

Post on 01-Jan-2016

57 views

Category:

Documents


0 download

DESCRIPTION

Chapter 1 Introduction to Computers and C++ Programming. Goals:. To introduce the fundamental hardware and software components of a computer system. To describe the role of compilers in high-level programming. To examine the use of algorithms in program design. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 1 Introduction to Computers and C++ Programming

Chapter 1Chapter 1Introduction to Computers Introduction to Computers and C++ Programmingand C++ Programming

Goals:Goals:

• To introduce the fundamental hardware and To introduce the fundamental hardware and software components of a computer systemsoftware components of a computer system

• To describe the role of compilers in high-level To describe the role of compilers in high-level programmingprogramming• To examine the use of algorithms in program designTo examine the use of algorithms in program design

• To define the software life cycleTo define the software life cycle

• To introduce the C++ programming languageTo introduce the C++ programming language

Page 2: Chapter 1 Introduction to Computers and C++ Programming

Chapter 1Chapter 1CS 140CS 140 Page Page 22

Computer HardwareComputer Hardware

Page 3: Chapter 1 Introduction to Computers and C++ Programming

Chapter 1Chapter 1CS 140CS 140 Page Page 33

Key ‘P’ struckKey ‘P’ struck

Computer SoftwareComputer Software

HardwareDirect access to circuitry, disks, mouse, keyboard, monitor, etc.

Operating SystemSoftware that relays messages

between application and hardware

ApplicationSoftware that performs high level

operations (computation, graphics, etc.)

““Hey, OS!Hey, OS!

key ‘P’!”key ‘P’!”

““Hey, App!Hey, App!

key ‘P’!”key ‘P’!”

Send explosion Send explosion sound to sound to

speaker and speaker and new pixel new pixel values to values to monitormonitor

Contact Contact sound & sound & graphics graphics

cardscards

Specify Specify resulting resulting audio & audio & videovideo

Page 4: Chapter 1 Introduction to Computers and C++ Programming

Chapter 1Chapter 1CS 140CS 140 Page Page 44

Low-Level Programming Low-Level Programming LanguagesLanguages

A computer processor is A computer processor is notnot smart! smart!

Its vocabulary is limited to a simple “machine Its vocabulary is limited to a simple “machine language” consisting of a small number of simple language” consisting of a small number of simple commands.commands.

Move this number over

there!

Move that number over

here!

Add this number to

that number!

Check to see if this

number is zero!

Page 5: Chapter 1 Introduction to Computers and C++ Programming

Chapter 1Chapter 1CS 140CS 140 Page Page 55

ProgrammingProgramming

To get the computer to perform sophisticated To get the computer to perform sophisticated operations, the programmer writes programs that operations, the programmer writes programs that tell the processor the sequence of primitive steps tell the processor the sequence of primitive steps to take to get a result.to take to get a result.

001001010110101010101010101010100001111110011100101111000001101010010010111010100010100100001101011001010100110101100101010010101001010010101011010010101000100101010101010101010101001010101001

13 01946.372

Programming in Programming in machine language is a machine language is a binary pain, so higher binary pain, so higher level languages have level languages have been developed to been developed to make the job of the make the job of the programmer more programmer more efficient and more efficient and more effective!effective!

Page 6: Chapter 1 Introduction to Computers and C++ Programming

Chapter 1Chapter 1CS 140CS 140 Page Page 66

Compiling a High-Level Compiling a High-Level ProgramProgramA program caller a “compiler” is used to translate A program caller a “compiler” is used to translate your “source program” (in a language like C++) your “source program” (in a language like C++) into an “object program” (in your system’s into an “object program” (in your system’s machine language).machine language).#include <iostream>

using namespace std;void main(){ int x, y; cout << “Enter two integers: ”; cin >> x >> y; if (x > y) cout << x << “ is the largest!”; else cout << y << “ is the largest!”;}

LEXICALANALYSIS

Split the source

program into words like “void”, “x”, “>”, and “;”.

PARSINGAnalyze the grammatical syntax of the

source program (e.g., “if (x > y)”makes sense, but “if (x > ) y”

doesn’t).

CODECODEGENERATIONGENERATIONGenerate an Generate an equivalent equivalent program in program in

machine machine language.language.

110101000101100011000010010110110100010101011110010101011100000010011100101011001110101010111001010100101010101000000110110111011101010100111110101010101001001001010000010101010101000000101111100101100001011101010101010100010101111110010100100100101000

Source ProgramSource Program Object ProgramObject Program

Page 7: Chapter 1 Introduction to Computers and C++ Programming

Chapter 1Chapter 1CS 140CS 140 Page Page 77

Linking and LoadingLinking and LoadingAfter being compiled, the object program must be After being compiled, the object program must be “linked” (i.e., connected to other compiled code “linked” (i.e., connected to other compiled code from libraries, like math functions or input/output from libraries, like math functions or input/output operators)operators)and then “loaded” into main memory for and then “loaded” into main memory for execution.execution.SourceSource

ProgramProgramObjectObject

ProgramProgramLinkedLinked

ProgramProgramCompiledCompiledLibraryLibrary

ProgramsPrograms

COMPILECOMPILE

LINKLINKLOADLOAD

Page 8: Chapter 1 Introduction to Computers and C++ Programming

Chapter 1Chapter 1CS 140CS 140 Page Page 88

AlgorithmsAlgorithmsAfter defining a problem that the programmer After defining a problem that the programmer wants the computer to solve, the programmer wants the computer to solve, the programmer must first design an must first design an algorithmalgorithm, a sequence of , a sequence of precise instructions that lead to a solution.precise instructions that lead to a solution.

Problem: Find the largest value in a list of numbers.Problem: Find the largest value in a list of numbers.

Algorithm:Algorithm:

1) Retrieve the list of numbers.1) Retrieve the list of numbers.

2) Consider the first number the largest value so far.2) Consider the first number the largest value so far.

3) Starting at the second number in the list, compare3) Starting at the second number in the list, comparethe number in the list to the largest value so far;the number in the list to the largest value so far;if it’s larger, then make if it’s larger, then make itit the largest value so far. the largest value so far.

4) After examining all of the numbers, announce the4) After examining all of the numbers, announce thelargest value so far - it’s the largest in the list!.largest value so far - it’s the largest in the list!.

4747

2828

5656

61613030

1919

4747

5656

6161

4747

5656??????????

6161 LARGEST!LARGEST!

Page 9: Chapter 1 Introduction to Computers and C++ Programming

Chapter 1Chapter 1CS 140CS 140 Page Page 99

Another AlgorithmAnother AlgorithmProblem: Find the phone number of a specific person in an alphabetized phonebook.Problem: Find the phone number of a specific person in an alphabetized phonebook.

Algorithm:Algorithm:

1) Get the phonebook.1) Get the phonebook.

2) Crack what’s left of the phonebook open to the middle page.2) Crack what’s left of the phonebook open to the middle page.

3) Check to see if the name you’re seeking is on that page.3) Check to see if the name you’re seeking is on that page.If so, announce the phone number and you’re done!.If so, announce the phone number and you’re done!.Otherwise, throw away the “impossible” half of the phonebook,Otherwise, throw away the “impossible” half of the phonebook,and repeat the process, starting at step #2.and repeat the process, starting at step #2.

4) If the 4) If the entireentire phonebook is ever thrown out, then the person is unlisted! phonebook is ever thrown out, then the person is unlisted!

Page 10: Chapter 1 Introduction to Computers and C++ Programming

Chapter 1Chapter 1CS 140CS 140 Page Page 1010

The Software Life CycleThe Software Life CycleSpecificationSpecification

Clearly state the purpose of the Clearly state the purpose of the software, including full details of software, including full details of

the problem being solved.the problem being solved.

DesignDesignDevelop a solution to the problem, Develop a solution to the problem,

modularizing it and determining modularizing it and determining

specific pre- and post-conditions.specific pre- and post-conditions.

CodingCodingProgram the modules using a bottom-Program the modules using a bottom-up approach (with dummy drivers) or up approach (with dummy drivers) or

a top-down approach (with stubs).a top-down approach (with stubs).

TestingTestingDesign test scenarios for individual Design test scenarios for individual

modules, interaction between modules, interaction between

modules, and the entire program.modules, and the entire program.

MaintenanceMaintenanceRespond to “bugs” and “sugs”, Respond to “bugs” and “sugs”,

and determine when the software and determine when the software

has become obsolete.has become obsolete.

DOCUMENTATION!!! DOCUMENTATION!!!

Page 11: Chapter 1 Introduction to Computers and C++ Programming

Chapter 1Chapter 1CS 140CS 140 Page Page 1111

Introduction to C++Introduction to C++Ancient LanguagesAncient LanguagesFortran - Great for scientific computationsFortran - Great for scientific computationsCOBOL - Great for business file processingCOBOL - Great for business file processingVery special purpose, not good in generalVery special purpose, not good in general

Old LanguagesOld LanguagesC - General purpose language for UNIX systemsC - General purpose language for UNIX systemsPascal - General purpose language for PCsPascal - General purpose language for PCsEmphasis upon procedures instead of dataEmphasis upon procedures instead of data

Modern LanguagesModern LanguagesC++ - Object-oriented version of CC++ - Object-oriented version of CJava - Object-orientation with networking emphasisJava - Object-orientation with networking emphasisEmphasis upon the objects being manipulatedEmphasis upon the objects being manipulated

Page 12: Chapter 1 Introduction to Computers and C++ Programming

Chapter 1Chapter 1CS 140CS 140 Page Page 1212

A Sample C++ ProgramA Sample C++ Program#include <iostream> // This library facilitates input & output.#include <cmath> // This library enables math functions, like sqrt.using namespace std; // Assigns the program to a specific namespace.void main() // Every C++ program must have a "main" function.{ // Opening brace to contain main's statements. int nbr; // Declare variable "nbr" to be an integer. double root; // Declare variable "root" to be a long real number.

nbr = 2; // Set value of nbr to be 2. root = sqrt(nbr); // Calculate square root of nbr.

cout << "The square root of " // Output a message to the memory file << nbr << " is " << root // associated with the monitor (i.e., cout), << endl << endl; // including nbr, root, and a skipped line.

cout << "Enter a number: "; // Ask the user for a value for nbr. cin >> nbr; // Input from the file associated with the keyboard (cin). root = sqrt(nbr); // Calculate the square root of nbr's new value.

cout << "The square root of " // Output a message to the cout file << nbr << " is " << root // concerning the values of nbr and root, << endl << endl << endl; // followed bt two skipped lines.

return; // Terminate the program's execution.} // Closing brace to indicate end of main function.