cisc 1110 introduction to computing using the c++ programming language
Post on 19-Jan-2016
218 Views
Preview:
TRANSCRIPT
CISC 1110
Introduction to Computing
Using the C++ Programming Language
Why Program?
Computer – programmable machine designed to follow instructions
Program – instructions in computer memory to make it do something
Programmer – person who writes instructions (programs) to make computer perform a task
SO, without programmers, no programs; without programs, the computer cannot do anything
1-2CISC 1110 Introduction to Computing with C++
von Neumann Architecture Although specific components may vary, virtually all modern computers
have the same underlying structure known as the von Neumann architecture named after computer pioneer, John von Neumann, who popularized the
design in the early 1950's The von Neumann architecture identifies 3 essential components
1. Input/Output Devices (I/O) allow the user to interact with the computer2. Memory stores information to be processed as well as programs
(instructions specifying the steps necessary to complete specific tasks)3. Central Processing Unit (CPU) carries out the instructions to process
information
1-3CISC 1110 Introduction to Computing with C++
Hardware: The CPUThe Central Processing Unit: (CPU)
Carries out the instructions of the programs.Moves data from one part of machine to another
part of the machine.Manipulates data (e.g., adds, compares, etc.).CPU cycle:
Fetch: get the next program instruction from main memory
Decode: interpret the instruction and generate a signal
Execute: route the signal to the appropriate component to perform an operation
1-4CISC 1110 Introduction to Computing with C++
Central Processing Unit (CPU) The CPU is the "brains" of the computer, responsible for controlling its
inner workings made of circuitry – electronic components wired together to control the flow of
electrical signals the circuitry is embedded in a small silicon chip, 1-2 inches square despite its small size, the CPU is the most complex part of a computer
(CPU circuitry can have 10's of millions of individual components) commercial examples: Intel Pentium 4, AMD Athlon, Motorola PowerPC G4
1-5CISC 1110 Introduction to Computing with C++
Hardware: Memory
Modern computers are digital devices, meaning they store and process information as binary digits (bits)bits are commonly represented as either 0
or 1bits are the building block of digital memory
by grouping bits together, large ranges of values can be represented
1-6CISC 1110 Introduction to Computing with C++
Memory Memory capacity is usually specified in bytes
a byte is a collection of 8 bits – so can represent a range of 28 = 256 values large collections of bytes can be specified using prefixes
Since a byte is sufficient to represent a single character, think of memory in terms of text a kilobyte can store a few paragraphs (roughly 1 thousand characters) a megabyte can store a book (roughly 1 million characters) a gigabyte can store a small library (roughly 1 billion characters) a terabyte can store a book repository (roughly 1 trillion characters)
1-7CISC 1110 Introduction to Computing with C++
MemoryInternal Memory
Used to store programs and data that are currently being processed by the CPU
RAM - Random Access MemoryROM - Read Only MemoryBit / Byte / Word
External Memory Used for long term and mass storage of programs
and data magnetic: floppy or hard disk drive, internal or externaloptical: CD or DVD drive flash: USB flash drive
1-8CISC 1110 Introduction to Computing with C++
Memory
Main (primary) memory)is fast and expensive memory is volatile – data is lost when the computer is
turned off External (secondary) memory is slower but cheaper
memory is permanent – useful for storing long-term data
1-9CISC 1110 Introduction to Computing with C++
Hardware: Input/Output Devices
Input Devices: used to input programs and data into the computer.
keyboard, mouse, microphone, scanner, digital camera, disk drive, CD/DVD drive, USB flash drive
Output Devices: used to receive output information from the computer.
Computer screen, printer, speakers, disk drive, CD/DVD recorder, USB flash drive
1-10CISC 1110 Introduction to Computing with C++
Software
Operating system software programs that manage the computer hardware and the
programs that run on the computer controls how the CPU, memory, and I/O devices work
together how many programs can run at once?
Single tasking - one program at a time (MS-DOS) Multitasking – multiple programs at a time (UNIX, Windows
XP/Vista/7) how many people can use computer at the same time?
Single user – MS-DOS, early versions of Windows Multiuser - UNIX
Application software programs that provide services to the user. Ex: word processing, games, programs to solve specific problems
1-11CISC 1110 Introduction to Computing with C++
Types of Computers Different types of computers have different characteristics
supercomputers: powerful but expensive; used for complex computations (e.g., weather forecasting, engineering design and modeling)
desktop computers: less powerful but affordable; used for a variety of user applications (e.g., email, Web browsing, document processing)
laptop computers: similar functionality to desktops, but mobile palmtop computers: portable, but limited applications and screen size
1-12CISC 1110 Introduction to Computing with C++
Common Desktop Hardware
1-13CISC 1110 Introduction to Computing with C++
Machine Languages
The first programming languages were known as machine languages a machine language consists of instructions that correspond directly to
the hardware operations of a particular machine i.e., instructions deal directly with the computer’s physical components including main
memory, registers, memory cells in CPU very low level of abstraction
machine language instructions are written in binary programming in machine language is tedious and error prone code is nearly impossible to understand and debug
excerpt from a machine language program
1-14CISC 1110 Introduction to Computing with C++
High-Level Languages In the early 1950’s, assembly languages evolved from machine
languages an assembly language substitutes words for binary codes much easier to remember and use words, but still a low level of abstraction
(instructions correspond to hardware operations)
In the late 1950's, high-level languages were introduced high-level languages allow the programmer to write code closer to the way
humans think (as opposed to mimicking hardware operations) a much more natural way to solve problems plus, programs are machine independent
1-15CISC 1110 Introduction to Computing with C++
High-Level Languages
Two high level languages that perform the same task (in JavaScript and C++)
1-16CISC 1110 Introduction to Computing with C++
Program TranslationUsing a high-level language, the programmer is able to reason at a high-level of abstraction
but programs must still be translated into machine language that the computer hardware can understand/execute
There are two standard approaches to program translation
Interpreters The interpreter reads one high-level statement at a time,
immediately translating and executing the statement before processing the next one
JavaScript is an interpreted languageCompilers
1-17CISC 1110 Introduction to Computing with C++
Compilers The compilation approach relies on a program known as a compiler to
translate the entire high-level language program into its equivalent machine-language instructions
the resulting machine-language program can be executed directly on the computer
most languages used for the development of commercial software employ the compilation technique (C, C++)
1-18CISC 1110 Introduction to Computing with C++
High-level Program to Executable
1-19CISC 1110 Introduction to Computing with C++
Interpreters and CompilersInterpreter
produces results almost immediately particularly useful for dynamic, interactive features of web
pages program executes more slowly (slight delay between the
execution of statements)Compiler
produces machine-language program that can run directly on the underlying hardware
program runs very fast after compilation must compile the entire program before execution used in large software applications when speed is of the
utmost importance1-20CISC 1110 Introduction to Computing with C++
Simple C++ Programs/* display a message on the monitor */
#include <iostream>
using namespace std;
int main()
{
cout << "Welcome to computer programming!” << endl;
return 0;
}
/* type in a number on the keyboard & echo it to the screen*/
#include <iostream>
using namespace std;
int main()
{
int number;
cout << "Type in a number on the keyboard” << endl;
cin >> number;
cout << "The number is “ << number << endl;
return 0;
}
/* a program to do some addition */
#include <iostream>
using namespace std;
int main()
{
int number;
cout << "Type in a number on the keyboard” << endl;
cin >> number;
number = number + 5;
cout << "The new number is “ << number << endl;
return 0;
}
1-21CISC 1110 Introduction to Computing with C++
The C++ Programming Language
Like any language, C++ has:A Lexicon (a dictionary):
A set of allowable basic elements.A Syntax (a grammar):
Rules for combining the basic elements into programs.
A Semantics (meaning):Rules for understanding what programs and
their parts cause the computer to do.
1-22CISC 1110 Introduction to Computing with C++
The Lexical Elements of C++
Keywords and Standard Identifiers:Keywords and Standard (Predeclared)
Identifiers that have special meaning in C++:main char cout for if switch int float cin
while else structCan not be used for another pruposeThe meaning of a keyword is reserved and
can not change.The meaning of a predeclared identifier
can be changed – but it is recommended that you do not.
1-23CISC 1110 Introduction to Computing with C++
The Lexical Elements of C++
Identifiers:Words that the writer of the program chooses
to name things (e.g., functions, variables, etc.).
Identifiers must start with a letter or an underbar. The remaining characters are any combination of letters, numerals, and underbars. (Note: upper and lower case are different.)number1 Number1 sum R2D2 Last_Name
1-24CISC 1110 Introduction to Computing with C++
The Lexical Elements of C++String Literal:
Anything that is written between double quotes."The final sum is " "hello!"
Numeric Constant:Numerals which express specific numeric
values.5 -227 33.676
Special Characters:Characters or groups of characters that have a
special meaning in C++ (e.g., operators) ; , { } < <= > >= == + - * % & && | ||
1-25CISC 1110 Introduction to Computing with C++
The Program Syntax of C++
The Structure of a Simple C++ Program:/* comment on purpose of program */ // comment#directives // compiler directivesusing namespace std; // allows .h header filesint main() // program header { // start of programdata_type list_of_identifiers; // variable declarations<data_type1> <list1>;<data_type2> <list2>;...action part of the program // do program's taskreturn 0;} // end of program
1-26CISC 1110 Introduction to Computing with C++
The Program Syntax of C++A variable is a part (location) of memory that is
capable of holding a piece of data that may change its value.
The 'variable declaration' Part of the Program: Indicates how much space in memory that the
action part of the program will need, and the identifiers that will be used to refer to those locations. These identifiers are called the variables of the program.
The Action Part of the Program:The series of C++ statements that accomplish the
program's task.1-27CISC 1110 Introduction to Computing with C++
The Statement Syntax of C++ Rules for combining lexical elements into C++ statements. The Declaration Statement Syntax:
data_type variable_1,...,variable_n; int number; int num1, num2, num3; integer number; // illegal ints num1, num2, num3; // illegal int switch; // illegal
C++ Expressions: an integer constant 3 an identifier x an identifier, an operator, an integer constant sum + 10 an integer constant, an operator, an identifier 10 * y an identifier, an operator, an identifier sum – num
The Assignment Statement Syntax: variable = expression;
z = 33; x = y; sum = number1 + number2 - 5; sqnumber = number * number;
1-28CISC 1110 Introduction to Computing with C++
The cout Stetement The cout Statement Syntax:
cout << item1 << item2 << ... << itemN; where << is the insertion operator and each item is either a string literal
or an expression or the stream manipulator
endl endl outs a newline character and flushes the output buffer.
Example:number = 4;sqnumber = number * number;cout<<"The square of “<<number<<“is “<<sqnumber<<endl;
The above code prints:The square of 4 is 16
Another Example: cout << number+1 << endl;
1-29CISC 1110 Introduction to Computing with C++
C++ Statement Types C++ statements can be simple or compound. Simple Statement (Examples):
cout << "hello! << endl; x = 25;
Compound Statement:{ statement-list}
Statement-List: A list of C++ statements. Each statement may be simple or compound.
statement; x=3;statement; y=x*2 -3;...statement; cout<<“x= “<<x<<“ y= “<<y<<endl;statement;
1-30CISC 1110 Introduction to Computing with C++
Arithmetic Operations Addition:
a + b Subtraction:
a - b Multiplication:
a * b Division:
a / b a % b (yields remainder (or modulus) of integer division)
Examples:int number,sqnumber;int num1,num2;int quotient,remainder;
number = 4;sqnumber = number * number; //sqnumber is assigned 16
num1 = 5;num2 = 37;quotient = num2 / num1; //quotient is assigned 7remainder = num2 % num1; //remainder is assigned 2
1-31CISC 1110 Introduction to Computing with C++
Conditions A C++ condition is a Boolean expression of the form:
expression_1 relational_operator expression_2 which takes on the value of either TRUE or FALSE.
Relational Operators:< less than<= less than or equal to== equal to!= not equal to> greater than>= greater than or equal to
Example:Let: n = 15; sum = 11
Then:sum < n TRUEn <= 0 FALSEn > 0 TRUEn >= sum - 1 TRUEn == n + 1 FALSEn + 2 != 17 FALSE
1-32CISC 1110 Introduction to Computing with C++
A Complete C++ ProgramProblem: Write a C++ program to compute and print the squares of the integers from 4 to 8.
Program:/* program to print the number from 4 to 8 and their squares */
#include <iostream>
using namespace std;
int main()
{
int number,sqnum;
number = 4;
sqnum = number * number;
cout<<"number = “<<number<<“ sqnum = “<<sqnum<<endl;
number = 5;
sqnum = number * number;
cout<<"number = “<<number<<“ sqnum = “<<sqnum<<endl;
number = 6;
sqnum = number * number;
cout<<"number = “<<number<<“ sqnum = “<<sqnum<<endl;
number = 7;
sqnum = number * number;
cout<<"number = “<<number<<“ sqnum = “<<sqnum<<endl;
number = 8;
sqnum = number * number;
cout<<"number = “<<number<<“ sqnum = “<<sqnum<<endl;
return 0;
}
Output of the Program:number = 4 sqnum = 16
number = 5 sqnum = 25
number = 6 sqnum = 36
number = 7 sqnum = 49
number = 8 sqnum = 64
1-33CISC 1110 Introduction to Computing with C++
Iteration Repeating a Series of Instructions:
number = 4;sqnum = number * number;cout<<"number = “<<number<<“ sqnum = “<<sqnum<<endl;number = 5;sqnum = number * number;cout<<"number = “<<number<<“ sqnum = “<<sqnum<<endl;number = 6;sqnum = number * number;cout<<"number = “<<number<<“ sqnum = “<<sqnum<<endl;
The for statement (or for loop) repeats a group of statements while a control-variable is between its initial and final values.
for (part1; part2; part3) body (statement) of the loop;
wherepart1 - (control variable) initialization statement(s),part2 - a relational test (a boolean expression),part3 - (control variable) update statement(s).body of loop can be either a simple statement or a compound statement enclosed with braces.
1-34CISC 1110 Introduction to Computing with C++
Iteration - Examplesfor (j = 1; j <= 10; j = j+1)
cout << j <<endl;
for (number = 8; number >= 4; number = number-1)
{
sqnum = number * number;
cout<<"number = “<<number<<“ sqnum = “<<sqnum<<endl;
}
for (number = 4; number <= 6; number = number+1)
{
sqnum = number * number;
cout<<"number = “<<number<<“ sqnum = “<<sqnum<<endl;
}
1-35CISC 1110 Introduction to Computing with C++
Revised Program with Iteration
Revised Program:
Write a C++ program to compute and print the squares of the integers from 4 to 8.
/* program to print the number from 4 to 8 and their squares */
#include <iostream>
using namespace std;
int main()
{
int number,sqnum;
for (number = 4; number <= 8; number=number+1)
{
sqnum = number * number;
cout << "number = “ << number<< “ sqnum = “ << sqnum<< endl;
}
return 0;
}
Trace of the Program
number = 4 sqnum = 16
number = 5 sqnum = 25
number = 6 sqnum = 36
number = 7 sqnum = 49
number = 8 sqnum = 64
1-36CISC 1110 Introduction to Computing with C++
Shorthand for Increments and Decrements
Because increments and decrements are so common, C++ has a shorthand notation for these operations:j = j + 1; can be replaced by j++; i = i - 1; can be replaced by i--;
Example: Use in for loopsfor (number = 4; number <= 8; number++){ sqnum = number * number; cout<<"number = “<<number<<“ sqnum = “<<sqnum<<endl;}
Example:for (number = 8; number >= 4; number--){ sqnum = number * number; cout<<"number = “<<number<<“ sqnum = “<<sqnum<<endl;}
Examplefor (i = 1, j = 7; i < j; i=i+3, j++)cout << i <<“,”<< j << endl;
Output:1, 74, 87, 9
Note: ++j will also increment (can be different than j++)Note: --i will also decrement (can be different than i--)
1-37CISC 1110 Introduction to Computing with C++
top related