CH6 Decision (Updated)-mod.pptx
Document Details

Uploaded by IntelligentJasper852
Full Transcript
DECISIONS AND RAMIFICATIONS Topic 6 INTRODUCTION TO COMPUTER PROGRAM A program is a set of instructions that tell a computer what to do. A program is used to make a computer perform a specific task. Desirable characteristics of a program: o Correct:- A program should do what it is supposed to...
DECISIONS AND RAMIFICATIONS Topic 6 INTRODUCTION TO COMPUTER PROGRAM A program is a set of instructions that tell a computer what to do. A program is used to make a computer perform a specific task. Desirable characteristics of a program: o Correct:- A program should do what it is supposed to do o Clear: - The program logic should be easily understood. o Modular: - It should be broken down in sub-routines that interact in clear ways. o Easy to modify HARDWARE AND SOFTWARE Programs can also be called software. Software refers to the computer programs that a computer uses to complete a task. Hardware refers to the physical components that a computer is made of. ▪ A computer is not one device, but a system of devices working in tandem. Each device plays a part. ▪ Major components of the computer: o Central Processing Unit o Main Memory o Secondary Storage Devices o Input Devices o Output Devices SOFTWARE CATEGORIES PROGRAM DEVELOPMENT CYCLE Steps Describe 1 Analyze Define the Problem Make sure that you understand what the program should do. What should the user be able to enter? How? How does the program come up with an answer? What does the program output? How? o User – a person who uses a computer program. o End User – the user that the program was made for. 2 Design Plan a Solution for the Problem 3 Write the Code Implement a solution Develop a PRECISE sequence of steps to solve the problem o An algorithm is a precise sequence of steps to solve a problem The instructions in a programming language collectively called code. Your code should be a translation of your algorithm developed into the programming language. Meaning, you need to be able to logically solve the problem in order to write a program for o it. 4 Testing and Debugging Locate and remove any errors in the program 5 Complete All Documentatio n Organize the material that describes the program. Locate and remove any errors in the program Testing is the process of finding errors in a program Debugging is the process of removing errors in a program. o An error in a program is called a bug. Two kinds of documentation: 1. External Documentation – Material outside of the code files that describe the program. 2. Internal Documentation – Lines inside of a code file that do nothing except describe details of the program. In Java, these are called comments. TYPES OF PROGRAMMING LANGUAGE Programming Language Machine Language Assembly Language High-level languages HLL Definition This is a sequence of instructions written in the form of binary numbers consisting of 1's. 0's to which the computer responds directly. The machine language was initially referred to as code. although now the term code is used more broadly to refer to any program text. Is a type of low-level programming language that is intended to communicate directly with a computer’s hardware. Unlike machine language, which consists of binary and hexadecimal characters, assembly languages are designed to be readable by humans. HLL allow us to write computer code using instructions resembling everyday spoken language (for example: print, if, while) which are then translated into machine language to be executed. Programs written in a high-level language need to be translated into machine language before they can be executed. Some programming languages use a compiler to perform this translation and others use an interpreter. TYPES OF PROGRAMMING LANGUAGE The Compiler and Interpreter have different approaches to translation Compiler Interpreter a compiler is a computer program that translates source code from a high-level programming language to a lower level language (e.g. assembly language, object code, or machine code) to create an executable program An interpreter is a computer program that is used to directly execute program instructions written using one of the highlevel programming languages Scans the entire program first and then translates it into Translates the program line by line machine code. Converts the entire program to machine code;, when all the Each time the program is executed every line is checked for syntax errors are removed execution takes place. syntax error and then converted to equivalent machine code Slow for debugging Good for fast debugging Execution time is less Execution time is more. ALGORITHMS An algorithm is a sequence of steps to solve a particular problem or algorithm is an ordered set of unambiguous steps that produces a result and terminates in a finite time. ALGORITHMS TYPES OF CONTROL STRUCTURE The algorithm includes following three types of control structures. 1. Sequence: In the sequence structure, statements are placed one after the other and the execution takes place starting from up to down. 2. Branching (Selection): In branch control, there is a condition and according to a condition, a decision of either TRUE or FALSE is achieved. In the case of TRUE, one of the two branches is explored; but in the case of FALSE condition, the other alternative is taken. Generally, the ‘IF-THEN’ is used to represent branch control. 3. Loop (Iteration): The Loop or Repetition allows a statement(s) to be executed repeatedly based on certain loop condition e.g. WHILE, FOR loops. HOW TO WRITE ALGORITHM ALGORITHM EXAMPLES Problem 1 : Find the area of a Circle of radius r. Problem 2: Write an algorithm to read two numbers and find their sum. Inputs to the algorithm: Radius r of the Circle. Inputs to the algorithm: First num1. Second num2. Expected output: Area of the Circle Algorithm: Expected output: Sum of the two number Solution Solution : Step1: Start Step1: Start Step2: Read\input the first num1. Step2: Read\input the Radius r of the Circle Step3: Read\input the second num2. Step3: Area PI*r*r // calculation of area Step4: Sum num1+num2 // calculation of sum Step4: Print Area Step5: Print Sum Step5: End Step6: End FLOWCHARTS The first design of flowchart goes back to 1945 which was designed by John Von Neumann. Unlike an algorithm, Flowchart uses different symbols to design a solution to a problem. Advantages of flowchart: Communication: Flowchart is an excellent way of communicating the logic of a program. Effective Analysis: Easy and efficient to analyze problem using flowchart. Efficient Coding: is easy to convert the flowchart into any programming language code Proper documentation: Program flowcharts serve as a good program documentation, which is needed for various purposes. Proper Debugging: The flowchart helps in debugging process. Flowchart Symbols FLOWCHART EXAMPLES Start Problem 1: Draw a Flowchart to find the sum of two numbers Declare variables num1, num2 and sum Read num1, num2 Algorithm Step1: Start Step2: Declare SUM,num1,num2 sum = num1 + num2 Step3: Input numbers num1,num2 Step4: SUM = num1+num2 Step5: Display SUM Display sum Step6: End End FLOWCHART EXAMPLES Start Problem 2: Draw a Flowchart to convert temperature from Fahrenheit to Celsius Declare variables C,F Read F Algorithm Step1: Start Step2: Declare C,F C = 5.0/9.0 *(F – 32) Step3: Input temperature in Fahrenheit say F Step4: C = 5.0/9.0* (F - 32 ) Step5: Display C Display C Step6: End End ADVANCED FLOWCHART EXAMPLES Problem 1: Draw a flowchart for a program that prints the type of number entered by the user (odd or even) Start Declare number Read number Algorithm Step1: Start No Step2: Declare number Step3 Input the number Step4: If number%2==0 o Display even Step5: If number%20 o Display odd Step6: End (why?) If number %2=0 yes Print “Even” Print “Odd” (why?) End ADVANCED FLOWCHART EXAMPLES Problem :Draw a flowchart for a program that find the largest value of any two numbers Start Read Input num1,num2 Algorithm Step1: Start No ▪ Step2: Declare num1, num2, Max Step3: Read num1, num2 If num1>= num2 Max=num2 Max=num1 Step4: If (num1 >= num2) then o Max← num1 Step5: If (num2 >= num1) then o Max← num2 Step6: Display Max Step7: End yes Print Max End ADVANCED FLOWCHART EXAMPLES Problem: Draw a flowchart for a program that prints odd numbers from 9 to 100. Start Declare counter counter = 9 Algorithm Step1: Start Print counter Step2: Declare variable counter counter = counter + 2 Step3: counter ←9 (starting value Assignment) Step4: if counter