0% found this document useful (0 votes)
52 views101 pages

Elements of A Computer System: - Hardware - Cpu - Main Memory - Secondary Storage - Input/Output Devices - Software

A computer system consists of hardware and software. The hardware includes the central processing unit, memory, storage, and input/output devices. Software programs control the computer and allow it to perform tasks. Programming involves analyzing problems, designing algorithms to solve them, coding the algorithms, and testing the programs.

Uploaded by

Dalip Bali
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views101 pages

Elements of A Computer System: - Hardware - Cpu - Main Memory - Secondary Storage - Input/Output Devices - Software

A computer system consists of hardware and software. The hardware includes the central processing unit, memory, storage, and input/output devices. Software programs control the computer and allow it to perform tasks. Programming involves analyzing problems, designing algorithms to solve them, coding the algorithms, and testing the programs.

Uploaded by

Dalip Bali
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 101

Elements of a Computer System

• Hardware
• CPU
• Main memory
• Secondary storage
• Input/Output devices
• Software

1
Hardware
• CPU
• Main memory: RAM
• Input/output devices
• Secondary storage

2
Central Processing Unit and Main Memory

• Central processing unit


– Brain of the computer
– Most expensive piece of hardware
– Carries out arithmetic and logical operations

3
Central Processing Unit and Main Memory
(cont'd.)

4
Central Processing Unit and Main Memory
(cont'd.)
• Random access memory
• Directly connected to the CPU
• All programs must be loaded into main
memory before they can be executed
• All data must be brought into main memory
before it can be manipulated
• When computer power is turned off,
everything in main memory is lost
5
Secondary Storage
• Secondary storage: device that stores
information permanently
• Examples of secondary storage:
– Hard disks
– Flash drives
– Floppy disks
– Zip disks
– CD-ROMs
– Tapes
6
Input/Output Devices
• Input devices feed data and programs into
computers
– Keyboard
– Mouse
– Secondary storage
• Output devices display results
– Monitor
– Printer
– Secondary storage
7
Software
• Software: programs that do specific tasks
• System programs take control of the
computer, such as an operating system
• Application programs perform a specific task
– Word processors
– Spreadsheets
– Games

8
The Language of a Computer
• Digital signals: sequences of 0s and 1s
• Machine language: language of a computer
• Binary digit (bit):
– The digit 0 or 1
• Binary code:
– A sequence of 0s and 1s
• Byte:
– A sequence of eight bits

9
The Language of a Computer (cont’d.)

10
The Language of a Computer (cont'd.)

• ASCII (American Standard Code for


Information Interchange)
– 128 characters
– A is encoded as 1000001 (66th character)
– 3 is encoded as 0110011

11
The Language of a Computer (cont'd.)

• EBCDIC
– Used by IBM
– 256 characters
• Unicode
– 65536 characters
– Two bytes are needed to store a character

12
The Evolution of Programming Languages
(cont'd.)

• Assembly language instructions are mnemonic


• Assembler: translates a program written in
assembly language into machine language

13
The Evolution of Programming Languages
(cont'd.)
• Using assembly language instructions, wages
= rates • hours can be written as:

LOAD rate
MULT hour
STOR wages

14
The Evolution of Programming Languages
(cont'd.)
• High-level languages include Basic, FORTRAN,
COBOL, Pascal, C, C++, C#, and Java
• Compiler: translates a program written in a
high-level language machine language
• The equation wages = rate • hours
can be written in C++ as:
wages = rate * hours;

15
Processing a C++ Program
#include <iostream>
using namespace std;
int main()
{
cout << "My first C++ program." << endl;
return 0;
}

Sample Run:
My first C++ program.

16
Processing a C++ Program (cont'd.)
• To execute a C++ program:
– Use an editor to create a source program in C++
– Preprocessor directives begin with # and are
processed by a the preprocessor
– Use the compiler to:
• Check that the program obeys the rules
• Translate into machine language (object program)

17
Processing a C++ Program (cont'd.)
• To execute a C++ program (cont'd.):
– Linker:
• Combines object program with other programs
provided by the SDK to create executable code
– Loader:
• Loads executable program into main memory
– The last step is to execute the program

18
Processing a C++ Program (cont'd.)

19
Programming with the Problem Analysis–
Coding–Execution Cycle
• Programming is a process of problem solving
• One problem-solving technique:
– Analyze the problem
– Outline the problem requirements
– Design steps (algorithm) to solve the problem
• Algorithm:
– Step-by-step problem-solving process
– Solution achieved in finite amount of time
20
The Problem Analysis–Coding–Execution
Cycle (cont’d.)
• Step 1: Analyze the problem
– Outline the problem and its requirements
– Design steps (algorithm) to solve the problem
• Step 2: Implement the algorithm
– Implement the algorithm in code
– Verify that the algorithm works
• Step 3: Maintenance
– Use and modify the program if the problem domain
changes
21
The Problem Analysis–Coding–Execution
Cycle (cont’d.)

22
The Problem Analysis–Coding–Execution
Cycle (cont'd.)
• Thoroughly understand the problem
• Understand problem requirements
– Does program require user interaction?
– Does program manipulate data?
– What is the output?
• If the problem is complex, divide it into
subproblems
– Analyze each subproblem as above

23
The Problem Analysis–Coding–Execution
Cycle (cont'd.)
• If problem was broken into subproblems
– Design algorithms for each subproblem
• Check the correctness of algorithm
– Can test using sample data
– Some mathematical analysis might be required

24
The Problem Analysis–Coding–Execution
Cycle (cont'd.)
• Once the algorithm is designed and
correctness verified
– Write the equivalent code in high-level language
• Enter the program using text editor

25
The Problem Analysis–Coding–Execution
Cycle (cont'd.)
• Run code through compiler
• If compiler generates errors
– Look at code and remove errors
– Run code again through compiler
• If there are no syntax errors
– Compiler generates equivalent machine code
• Linker links machine code with system
resources

26
The Problem Analysis–Coding–Execution
Cycle (cont'd.)
• Once compiled and linked, loader can place
program into main memory for execution
• The final step is to execute the program
• Compiler guarantees that the program follows
the rules of the language
– Does not guarantee that the program will run
correctly

27
Example 1-1
• Design an algorithm to find the perimeter and
area of a rectangle
• The perimeter and area of the rectangle are
given by the following formulas:

perimeter = 2 * (length +
width)
area = length * width
28
Example 1-1 (cont'd.)
• Algorithm:
– Get length of the rectangle
– Get width of the rectangle
– Find the perimeter using the following equation:
perimeter = 2 * (length + width)
– Find the area using the following equation:
area = length * width

29
Programming Methodologies

• Two popular approaches to programming


design
– Structured
– Object-oriented

30
Structured Programming
• Structured design:
– Dividing a problem into smaller subproblems
• Structured programming:
– Implementing a structured design
• The structured design approach is also called:
– Top-down (or bottom-up) design
– Stepwise refinement
– Modular programming

31
Object-Oriented Programming
• Identify components called objects
• Specify relevant data and possible operations
to be performed on that data
• Each object consists of data and operations on
that data
• An object combines data and operations on
the data into a single unit

32
Object-Oriented Programming (cont'd.)

• A programming language that implements


OOD is called an object-oriented programming
(OOP) language
• Learn how to represent data in computer
memory, how to manipulate data, and how to
implement operations
• Write algorithms and implement them in a
programming language

33
Object-Oriented Programming (cont'd.)

• Learn how to combine data and operations on


the data into a single unit called an object
• C++ was designed to implement OOD
• OOD is used with structured design

34
ANSI/ISO Standard C++
• C++ evolved from C
• C++ designed by Bjarne Stroustrup at Bell
Laboratories in early 1980s
• C++ programs were not always portable from
one compiler to another
• In mid-1998, ANSI/ISO C++ language standards
were approved

35
Summary
• Computer: electronic device that can perform
arithmetic and logical operations
• Computer system has hardware and software
• Central processing unit (CPU): brain
• Primary storage (MM) is volatile; secondary
storage (e.g., disk) is permanent
• Operating system monitors the overall activity of
the computer and provides services

36
Summary (cont'd.)
• Various kinds of languages, such as machine
language, assembly, high-level
• Algorithm: step-by-step problem-solving process;
solution in finite amount of time
• Problem-solving process has three steps:
– Analyze problem and design an algorithm
– Implement the algorithm in code
– Maintain the program

37
Summary (cont'd.)
• Structured design:
– Problem is divided into smaller subproblems
– Each subproblem is solved
– Combine solutions to all subproblems
• Object-oriented design (OOD): a program is a
collection of interacting objects
– Object: data and operations on those data

38
Introduction
• Computer program
– Sequence of statements whose objective is to
accomplish a task
• Programming
– Process of planning and creating a program

39
The Basics of a C++ Program
• Function: collection of statements; when
executed, accomplishes something
– May be predefined or standard
• Syntax: rules that specify which statements
(instructions) are legal
• Programming language: a set of rules, symbols,
and special words
• Semantic rule: meaning of the instruction
40
Comments
• Comments are for the reader, not the compiler
• Two types:
– Single line
// This is a C++ program. It prints the sentence:
// Welcome to C++ Programming.

– Multiple line
/*
You can include comments that can
occupy several lines.
*/

41
Special Symbols

• Special symbols

+ ?
- ,
* <=
/ !=
. ==
; >=

42
Reserved Words (Keywords)
• Reserved words, keywords, or word symbols
– Include:
• int
• float
• double
• char
• const
• void
• return

43
Identifiers
• Consist of letters, digits, and the underscore
character (_)
• Must begin with a letter or underscore
• C++ is case sensitive
– NUMBER is not the same as number
• Two predefined identifiers are cout and cin
• Unlike reserved words, predefined identifiers may
be redefined, but it is not a good idea

44
Identifiers (cont'd.)
• Legal identifiers in C++:
– first
– conversion
– payRate

45
Whitespaces
• Every C++ program contains whitespaces
– Include blanks, tabs, and newline characters
• Used to separate special symbols, reserved
words, and identifiers
• Proper utilization of whitespaces is important
– Can be used to make the program readable

46
Data Types
• Data type: set of values together with a set of
operations
• C++ data types fall into three categories:

47
Simple Data Types
• Three categories of simple data
– Integral: integers (numbers without a decimal)
– Floating-point: decimal numbers
– Enumeration type: user-defined data type

48
Simple Data Types (cont'd.)

• Different compilers may allow different ranges


of values

49
int Data Type
• Examples:
-6728
0
78
+763
• Positive integers do not need a + sign
• No commas are used within an integer
– Commas are used for separating items in a list

50
bool Data Type
• bool type
– Two values: true and false
– Manipulate logical (Boolean) expressions
• true and false
– Logical values
• bool, true, and false
– Reserved words

51
char Data Type
• The smallest integral data type
• Used for characters: letters, digits, and special
symbols
• Each character is enclosed in single quotes
– 'A', 'a', '0', '*', '+', '$', '&'
• A blank space is a character
– Written ' ', with a space left between the single
quotes

52
Floating-Point Data Types
• C++ uses scientific notation to represent real
numbers (floating-point notation)

53
Floating-Point Data Types (cont'd.)
• float: represents any real number
– Range: -3.4E+38 to 3.4E+38 (four bytes)
• double: represents any real number
– Range: -1.7E+308 to 1.7E+308 (eight bytes)

54
Floating-Point Data Types (cont'd.)
• Maximum number of significant digits
(decimal places) for float values is 6 or 7
• Maximum number of significant digits for
double is 15
• Precision: maximum number of significant
digits
– Float values are called single precision
– Double values are called double precision

55
Arithmetic Operators and Operator
Precedence
• C++ arithmetic operators:
– + addition
– - subtraction
– * multiplication
– / division
– % modulus operator
• +, -, *, and / can be used with integral and
floating-point data types
• Operators can be unary or binary
56
Order of Precedence
• All operations inside of () are evaluated first
• *, /, and % are at the same level of
precedence and are evaluated next
• + and – have the same level of precedence
and are evaluated last
• When operators are on the same level
– Performed from left to right (associativity)
• 3 * 7 - 6 + 2 * 5 / 4 + 6 means

57
Output
(((3 * 7) – 6) + ((2 * 5) / 4 )) + 6

58
Expressions
• If all operands are integers
– Expression is called an integral expression
• Yields an integral result
• Example: 2 + 3 * 5
• If all operands are floating-point
– Expression is called a floating-point expression
• Yields a floating-point result
• Example: 12.8 * 17.5 - 34.50

59
Mixed Expressions
• Mixed expression:
– Has operands of different data types
– Contains integers and floating-point
• Examples of mixed expressions:
2 + 3.5
6 / 4 + 3.9
5.4 * 2 – 13.6 + 18 / 2

60
Mixed Expressions (cont'd.)
• Evaluation rules:
– If operator has same types of operands
• Evaluated according to the type of the operands
– If operator has both types of operands
• Integer is changed to floating-point
• Operator is evaluated
• Result is floating-point
– Entire expression is evaluated according to
precedence rules

61
Input
• Data must be loaded into main memory
before it can be manipulated
• Storing data in memory is a two-step process:
– Instruct computer to allocate memory
– Include statements to put data into memory

62
Allocating Memory with Constants and
Variables
• Named constant: memory location whose
content can’t change during execution
• The syntax to declare a named constant is:
• In C++, const is a reserved word

63
Allocating Memory with Constants and
Variables (cont'd.)
• Variable: memory location whose content may
change during execution
• The syntax to declare a named constant is:

64
Putting Data into Variables
• Ways to place data into a variable:
– Use C++’s assignment statement
– Use input (read) statements

65
Assignment Statement
• The assignment statement takes the form:

• Expression is evaluated and its value is


assigned to the variable on the left side
• In C++, = is called the assignment operator

66
Assignment Statement (cont'd.)

67
Saving and Using the Value of an Expression

• To save the value of an expression:


– Declare a variable of the appropriate data type
– Assign the value of the expression to the variable
that was declared
• Use the assignment statement
• Wherever the value of the expression is
needed, use the variable holding the value

68
Declaring & Initializing Variables
• Variables can be initialized when declared:
int first=13, second=10;
char ch=' ';
double x=12.6;
• All variables must be initialized before they are
used
– But not necessarily during declaration

69
Input (Read) Statement
• cin is used with >> to gather input

• The stream extraction operator is >>


• For example, if miles is a double variable
cin >> miles;
– Causes computer to get a value of type double
– Places it in the variable miles

70
Input (Read) Statement (cont'd.)
• Using more than one variable in cin allows
more than one value to be read at a time
• For example, if feet and inches are
variables of type int, a statement such as:
cin >> feet >> inches;
– Inputs two integers from the keyboard
– Places them in variables feet and inches
respectively

71
Input (Read) Statement (cont'd.)

72
Variable Initialization
• There are two ways to initialize a variable:
int feet;
– By using the assignment statement
feet = 35;
– By using a read statement
cin >> feet;

73
Increment and Decrement Operators
• Increment operator: increment variable by 1
– Pre-increment: ++variable
– Post-increment: variable++
• Decrement operator: decrement variable by 1
– Pre-decrement: --variable
– Post-decrement: variable—
• What is the difference between the following?
x = 5; x = 5;
y = ++x; y = x++;
74
Output

• The syntax of cout and << is:

– Called an output statement


• The stream insertion operator is <<
• Expression evaluated and its value is
printed at the current cursor position on
the screen

75
Output (cont'd.)
• A manipulator is used to format the output
– Example: endl causes insertion point to move to
beginning of next line

76
Output (cont'd.)

• The new line character is '\n'


– May appear anywhere in the string
cout << "Hello there.";
cout << "My name is James.";
• Output:
Hello there.My name is James.
cout << "Hello there.\n";
cout << "My name is James.";
• Output :
Hello there.
My name is James.
77
Output (cont'd.)

78
Preprocessor Directives
• C++ has a small number of operations
• Many functions and symbols needed to run a C++
program are provided as collection of libraries
• Every library has a name and is referred to by a
header file
• Preprocessor directives are commands supplied to
the preprocessor
• All preprocessor commands begin with #
• No semicolon at the end of these commands

79
Preprocessor Directives (cont'd.)
• Syntax to include a header file:

• For example:

#include <iostream>

– Causes the preprocessor to include the header file


iostream in the program

80
namespace and Using cin and cout in a
Program
• cin and cout are declared in the header file
iostream, but within std namespace
• To use cin and cout in a program, use the
following two statements:
#include <iostream>
using namespace std;

81
Creating a C++ Program
• C++ program has two parts:
– Preprocessor directives
– The program
• Preprocessor directives and program
statements constitute C++ source code (.cpp)
• Compiler generates object code (.obj)
• Executable code is produced and saved in a
file with the file extension .exe

82
Creating a C++ Program (cont'd.)
• A C++ program is a collection of functions, one of
which is the function main
• The first line of the function main is called the
heading of the function:
– int main()
• The statements enclosed between the curly braces
({ and }) form the body of the function
– Contains two types of statements:
• Declaration statements
• Executable statements

83
Debugging: Understanding and Fixing Syntax
Errors
• Compile a program
– Compiler will identify the syntax error
– Specifies the line numbers where the errors occur
Example2_Syntax_Errors.cpp
c:\chapter 2 source code\example2_syntax_errors.cpp(9) : error
C2146: syntax error :
missing ';' before identifier 'num'
c:\chapter 2 source code\example2_syntax_errors.cpp(11) :
error C2065: 'tempNum' :
undeclared identifier

• Learn how to spot and fix syntax errors

84
Use of Semicolons, Brackets, and Commas

• All C++ statements end with a semicolon


– Also called a statement terminator
• { and } are not C++ statements
• Commas separate items in a list

85
Naming Identifiers
• Identifiers can be self-documenting:
– CENTIMETERS_PER_INCH
• Avoid run-together words :
– annualsale
– Solution:
• Capitalize the beginning of each new word:
annualSale
• Inserting an underscore just before a new word:
annual_sale

86
More on Assignment Statements
• C++ has special assignment statements called
compound assignments
+=, -=, *=, /=, and %=
• Example:
x *= y;

87
Programming Example: Convert
Length
• Write a program that takes as input a given length
expressed in feet and inches
– Convert and output the length in centimeters
• Input: length in feet and inches
• Output: equivalent length in centimeters
• Lengths are given in feet and inches
• Program computes the equivalent length in
centimeters
• One inch is equal to 2.54 centimeters
88
Programming Example: Convert Length
(cont'd.)
• Convert the length in feet and inches to all
inches:
– Multiply the number of feet by 12
– Add given inches
• Use the conversion formula (1 inch = 2.54
centimeters) to find the equivalent length in
centimeters

89
Programming Example: Convert Length
(cont'd.)
• The algorithm is as follows:
– Get the length in feet and inches
– Convert the length into total inches
– Convert total inches into centimeters
– Output centimeters

90
Programming Example: Variables and
Constants
• Variables
int feet; //variable to hold given feet
int inches; //variable to hold given inches
int totalInches; //variable to hold total inches
double centimeters; //variable to hold length in
//centimeters

• Named Constant
const double CENTIMETERS_PER_INCH = 2.54;
const int INCHES_PER_FOOT = 12;

91
Programming Example: Main Algorithm

• Prompt user for input


• Get data
• Echo the input (output the input)
• Find length in inches
• Output length in inches
• Convert length to centimeters
• Output length in centimeters

92
Programming Example: Putting It Together

• Program begins with comments


• System resources will be used for I/O
• Use input statements to get data and output
statements to print results
• Data comes from keyboard and the output will
display on the screen
• The first statement of the program, after
comments, is preprocessor directive to include
header file iostream
93
Programming Example: Putting It Together
(cont'd.)
• Two types of memory locations for data
manipulation:
– Named constants
• Usually put before main
– Variables
• This program has only one function (main),
which will contain all the code
• The program needs variables to manipulate
data, which are declared in main
94
Programming Example: Body of the Function

• The body of the function main has the


following form:
int main ()
{
declare variables
statements
return 0;
}
95
Programming Example: Writing a Complete
Program
• Begin the program with comments for
documentation
• Include header files
• Declare named constants, if any
• Write the definition of the function main

96
Programming Example: Writing a Complete
Program (cont’d.)

97
Programming Example: Sample Run

Enter two integers, one for feet, one for inches: 15 7

The numbers you entered are 15 for feet and 7 for inches.
The total number of inches = 187
The number of centimeters = 474.98

98
Summary
• C++ program: collection of functions where each
program has a function called main
• Identifier consists of letters, digits, and
underscores, and begins with letter or underscore
• The arithmetic operators in C++ are addition (+),
subtraction (-),multiplication (*), division (/), and
modulus (%)
• Arithmetic expressions are evaluated using the
precedence associativity rules
99
Summary (cont'd.)
• All operands in an integral expression are integers
and all operands in a floating-point expression
are decimal numbers
• Mixed expression: contains both integers and
decimal numbers
• Use the cast operator to explicitly convert values
from one data type to another
• A named constant is initialized when declared
• All variables must be declared before used
100
Summary (cont'd.)
• Use cin and stream extraction operator >>
to input from the standard input device
• Use cout and stream insertion operator <<
to output to the standard output device
• Preprocessor commands are processed before
the program goes through the compiler
• A file containing a C++ program usually ends
with the extension .cpp

101

You might also like