Computer Programming Lecture1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Assistant lecturer

Safa Hussain
What is the computer:
0The computer is a device that operated upon information or data. It is an
electronic device which accepts inputs data, stores the data, does arithmetic
and logic operations and provides the outputs in the desired format. The
computer receives data, process it, produces output and stores it for further
references. So, a computer should have at least four major components to
perform these tasks. A block diagram of the basic computer organization has
the following functional units.

Four logical units in every computer:


1.Input unit
•Obtains information from input devices (keyboard, mouse)
2.Output unit
•Outputs information (to screen, to printer, to control other devices)
3.Memory unit
•RAM
–Quick access, low capacity, stores input information
•Secondary storage unit
–Cheap, long-term, high-capacity storage
–Stores inactive programs
4 .Central processing unit (CPU)
–Supervise and coordinate the other sections of the computer
•Arithmetic logic unit (ALU)
–Performs arithmetic calculations and logic decisions
•Control Unit (CU)
– The control unit is like a traffic warden directing and controlling the
flow of data

Lec. MSc Safa Hussain


1
COMPUTER SYSTEM
Is a collection of entities(hardware,software ) that are designed to receive,
process, manage and present information in a meaningful format.

COMPONENTS OF COMPUTER SYSTEM

 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 - sytem software and
application software

Lec. MSc Safa Hussain


2
Programming:

A set of commands that are given to the computer to perform a specific work
in a way that the computer understands. For example, give the computer a
command to print a sentence or run a calculation in a way that the computer
calculates.

How can I communicate with the computer?

In a computer-understood language that is electrical signals (1-0).

How can I communicate with the computer and the


programmer?

Through a set of programming languages.

Programming languages:

A programming language is a set of rules that provides a way of telling a


computer what operations to perform. A programming language is a set of
rules for communicating an algorithm. It provides a linguistic framework for
describing computations.

In other term:

Is a set of tools that provide written commands in a language understood by


the programmer and converted to the machine language using a set of tools
and programs.

Three types of programming languages :

 Machine languages
machine language is the binary language made of 1s and 0s is
executed directly by a computer. It is difficult, dependent on the type of
machine. Difficult to correct the errors

 Assembly languages

To solve the above problems An Assembly language is design . It is a


little easier than machine language, It uses symbols, and abbreviations
to describe the instruction for example add, sub, jump. An Assembly
language is called symbolic machine code.

Lec. MSc Safa Hussain


3
Assembly language is converted into executable machine code by a
utility program referred to as an assembler. The conversion process is
referred to as assembly, or assembling the source code.

 High-level languages
high-level language is a programming language such as C, FORTRAN,
or Pascal that enables a programmer to write programs that are more
or less independent of a particular type of computer. Such languages
are considered high-level because they are closer to human languages
and further from machine languages.

Lec. MSc Safa Hussain


4
What is c++?
C++ (1983)( by Bjarne Stroustrup).

C++ is a general-purpose object-oriented programming (OOP) language,


developed by Bjarne Stroustrup, and is an extension of the C language. It is
therefore possible to code C++ in a "C style" or "object-oriented style." In
certain scenarios, it can be coded in either way and is thus an effective
example of a hybrid language.

C++ is considered to be an intermediate-level language, as it encapsulates


both high- and low-level language features. Initially, the language was called
"C with classes" as it had all the properties of the C language with an
additional concept of "classes." However, it was renamed C++ in 1983.
1. It supports all features of both structured programming and OOP.
2. C++ focuses on function and class templates for handling data
Types.

C++ Program Development Process (PDP):


C++ programs typically go through six phases before they can be
executed. These phases are:
1. Edit: The programmer types a C++ source program, and makes
correction, if necessary. Then file is stored in disk with extension (.cpp).
2. Pre-Processor: Pre-processing is accomplished by the pre-proceccor
before compilation, which includes some substitution of files and other
directories to be include with the source file.
3. Compilation: Converting the source program into object-code.
4. Linking: A linker combines the original code with library functions to
produce an executable code.
5. Loading: The loader loads the program from the disk into memory.
6. CPU: Executes the program, residing in memory.

Lec. MSc Safa Hussain


5
These steps are introduced in the figure below:

Algorithm:
algorithm can be defined as a finite sequence of effect statements to solve a
problem. An effective statement is a clear, unambiguous instruction that can
be carried out.
Algorithm properties:
•Finiteness : the algorithm must terminate a finite number of steps.
• Non-ambiguity: each step must be precisely defined. At the completion of
each step the nest step should be uniquely determined .
•Effectiveness: the algorithm should solve the problem in a reasonable
amount of time.

Ex1:Write algorithm to find maximum number from two


numbers.
1.start
2. input two numbers(num1 and num2)
3.if num1 greater than num2 print num1
4.if num2 greater than num1 print num2
5. end

Lec. MSc Safa Hussain


6
Ex2: Write algorithm to sum two numbers
1.Start
2.Input first number
3.Input second number
4.Calculate the sum
5.Print the result
6.end

Ex3 Write algorithm to find the division two number

1.Start
2.Input first number
3.Input second number
4.If second number <>0 Goto 6
5.If second number=0 Goto 9
6.Calculate division
7.Print the result
8.Goto 10
9.Print error
10.end

Lec. MSc Safa Hussain


7
Ex 4:Write algorithm o find the sum of negative numbers
among 50 numbers.

1.Start
2.Initialize two variables (count and sum), set count to 0 ,sum to 0
3.Input number.
4. increment the counter of input numbers
5.If the number is negative go to 7
6.If the number is positive go to 8
7.Add the number to the sum.
8.if the counter less than 50 go to 3
9. print sum
10. End

A flowchart

is a graphical representation of an algorithm or of a portion of an algorithm


.Flowcharts are drawn using symbols. The main symbols used to draw a
flowchart are shown in following figure .

Lec. MSc Safa Hussain


8
Ex1:Draw the flowchart of maximum number between two
numbers?

Lec. MSc Safa Hussain


9
Ex2: draw the flowchart of sum two numbers?

Lec. MSc Safa Hussain


10
Ex3: draw the flowchart of the division of two numbers?

Lec. MSc Safa Hussain


11
Homework:

1)Draw a flowchart to read 3 numbers: x , y and z and print the largest


number of them.

2)Write an algorithm that finds the average of three degrees. If the


avarage is successful, printed word "success" ,opposite of that printed
word"fail"?

Lec. MSc Safa Hussain


12

You might also like