Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
21 views

Programing I

This document outlines an introduction to computers and C++ programming course. The course will cover topics such as problem solving methodology, algorithms, programming basics like data types and input/output, control structures, arrays, functions, and structures. References for the course include a C++ programming textbook and an Arabic language textbook on C++ programming.

Uploaded by

mhmdalswydy808
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Programing I

This document outlines an introduction to computers and C++ programming course. The course will cover topics such as problem solving methodology, algorithms, programming basics like data types and input/output, control structures, arrays, functions, and structures. References for the course include a C++ programming textbook and an Arabic language textbook on C++ programming.

Uploaded by

mhmdalswydy808
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 79

Introduction to Computers

and C++ Programming


Dr. Methaq
Course out line
 Introduction to Computers,
 Problem-Solving Methodology
 Algorithms and C++ programming
 Programming Basics (data types, operators and expressions, keywords and
identifiers, variables and assignment, basic input/output routines)
 Control Structures (sequence, selection [if, if\else, switch], iteration [for,
while, do/while]), break and continue statements
 Arrays (One dimension and Two dimension(
 Functions
 Structures
Refrences

Deitel and Deitel, C++ How to program, 10th ed.


‫ساري علي الحاج البرمجه بلغة سي بلس بلس‬
2010‫دار الشعاع و‬
Introduction to Computer

 A computer is an electronic device, operating under the control of


instructions stored in its own memory that can accept data (input}, process
the data according to specified rules, produce information (output}, and
store the information for future use
Functionalities of a computer
Any digital computer carries out five functions in gross terms:
 Takes data as input.
 Stores the data/instructions in its memory and use them when required.
 Processes the data and converts it into useful information.
 Generates the output
 Controls all the above four steps
Functionalities of a computer
Data vs. Information

Data is a representation of a fact or idea:


Number ,Word, Picture, sound
Information is data that has been organized or presented in a meaningful.
Data vs. Instruction
Computer Classification
Computers can be classified by size and power to:

 Personal computer {PCs}

 Mobile computer and mobile device.

 Minicomputer

 Mainframe

 Supercomputer
Computer Classification

Compare between the previous type of computers based on

 Price

 Processing Speed

 Storage Capacity

 Powerful

 Single-user or Multi-user

 Computer Size

 Companies size
Computer Classification

 A single-user computer that can perform all input, processing, output and storage operations on
its own.
 Two types of personal computer are desktop computer and workstation.
Mobile computer and mobile device.

 notebook computer
 Portable, small enough to fit on your lap.
 Also called a laptop computer.
 Usually more expensive than a desktop
computer with equal capabilities.
Mobile computer and mobile device.

Tablet PC

 A computer that is shaped like a slate or notebook.


 Normally fitted with a touch screen
for users to enter input through a stylus.
Mobile computer and mobile device.

 A powerful mobile phone

 Designed to run a variety of applications


in addition to phone service.

 Compare it with the tablet?

 Internet access is an important feature of smartphones.


(3G or 4G) (Wi-Fi Service)
Minicomputer

Mid-Range Server
 More powerful and larger that a workstation and can support up to 4,000
users at one time.
 Formerly known as minicomputer.
Mainframe Computer

 Very powerful, expensive computer that supports thousands of


connected users
Supercomputer

 The fastest, most powerful, most expensive computer.

 Used for applications requiring complex mathematical calculations.


COMPUTER System

 Isa collection of entities ( hardware, software and


User) that are designed to receive, process, manage
and present information in a meaningful format.

Computer hardware - Are physical parts/ intangible parts of a computer. eg Input devices, output devices,
central processing unit and storage devices

 Computer software - also known as programs or applications. They are classified into two classes namely -
system software and application software

r- The user commands the computer system to execute on instructions.


Computer
system

Hardware Software

CPU System Application


Programs Programs

Secondary
RAM I/O Devices
Storage
COMPUTER HARDWARE
 There are five main hardware components in a computer system: Input,
Processing, Storage, Output and Communication devices.
Input Device
 Input device is any peripheral piece of computer

hardware equipment to provide data

and control signals to the computer.

 Input device Translate data from

a form that humans understand

to one that the computer can work with.

Most common are keyboard and mouse


Processing Devices/Processor/ microprocessor
(CPU)
CENTRAL PROCESSING UNIT ( C P U) :the brain or the heart of a computer.
 Typical capabilities of CPU include:
add
subtract
multiply
divide
move data from location to location
 consist of three units namely -
i) Control Unit ( C U)
ii) Arithmetic logic Unit ( A L U)
iii) Main Memory unit ( M M U)
CENTRAL PROCESSING UNIT ( C P U)
Arithmetic Logic Unit (ALU)
 Executes all arithmetic and logical operations.

 Arithmetic calculations like as addition, subtraction,


multiplication and division.

 Logical operation like compare numbers, letters, or


special characters
Control Unit (CU)
 controls and co-ordinates computer components.
 Every Operation such as storing , computing and
retrieving the data should be governed by the control
unit.
Memory unit (MU )
 MU is used for storing the data.
 Long list of memory locations
 Each contains zeros and ones
 Can change during program execution
 Binary Digit or Bit
A digit that can only be zero or one
 Byte
 Each memory location has eight bits
 Address
 Number that identifies a memory location
Memory unit (MU )…
 The Memory unit is classified into two types:

1) Primary Memory /Main memory: stores instructions and


data while a program is running.

2) Secondary Memory :Stores instructions and data between sessions

 Primary memory: which are treated as:

ROM: It represents Read Only Memory that stores data


and instructions even when the computer is turned off.

RAM: It represents Random Access Memory that stores


data and instructions when the computer is turned on.
Secondary Memory

 Stores data and programs permanently

 its retained after the power is turned off

 Main Examples

Hard Disk

Optical Disk

Flash memory
Output Device
 An output device is any piece of computer

hardware equipment used to communicate

the results of data processing carried out by

a computer

 Converts the electronically generated

information into human-readable form.


Software
The term software refers to the set of computer data and
instructions that direct the hardware to accomplish a task.
often broken into major categories:

1. .

2.

3.
Known as Operating System is used to control all hardware
components of computer and serves as an interface between
user and hardware.
1. Control all Components of Computer
2. Allows us to communicate with the computer
For example of OS:
 Microsoft Windows
 Linux
 Unix
 Mac OSX
 DOS
Application software is used or design for the specific purpose of the user.

For example:

 MS-WORD

 MS-EXCEL

 MS-POWER POINT

 MS-ACCESS

 Game

 Paint
 A program is a sequence of instructions that instructs the
computer what to do.
 What is programming?
 Programming is the ability to get computer to perform useful
tasks for us
 Programming is about problem solving
 Programming is about solution development
 From the problem to an algorithm
 From algorithm to a program
 Programming is about how computer works
Programming Languages
 Types of Programming Languages
 Machine language
 Assembly language
 High level languages
 High Level Programming Languages:
Imperative (Procedural) Languages e.g. C , C++ , Java
 Resemble human languages
Are designed to be easy to read and write
Must be translated to zeros and ones for the CPU
to execute a program
Compilers
 Translate high-level language to
machine language

 Source code
 the original program in a high level language
 Object code
 the translated version in machine language

Slide 35
Problem Solving
 Whatever activity a human being or machine do for achieving a specified
objective comes under problem solving.

 Example1: If you are watching a news channel on your TV and you want
to change it to a sports channel, you need to do something i.e. move to
that channel by pressing that channel number on your remote. This is a
kind of problem solving.

 Example 2: Some students in a class plan to go on picnic and decide to


share the expenses among them. So calculating total expenses and the
amount an individual have to give for picnic is also a kind of problem
solving
Problem-Solving Methodology

The process for problem-solving has five steps:

1. State the problem clearly.

2. Describe the input and output information.

3. Work the problem by hand (or with a calculator) for a simple set of
data.

4. Develop an algorithm and convert it to a computer program. The


algorithm can be listed as operations that are performed one after
another.

5. Test the program with a variety of data.


Problem-Solving Methodology….
Algorithms
 We can consider algorithms to be practical solutions to
problems. These solutions are not answers, but specific
instructions for getting answers.

 Algorithm can be defined as: “A sequence of steps to be


processed for getting desired output from a given input.”

 Before writing an algorithm for a problem, one should find out


what is/are the inputs to the algorithm and what is/are
expected output after running the algorithm.

 Computers are machines found to solve problems.


Characteristics Of An Algorithm
 Every algorithm should have the following 5 characteristics :

1. Input and Output.

2. Definiteness: Each step must be define precisely.

3. Effectiveness : An algorithm must provide the correct answer to the


problem.

4. Termination: must terminate after a finite number of steps.

5. An algorithm must be general. This means that it must solve every


instance of the problem.

For example, a program that computes the area of a rectangle should


work on all possible dimensions of the rectangle, within the limits of
the programming language and the machine.
Development of Algorithms

 Computer algorithms work via input and output. They take


the input and apply each step of the algorithm to
generate an output.

 For example, a search engine is an algorithm that takes a


search query as an input and searches its database for
items relevant to the words in the query. It then outputs
the result
Development of Algorithms

 To describe the process of solving a problem with sequential logic, we use the top-down

design. There are several ways to develop an algorithm:

 Pseudocode: uses English-like statements to describe the steps in an algorithm.

 Flowcharts: FLOWCHART Flowchart is a diagrammatic representation of an

algorithm. Flowchart is very helpful in writing program and explaining program to

others

 The next example shows an algorithm described by a pseudocode and flowchart.


What is a Pseudocode
 A pseudocode is an informal way of writing a program.

 it is not a computer program.

 It only represents the algorithm of the program in natural


language and mathematical notations.

 there is no particular programming language to write a


pseudocode.

 Unlike in regular programming languages, there is no syntax to


follow when writing a pseudocode.

 So, pseudocodes are Not executed on computers.


Why is Pseudocode Necessary?
 The programming process is a complicated one.

 You must first understand the program specifications.

 Then you need to organize your thoughts and create the


program.

 You must break the main tasks that must be accomplished into
smaller ones in order to be able to write fully developed code.

 Writing Pseudocode will save you time later during the


construction & testing phase of a program's development.
Example of Pseudocode

 Problem1: Write an algorithm to read two numbers and find their sum.

 Inputs to the algorithm: First num1. Second num2. Expected output: Sum
of the two numbers.

 Algorithm:

Step1: Start

Step2:Read\input the first num1.

Step3: Read\input the second num2.

Step4: Sum= num1+num2

Step5: Print Sum

Step6: End
Example of Pseudocode….
 Problem 2: Find the area of a Circle of radius r.
Inputs to the algorithm: Radius r of the Circle
Expected output: Area of the Circle
Algorithm:
Step1: Read\input the Radius r of the Circle
Step2: Area= π r 2
Step3: Print Area
Step4:END
Example of pseudocode
 Example
Write a algorithm to find out number is odd or even?
step 1 : start
step 2 : input number
step 3 : rem=number mod 2
step 4 : if rem=0 then
print "number even“
else print "number odd"
end if
step 5 : stop
Flowchart

 The flowchart is a diagram which visually presents the flow


of data through processing systems

 A flowchart is to describe the sequence of steps


/operations and logic of solving a problem, before writing
a computer program
Flowchart Symbols
Examples of flowcharts in programming

 Draw a flowchart to add two


numbers entered by user
Some Examples Of Flowcharts
Problem: Find the area of a circle of radius r.
Examples of flowcharts in programming
 Problem3: Algorithm to find the largest number between two numbers.
Basic Control Structures

 The pseudo-code and flowchart illustrate the three types


of control structures. They are:

 1. Sequence

 2.Selection

 3. Loop (Repetition)
Sequence

 The sequence is exemplified by sequence of statements


place one after the other, the one above or before
another gets executed first.

 In flowcharts, sequence of statements is usually contained


in the rectangular process box
Calculate Pay - Sequence

 Begin
 input hours
 input rate pay = hours * rate
 print pay
 End
Selection

 The selection refers to a binary decision based on some


condition. If the condition is true, one of the two
branches is explored; if the condition is false, the other
alternative is taken. This is usually represented by the

“if-then” construct in pseudocodes and programs. In


flowcharts, this is represented by the diamond-shaped
decision box.
Selection
 Problem1: write algorithm to find the greater number
between two numbers

1.Start
2. Read/input A and B
3. If A greater than B then C=A
4. if B greater than A then C=B
5. Print C
6. End
Selection

 Problem2: write algorithm to find the result of equation:

 1. Start

 2. Read/input x

 3. If X Less than zero then F=-X

 4. if X greater than or equal zero then F=X

 5. Print F

 6. End
Selection
 Problem3: A algorithm to find the largest value of any
three numbers.
1.Start

2. Read/input A,B and C

3. If (A>=B) and (A>=C) then Max=A

4. If (B>=A) and (B>=C) then Max=B

5. If (C>=A) and (C>=B) then Max=C

6. Print Max

7. End
Loop (repetition structure)
 The loop allows a statement or a sequence of
statements to be repeatedly executed based
on some loop condition.

 It is represented by the „while‟ and „for‟


constructs in most programming languages

 In the flowcharts, a back arrow hints the


presence of a loop.

 A trip around the loop is known as iteration.


Loop (repetition structure)

Problem1: An algorithm to calculate even numbers between 0


and 99

1.Start

2. I ← 0

3. Write I in standard output

4. I ← I+2

5. If (I <=98) then go to line 3

6. End
Loop (repetition structure)
loop
 what is the output for
the following flow chart?

A) 2, 10

B) 2, 8

C) no output
C++ Program

 The instruction written in C++ is much easier.

 However, a computer cannot execute C++ language instructions directly.

 To run on a computer, these C++ instructions first need to be translated into


machine language.

 A program called a compiler translates instructions written in the C++ high


level language into machine code.
Cont’d
Preprocessor program
Program is created processes the code
according to the (statements that begin The machine
language syntax with the symbol #) language version of
Source Preprocesso Object
the program
Editor Code Complier Code
r
Program is called Compiler checks the source
source code (.cpp) program for syntax errors
and translates it into the
equivalent machine
language.

Linker Loader Execution


Combines the object program Puts the executable Execute each
with other programs in the program in memory instruction
library and creates the 65
executable code (.out)
Cont’d
 You need to be concerned only with step1.

 Some IDEs (integrated development environments) such as visual C++


express do all the other steps.
 These IDEs contain:
o A text editor to create the source program,
o a compiler to check the source program for syntax errors,
o a program to link the object code with the IDE resources,
o and a program to execute the program.

66
C++ Program Structure
 Comments

 Libraries

 main function

 Statements that define memory locations (constants and variables)

 Statements that specify actions to be taken (i.e. flow of controls)

 Operational expressions involving data representations

 Representations of data of different types (e.g. structure)

 Other Functions and procedures


C++ Program Structure
// Comments
..... A Simple C++ Program
.....
// welcome.cpp
// Comments //
// This program prints
// a welcome message
Links to Libraries
#include <iostream>
...... Lib 1
using namespace std;
......

void main()
...... Lib M {
Links to Libraries cout<<“We come to C++
Main Program programming“;
{ }
input & initialisation Expressions, Statements function calls
output & termination
}
Simple Program
#include <iostream> It tells the compiler, during the
preprocessing stage, to find a file, named
iostream, and insert it verbatim into the
using namespace std; source file being compiled.
All symbols in that namespace (functions)
will become visible without adding the
namespace prefix. Otherwise, you have to
void main() add std as a prefix. Ex: std :: cout << “My
{ first C++ program”;

cout << "My first C++ program." << endl;


}
Program Output:
My first C++
program.
69
Basic Elements of C++

 Identifiers and Keywords

 Identifiers are the names of variables, functions, labels, and various other user-defined objects.
Rules of a valid identifier:

 An identifier must begin with an alphabetic character or underscore _

 Alphabetic characters in an identifier can be lowercase or uppercase letters

 An identifier can contain digits, but not as the first character

 An identifier can be of any length

 Keywords are the commands that make up the C++ language. These keywords cannot be used for
identifiers.
Constants and Variables

 Constants are specific values such as 2 , 3.1416 , or -15.


 Variables are memory locations that are assigned a name or identifier.
 Example:
 int x , y=1;
 float mark;
 double total , average;
 char ch = ‘A’;
 Notes:
 We can declare variables anywhere in the main()function.
 We must declare a variable before we use it.
 C++ is case sensitive, i.e. it distinguishes uppercase letters from lowercase
Standard Input and Output

 In order to use input and output statements in our programs, we must include the library:
#include <iostream.h>

 cout STATEMENT
is used to print values and explanatory text to the screen.
cout << “age = ” << age << “ years” << endl;
 if the value of age is 20, the output of the statement is age = 20 years
Cont..
 cin STATEMENT
is used to enter values from the keyboard when a program is executed.
cin >> age;

 cin also allows us to enter multiple input values:


cin >> length >> width;
 Note:
 cin statement is usually preceded by cout statement to describe the information the user should enter
from the keyboard:
cout<< “Enter the length and width:” << endl;
cin >> length >> width;
The output of the above statements is:
Enter the length and width of a rectangle:
15.5 10
Bits and Bytes
 →Bit: One digit, either 0 or 1
 → Byte: any combination of 0 or 1.

You might also like