Download presentation
Presentation is loading. Please wait.
1
Programming Fundamentals
Chapter 2 To be discussed in this chapter: Programs and Programming Languages The programming process (Complie Link and Run process) Source code , Object code, and Executable code The integrated development environment (IDE) Program Design Algorithms Flowcharting (Starting out with C++, appendix O) A brief look at the program development cycle
2
Overview of Programming
2.1 Overview of Programming
3
The Basics Program: a list of instructions that tells the computer how to solve a problem or perform a task. Programming: the act of defining these instructions. Programmer: the person doing the defining. User: the person who ultimately makes use of the program. (Examples??) These are just basic definitions to get started. Note: an alternate definition of Program also includes data (fixed data) together with the instructions Example– Students Teachers and Computers We learn to compute at a young age. At first we just add and subtract numbers. One plus one equals two. Five minus two is three. As we grow older we learn about additional mathematical operations, like exponentiation and sine, but we also learn to describe rules of computation. Given a circle of radius r, its circumference is r times two times pi. The truth is, our teachers turn us into computers and program us to execute simple computer programs. Computer programs are just very fast students, and we are their teachers
4
Remember Computers are digital devices that simply do what they are told. We must speak their language to tell them what to do. Does this mean, we should speak binary? ( 0’s and 1’s)?? ? ? Computers don’t understand English, Amharic, etc. The person teaching computers must speak a computer language. Instructions and data are all placed in the main memory Option A Place the 0’s and 1’s directly in memory ourselves => Extremely difficult This is programming at the machine language level Option B Represent meaningful groups of 1’s and 0’s with words and use that instead => Still difficult This is programming at the assembly language level Example: ADD A, B, C => ( ) Machine language and assembly language are general referred to as Low Level Programming Languages Option C Use something that looks like English but is structured and logical enough to be converted into machine language by another program This is programming at the High Level Programming Languages level Example: A = B+C; (C++ is a High Level Language) Option D Use something very much like English and let the computer do the hard work of translation. This is programming at the Very High Level Programming Languages level Examples: SELECT telephone FROM users WHERE name LIKE “abebe” SQL, Prolog, etc… Very Domain Specific
5
Programming Languages
Very High Level Languages (SQL, Prolog, …) High Level Languages (C++) As we go down, programming becomes more difficult In this course, we will speak to the computer in C++. It is a good compromise between a human language and what the computer understands Low Level Languages (Assembly, Machine Code)
6
Features of High Level Languages
Designed to make programming easier Emphasis on logic Aids in the design process Independence (relatively) from the Operating System and Hardware One HLL instruction gets translated to multiple machine level instructions * Programming Easier – it is to think in C++ than in assembly or machine level * Emphasis on logic – the programmer focuses more on how the logic of the program should be rather than on how it is to be placed in memory (and more) * Aids in the design process – it is much efficient to put down our ideas interms of C++: the language itself helps us in designing our programs * OS and HW independence – the programs we write with C++ can be made to work on any OS and any HW. (atleast theoretically). But those written in low level languages are specific to a certain OS AND HW combination. * To accomplish a simple task with a low level language requires us to write multiple instructions whereas with C++, we can write a single instruction for a task and it will be translated accordingly.
7
The Programming Process
Machine Code #include Int main( { … Compile LINK Object Code Source Code RUN Libraries, Other Object Code How exactly do we program? 1. Using a text editor, write the program 2. Save it as a file (.cpp extension) 3. Fix any errors that you might have Compile your program into object code. If there are errors, go back to step 3 Run the linker to generate executable code. If there are errors, go back to step 3 6. Run (execute) your program and test it. If there are errors, go back to step 3. Btw, this is an algorithm for programming with C++. Algorithms will be discussed later. Source Code: The instructions written in C++. This is the only part that we can understand. Compiler: Translates the source code into some intermediate form Linker: links our object code with other object codes (libraries) to create our executable program. Run: An executable program, once converted into machine code, is ready to run on our computer.
8
IDE IDE stands for Integrated Development Environment
Includes the compiler and the linker Has tools to assist the programmer Automates and simplifies the programming process For this course, we will be using the visual studio IDE and Eclipse.
9
2.2 Program Design
10
The Basics PROGRAMS = ALGORITHMS + Data Structures Niklaus Wirth Algorithm?? An effective procedure for solving a problem in a finite number of steps. The proper pronunciation of Niklaus Wirth is (Knee Close Veert) Programs = Algorithms + Data Structures is the title of his book. He is one of the most influential Computer Scientists of our time – he invented the Pascal programming language.
11
Program Development Algorithm
Using a text editor, write the program Save it as a file (.cpp extension) Fix any errors that you might have Compile your program into object code. If compilation is successful, continue to step 6. If it fails, go back to step 3. Run the linker to generate executable code. If linking is successful, continue to step 8. Otherwise, go back to step 3. Run (execute) your program and test it. If there are errors, go back to step 3. Otherwise finish. This is an algorithm for developing programs. It is the 9 step solution to the problem of writing a program! You can also define algorithms as A well defined computational procedure that takes some value or set of values as input and produces some value or set of values as output. The algorithm is thus a sequence of computational steps that transform the input into the output. Exercise Making tea Coming to school Etc…
12
The Blueprint You can think of an algorithm as the blueprint (plan) of your program. Before you begin writing a program, you first have to understand the problem and then develop a step-by-step solution (the algorithm). Developing an algorithm to solve a problem is usually more difficult than writing the code. Emphasize on the importance of an algorithm. It is the lifeline of the program, etc. Trying to write a program without an algorithm is like trying to build a house without a plan, and so forth…
13
Characteristics of Algorithms
An algorithm for a computer program must Have precisely defined steps Not be ambiguous Terminate after a finite number of steps An algorithm exhibits Sequence (Process) Decision (Selection) Repetition (Iteration/Looping) The first three characteristics are crucial in computer programs. If an algorithm doesn’t fulfill them, then it can’t be implemented with a computer.
14
Sequence Each task follows the previous one without altering the order of the tasks. Each task is executed once.
15
Decision Allows only one of two tasks to be carried out, depending on a controlling logical condition. The selected task is executed only once. If…then…, If… then… else… If today is Sunday then don’t go to school If today is Saturday or today is Sunday then don’t go to school, else go to school The outcome of a decision is either true or false. The underlined parts are the conditions, the blue parts are the actions.
16
Repetition Enables a task to be repeated until a controlling condition no longer holds. Keep going to school until you graduate. Repeat this example until the students understand and go to the next example when they do. What if it loops infinitely??? The underlined parts are the conditions The blue parts are the actions The bold part is the word that signals repetition You can talk about infinite loops Also try Repeat this example Until (students understand) Go to next example For a cashier While (there is a line of customers) call next customer serve customer Rest
17
More Concepts – Input/Output
Your algorithm usually needs a set of inputs from the user and may give outputs to the user. Examples The movie PG algorithm 1. Ask the user for their age {output} 2. Get the user’s age {Input} 3. If the user’s age is less than 18, then go to step 5, else go to step 4 {decision} 4. Watch movie 5. Exit cinema My Algorithm 1. Ask the students a question {output} 2. Get an answer {input} 3. If the answer is right, go to step 4, else go back to step 1. {steps 1, 2, and 3 make up a repetition} 4. Proceed to next topic
18
More Concepts - Variables
Variables are a way of representing data in your programs. You can think of them as containers for a value. Examples For the movie PG algorithm 1… 2. Get a { a is now a variable} 3. If a<18, then … {here a represents the value obtained in step 2} Do the following exercise with the students. Tell them they can’t use paper and pen 1. Add 5 and 6 and remember the result 2. Subtract 4 from 7 and remember the result 3. Multiply result 1 with 6 and remember the result 4. Divide result 3 by result 2 and tell me the value If you were allowed to use paper and pen, you might have used variables like this 1. A = = 11 2. B = 7 – 4 = 3 3. C = A*6 = 11*6 = 66 4. Final Answer = C/B = 66/3 = 22
19
Expressing Algorithms
Plain English (the step-by-step way) Pseudocode (False code) Uses much restricted vocabulary Closer to a Programming Language Uses variables and symbols There are different types available Flowchart Graphical method Uses various symbols for various actions Very easy to draw Very easy to read and understand We have been using Plain English so far when talking about algorithms. That is straight forward and needs no explanation. We won’t go into pseudocode in this course because an understanding of a programming language is somehow necessary to use pseudocode. (Most books use Pascal as a pseudocode) Next we will be discussing flowcharts.
20
Flowchart Basics A flowchart represents an algorithm or process, showing the steps as various symbols and showing their order by connecting them with arrows. The basic symbols are used in flowcharts are discussed here There are a lot of symbols for flowchart but here we will see only the basic ones
21
Terminal Points Rounded rectangles, or terminal points, indicate the starting and ending points of the flowchart. This is their only purpose, starting and ending flowcharts (processes). The actual work gets done inside this
22
Input/Output Operations
Parallelograms designate input or output operations.
23
Processing A rectangle depicts a process such as a mathematical computation or a variable assignment.
24
Connectors A connector symbol, which is a circle, is used to break flowchart into multiple parts. This is useful if the flowchart is bound to take more than one page.
25
Example Flowcharts Write the algorithms in plain English
26
The Decision Structure
With decisions, the path to follow is determined by the outcome of a test. The diamond symbol is used to represent the test to be performed. In a decision structure the next action to be performed depends on the outcome of a true/false test. A statement that is either true or false is evaluated. In the simplest form of decision structure, if the result of the test is true, a set of instructions is executed and if the result of the test is false, these instructions are skipped
27
Exercises Prime or Composite Average of three numbers Even or Odd
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.