Fundamental Of Structured Programming Lecture 1 Benha University PDF

Summary

This document is a lecture on fundamental structured programming concepts and algorithms. The lecture was delivered by Dr. Naglaa Fathy at Benha University.

Full Transcript

Fundamental Of Structured Programming Lecture 1 By Dr. Naglaa Fathy Lecturer, Computer Science Department, Faculty of computers and artificial intelligence, benha university Course Content The course presents the fun...

Fundamental Of Structured Programming Lecture 1 By Dr. Naglaa Fathy Lecturer, Computer Science Department, Faculty of computers and artificial intelligence, benha university Course Content The course presents the fundamental concepts of structured programming and algorithmic problem solving: primitive data types, control structures, functions and parameter passing, top-down design, arrays, files, and the mechanics of compiling, running, testing, and debugging programs. The Assessment Final Term Examination 50% quiz _1 5% Quiz_2 5% Midterm Exam 15% Attendance 5% Sections 20% ‫نظام التقويم‬ Lectures References Starting out with C++ From Control Structures through Objects TONY GADDIS Ninth Edition. Course Materials All course material will be made available on Fundamental of Structured Programming Google Drive Folder https://drive.google.com/drive/folders/1tPM- 3Xj4whtwlWia_tA0GaZVx_FTvJoC?usp=sharing Chapter 1 Introduction to Computers and Programming Why Program? Computer – programmable machine designed to follow instructions. Computers can do many different jobs because they are programmable. Programs are commonly referred to as software – instructions in computer memory to make it do something Programmer – person who writes instructions (programs) to make computer perform a task SO, without programmers, no programs; without programs, a computer cannot do anything Computer Systems: All computer systems consist of similar hardware devices and software components. Hardware and Software Main Hardware Component Categories: Main Hardware Component Categories Hardware refers to the physical components of which a computer is made. Hardware Component Categories : ▪ Central Processing Unit (CPU) ▪ Main Memory ▪ Secondary Memory / Storage ▪ Input Devices ▪ Output Devices Central Processing Unit (CPU) Comprised of: Control Unit Retrieves and decodes program instructions Coordinates activities of all other parts of computer Arithmetic & Logic Unit Hardware optimized for high-speed numeric calculation Hardware designed for true/false, yes/no decisions Main Memory It is volatile. Main memory is erased when the program terminates or the computer is turned off Also called Random Access Memory (RAM) A computer's memory is divided into tiny storage locations known as bytes. One byte is enough memory to store only a letter of the alphabet or a small number. Each byte is divided into eight smaller storage locations known as bits bit: smallest piece of memory. Has values 0 (off, false) or 1 (on, true) Main Memory Bytes have addresses Addresses – Each byte in memory is identified by a unique number known as an address. The number 149 is stored in the byte with the address 16, and the number 72 is stored at address 23. Secondary Storage Secondary storage is a type of memory that can hold data for long periods of time, even when there is no power to the computer. Non-volatile: data retained when the program is not running or the computer is turned off Comes in a variety of media: magnetic: traditional hard drives solid-state: optical: CD-ROM, DVD Flash drives Input Devices Devices that send information to the computer from outside Many devices can provide input: Keyboard, mouse, touchscreen, scanner, digital camera, microphone Disk drives, CD drives, and DVD drives Output Devices Output is any information the computer sends to the outside world. Many devices can provide output: screens, printers, and speakers. Software There are two general categories of software: system software and application software. The programs that control and manage the basic operations of a computer are generally referred to as system software. Programs that make a computer useful for everyday tasks are known as application software. Programs and Programming Languages A program is a set of instructions a computer follows in order to perform a task. A computer program is a set of instructions that tells the computer how to solve a problem or perform a task. these instructions are called an algorithm. A programming language is a special language used to write computer programs. programming languages. Types of languages: Low-level: used for communication with computer hardware directly. Often written in binary machine code (0’s/1’s) directly. High-level: closer to human language Machine Language Although the previous algorithm defines the steps for calculating the gross pay, it is not ready to be executed on the computer. The computer only executes machine language instructions Machine language instructions are binary numbers, such as 1011010000000101 Rather than writing programs in machine language, programmers use programming languages. Some Well-Known Programming Languages C++ BASIC Ruby FORTRAN Java Visual Basic COBOL C# JavaScript C Python Algorithms and flow chart Algorithms and flow chart Algorithms and flowcharts are used for the representation of a program in a different manner. The algorithm and flowchart are two types of tools to explain the process of a program. A typical programming task can be divided into two phases: Problem-solving phase produce an ordered sequence of steps that describe solution of a problem this sequence of steps is called an algorithm Implementation phase implement the program in some programming language Algorithm An algorithm is a set of well-defined steps for performing a task or solving a problem. Notice these steps are sequentially ordered. Step 1 should be performed before Step 2, and so forth. It is important that these instructions be performed in their proper sequence. Algorithm An algorithm is a step-by-step procedure, which has to be followed when an input is given to obtain the desired output or result. An algorithm is a procedure for solving problems. In order to solve a mathematical or computer problem, this is the first step in the process. An algorithm includes calculations, reasoning, and data processing. Algorithms can be presented by natural languages, pseudocode, and flowcharts, etc. Example Algorithm for sum of two numbers Step 1: Start Step 2: Enter two numbers in a and b Step 3: Add a and b and store in c Step 4: Display c Step 5: End Example Algorithm for Calculating Gross Pay Pseudocode Pseudocode is an artificial and informal language that helps programmers develop algorithms. Pseudocode is very similar to everyday English Example 1: 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. Pseudocode: 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” Flowchart a flowchart is a graphical representation of the steps to be followed of a program takes to obtain the desired output. The flowchart uses many geometric shapes to indicate different steps. Each geometric shape represents one different step. The main purpose of using a flowchart is to analyze different methods. Several standard symbols are applied in a flowchart Flowchart shows logic of an algorithm emphasizes individual steps and their interconnections e.g. control flow from one action to the next A flowchart is a diagram made up of Boxes, Diamonds and other Shapes, connected by Arrows. Each shape represents a step in the process, and the arrows show the order in which they occur. Standard Symbols used in Flowcharts The Terminal Symbol The terminal symbol indicates the beginning and the end of processing. It contains the words START, STOP, or END. It is represented by rounded rectangles. The Input / Output Symbol The input/output symbol (or I/O symbol) indicates that the computer is to obtain new data or to record the results of computations. This symbol often contains directions to read records or to print report lines. It represented by parallelograms. The process symbol The process symbol contains instructions that are not represented by other symbols. Process symbols usually contain directions for calculations or for the storage of data. It represented by rectangles. The Decision Symbol The decision symbol represents a branch in the flow of a program; it must contain directions that indicate which branch to follow. It represented by Diamond. This symbol usually contains a question, and each of its branches is labeled with an answer. Flow direction lines Flow direction lines indicate the flow of the program. Comments The comment or annotation symbol is used to annotate other entries. The dashed line indicates the position of the comment. The connector Symbol The connector is used to join areas of a flowchart, in the other word allows you to connect two flowchart segments. A connector represented by a Small Circle. The connector is used only when points to be joined occur on the same page. The off-page Connector Symbol The off-page connector is used to join areas of a flowchart when the points joined occur on different pages. The predefined process symbol or Model symbol is used to represent a predefined process when the flowcharts that define the process are not included with the current set of flowcharts. START Read Input. Call calc_pay Example: function. Display results. END Four Flowchart Structures Sequence Decision Repetition Case Sequence Structure A series of actions are performed in sequence Decision Structure One of two possible actions is taken, depending on a condition Repetition Structure A repetition structure represents part of the program that repeats. This type of structure is commonly known as a loop. Repetition Structure Pre-Test Post-Test 45 Repetition Structure In Pre-Test Repetition Structure The condition is tested BEFORE any actions are performed. YES x < y? Display x Add1 to x In Post-Test Repetition Structure The condition is tested AFTER the actions are performed Display x Add 1 to x YES x < y? Case Structure One of several possible actions is taken, depending on the contents of a variable. 47 Case Structure Example: If years employed = 2, If years employed = 3, bonus is set to 200 bonus is set to 400 If years employed = 1, If years employed is CASE bonus is set to 100 years employed any other value, bonus is set to 800 1 2 3 Other bonus = 100 bonus = 200 bonus = 400 bonus = 800 48 Exercises on Flowcharts Exercise _1 Design an Flowchart to convert a temperature in degrees Fahrenheit to degrees Celsius. Hint: Celsius = 5/9 * (Fahrenheit – 32). Start Read Fahrenheit Celsius = 5/9 * (Fahrenheit – 32) Output Celsius End Exercise_2 Design an Flowchart that accepts two numbers and computes their sum, difference, product, and quotient. Start Read Num1 Read Num2 Sum = Num1 +Num2 Difference = Num1 - Num2 Product = Num1 * Num2 Quotient = Num1/Num2 Print Sum Print Difference Print Product Print Quotient END Exercise _ 3 You work in a store that sells imported fabric. Most of the fabric you sell is measured in square yards, but your customers want to know the equivalent amount in square meters. Design an Flowchart to perform this conversion. Hint: Start 1 square meter = 1.198 yards Read value in Yard (Y) Square meter Value (S) = Y/1.198 Output S END Exercise _ 4 Design an Flowchart that computes the employee’s gross salary and Net salary given the hours work and the hourly rate. Assume that 15% of the salary is deducted as taxes. Start Read Hours Work (H) Read Hourly Rate (R) Gross Salary = H * R Tax = (15/100) * Gross Salary Net Salary = Gross Salary - Tax Print Net Salary END Exercise _ 5 Design an Flowchart that computes the distance between two points (X1, Y1) and (X2,Y2). Start Hint: Distance = Read X1 Read X2 Read Y1 Read Y2 Z1 = (x1 – x2) Z2 = (y1 – y) Distance = Output Distance END Exercise _ 6 Design a flowchart for adding the test scores as given below: 26, 49, 98, 87, 62, 75 Exercise _ 7 The problem with Exercise_6 have some steps appear more than once, i.e. step 5 get second number, step 7, get third number, etc. One could shorten the algorithm or flowchart as follows: Exercise _ 8 In Exercise_6, step 3 to 5 will be repeated, where a number is obtained and added to the sum. So, we can use repetition structure to represent the answer by adding terminal value to end loop Exercise _ 9 In Exercise_6, step 3 to 5 will be repeated, by using for loop. START Count=6 Sum = 0 n=1 IS n Y Print Diff END Exercise _ 11 Design an algorithm and the corresponding flowchart for finding the sum of the numbers 2, 4, 6, 8, …, n Exercise _ 12 Exercise _ 13 Exercise _ 14 Exercise _ 15 Assignment Using flowcharts, write an algorithm to read 100 numbers and then display the sum. Write an algorithm to read two numbers then display the largest. Write an algorithm to read two numbers then display the smallest. Write an algorithm to read three numbers then display the largest. Write an algorithm to read 100 numbers then display the largest. Thank you

Use Quizgecko on...
Browser
Browser