Computer Programming
Jimma University,
Jimma Institute of Technology
Course Name: Computer Programming
Course Number: ECEg 1052
Instructor: Dawod Y.
Email: dawud.abegaz@ju.edu.et
Course Description
•
This course is designed to introduce students to problem
solving techniques using computers.
•
The course covers topics like problem solving using computers,
algorithm development and representation, programming languages
and paradigms, program structure, basic programming elements of C++
(identifiers, constants, variables, reserved words, basic data types,
expressions, statements (assignment, I/O, jumping, flow-control), arrays,
strings pointers, and dynamic memory management.
Objectives
To introduce students to computer based problem solving.
To enable students to design, develop, compile and debug programs in a
high level programming language.
To enable students to develop programs to solve numerical engineering
problems.
Competencies
Students
shall
be
familiar with computer and
programming language and shall be able to plan, analyze and write
computer programs for basic engineering problems.
Course Outcomes
Upon completion of the course, the students will be able to:
1. Write, compile and debug programs in C language.
2. Use different data types in a computer program.
3. Design programs involving decision structures, loops, arrays and
functions.
4. Identify the difference between call by value and call by reference
5. Use pointers to understand the dynamics of memory
6. Create and perform different file operations
Reference
Glassborow, F. (2004), A Beginners Introduction to Computer
Programming, Wiley
Assessment Techniques:
Continuous Assessment (50%)
Final Examination (50%)
Tests Mini projects Assignments Class Attendance Final Exam
15%
15%
10%
10%
(50%)
Course Contents
Chapter 1: Introduction
1. Introduction to programming
2. Problem solving techniques
• Flowchart
• Pseudo code
• Algorithms
Course Contents
Chapter 2: Basics of programming
1. Structure of a Program
2.
C++ IDE
3.
Showing Sample program
4.
Keywords, Identifiers, Inputs, Outputs, Comments, Parts of a program
5.
Data Types, Variables, Constants
6.
Operators
• Assignment Operators, Compound Assignment Operators
• Arithmetic Operators and Relational Operators
• Increment and Decrement Operators
• Infix and postfix types
• Precedence of Operators
Course Contents
Chapter 3: Control Statements
1. If…. else statement
2. If… else if… else statement
3. Switch statement
4. For loop statement
5. While loop statement
6. Do…while statement
Course Contents
Chapter 4: Function and Passing argument to function
1.
Definition of function
2.
Declaration of function
3.
Passing value of a function by Value and Passing value of a
function by reference
Chapter 5: Arrays, Pointers & Strings
1.
One-dimensional
2.
Multi-dimensional array
3.
Address and pointer
4.
Pointer and array
5.
Pointer and function
6.
Pointer and string
Course Contents
Chapter 6: Structure
1. Specifying simple structure
2. Defining a structure variable
3. Accessing structure variable
Chapter 7: File
1. File and file management
CHAPTER-1
INTRODUCTION TO PROGRAMMING
11
Getting Started….
1.
What is computer?
2.
How does a computer work/ operate?
3.
What is programming?
• A basic understanding of what programming is and its purpose.
4.
What are the different programming paradigms?
• Explore paradigms like procedural, object-oriented, functional, and
declarative.
5.
What is a programming language?
• Understand the concept of a programming language and its syntax,
semantics, and libraries.
12
INTRODUCTION
• What is a computer?
• A Computer is an electronic device that accepts data, performs
computations, and
makes
logical
decisions
according
to
instructions that have been given to it; then produces meaningful
information in a form that is useful to the user.
Inputs
Process
Outputs
13
WHAT IS A PROGRAM?
• Computer programs, software programs, or just programs
are the instructions that tells the computer what to do.
• A program is a set of instructions, written to perform a specific
task by the computer.
• Computer programming (programming or coding) is the
process of creating a sequence of instructions using different
programming languages.
14
PROGRAMMING LANGUAGE
• A programming language is an artificial language that can be used to
control the behavior of a machine, particularly a computer.
• Programming languages, like natural language, are defined by syntactic
and semantic rules which describe their structure and meaning
respectively.
• The syntax of a language describes the possible combinations of
symbols that form a syntactically correct program.
• The meaning given to a combination of symbols is handled by
semantics.
15
CONT…
• A main purpose of programming languages is to provide
instructions to a computer.
• Available programming languages come in a variety of forms and
types.
• Programming languages can be divided in to two major categories:
• Low-level languages
• High-level languages
16
LOW-LEVEL LANGUAGES
The term low level highlights the fact that it is closer to a
language which the machine understands.
Machine language
This is the language (in the form of 0’s and 1’s, called binary
numbers) understood directly by the computer.
It is machine dependent.
It is difficult to learn and even more difficult to write programs
For example:
01000001 00111101 00111001
10011001 000000000010 000000000011
17
LOW-LEVEL LANGUAGE
Assembly language
Correspondences symbolic instructions and executable machine
codes and was created to use letters (called mnemonics) to each
machine language instructions to make it easier to remember or
write.
For example:
ADD A, B – adds two numbers in memory location A and B
The assembly language must be translated to machine code by a
separate program called assembler.
18
HIGH-LEVEL LANGUAGES
• Low level language requires extensive knowledge of the hardware since it is
machine dependent. To overcome this limitation, high level language has been
evolved .
• Use naturally understandable languages
• IDE’s are used for rapid development like C++, .Net environment
• Syntax, rules, and mnemonics are closer to "natural language“.
• High-level languages are more English-like and, therefore, make it easier for
programmers to "think" in the programming language.
• E.g., FORTRAN (FORmula TRANslator), BASIC (Beginners All Purpose
Symbolic Instruction Code), PASCAL, C, C++, Java.
19
CONT…
• High-level languages also require translation to machine language before
execution.
• This translation is accomplished by either a compiler or an interpreter.
• Compilers translate the entire source code program before execution.
• Interpreters translate source code programs one line at a time. Interpreters
are more interactive than compilers.
20
PROGRAMMING PARADIGMS
• A paradigm is the preferred approach to programming
that a language supports.
• Imperative : Programming with an explicit sequence of commands that
update state.
• Declarative : Programming by specifying the result you want, not how to
get it.
• Structured: Programming with clean, goto-free, nested control structures.
• Procedural : Imperative programming with procedure calls.
21
• Today, we'll demystify three of the most popular paradigms: Object-Oriented
Programming (OOP), Functional Programming (FP), and Procedural Programming.
22
PROBLEM SOLVING TECHNIQUES
Before a program is written, the programmer must clearly
understand what data are to be used, the desired result, and the
procedure to be used to produce the result.
The procedure, or solution, selected is referred to as an
algorithm.
An algorithm is defined as a step-by-step sequence of
instructions that must terminate and describe how the data is to
be processed to produce the desired outputs.
23
ALGORITHMS
• An algorithm is a set of well-defined instructions to solve a particular problem.
• An algorithm must satisfy the following requirements:
• Unambiguousness: Every step in an algorithm must be clear as to what it is
supposed to do and how many times it is expected to be executed.
• Generality: it should have to be general, not to be specific.
• Correctness: it must be correct and must solve the problem for which it is
designed.
• Finiteness: it must execute its steps and terminate in finite time.
• Algorithms are usually represented in pseudo code or by a flow chart.
24
PSEUDOCODES
• Pseudocode is an artificial and informal language that helps
programmers develop algorithms
• Pseudocode is a generic way of describing an algorithm without use
of any specific programming language syntax.
• The purpose of using pseudocode is that it may be easier for humans
to read than conventional programming languages
• No standard for pseudocode syntax exists, as a program in
pseudocode is not an executable program.
25
CONT…
Example1:
• Original Program Specification:
• Write a program that obtains two integer numbers from the user. It will print out
the sum of those numbers.
Pseudo code:
BEGIN
NUMBER num1, num2, sum
OUTPUT("Input number1:")
INPUT num1
OUTPUT("Input number2:")
INPUT num2
sum= num1 + num2
OUTPUT sum
END
Step 1: Start
Step 2: Declare variables num1, num2, and sum.
Step 3: Read values for num1, num2.
Step 4: Calculate sum = num1 + num2
Step 5: Display sum
Step 6: Stop
26
CONT…
Example 2: Write an algorithm to determine a student’s final grade and
indicate whether it is passing or failing. The final grade is calculated as the
average of four marks.
Pseudo Code
input a set of 4 marks
Calculate their average by summing and dividing by 4
if average is below 50
Print “FAIL”
else
Print “PASS”
Step 1: Input M1,M2,M3,M4
Step 2: GRADE =(M1+M2+M3+M4)/4
Step 3: if (GRADE < 50) then
Print “FAIL”
else
Print “PASS”
End if
27
FLOWCHARTS
• Flowchart is a graphical or symbolic representation of an algorithm
• The advantage of flowchart is it doesn’t depend on any particular
programming language, so that it can used, to translate an
algorithm to more than one programming language.
• Flowchart uses different symbols (geometrical shapes) to represent
different processes.
28
CONT…
29
Algorithms and Flowcharts
30
EXAMPLES -
31
CONT…
Example 1:
• Draw flow chart of an algorithm to add two numbers and display
their result.
• Algorithm description
• Read two numbers (A and B)
• Add A and B
• Assign the sum of A and B to C
• Display the result ( c)
32
CONT…
33
CONT…
Example 2
• Write an pseudo code and draw a flow chart to check a
number is negative or not.
Algorithm description.
1.
2.
3.
Start
Read a number x
If x is less than zero then
Write “Negative”
Else
Write “Not Negative”
4. stop
34
CONT…
Exercise:
1.Write an algorithm description and draw a flow chart to check a
number is even or odd.
35
CONT…
Exercise:
2.Write an algorithm description and draw a flow chart to check greatest
of two numbers
Step 1: Start
Step 2: get a, b value
Step 3: check if(a>b) print a is
greater
Step 4: else b is greater
Step 5: Stop
36
CONT…
Exercise:
3.Write an algorithm description and draw a flow chart to check greatest
of three numbers
Step1: Start
Step2: Get A, B, C
Step3: if(A>B) goto Step4 else goto
step5
Step4: If(A>C) print A else print C
Step5: If(B>C) print B else print C
Step6: Stop
37
Pseudo code Vs. Flowchart
• Pseudocode - is a textual description of an algorithm.
• Flowcharts - are visual representations of an
algorithm.
38
EXERCISE
1. Draw flowchart that calculate the Sum of the First 50
Numbers.
2. Draw a Flowchart to print the Even numbers between 9 and
100.
39