PROBLEM SOLVING CSC 111.

Slides:



Advertisements
Similar presentations
ALGORITHMS AND FLOWCHARTS
Advertisements

Solving Problems with Computers
Introduction to Programming
Exercise (1).
Flow Control Analysis & Design Tool: Flowcharts
ALGORITHMS AND FLOWCHARTS
Chapter 1 Pseudocode & Flowcharts
Chapter 1 Pseudocode & Flowcharts
Original Source : and Problem and Problem Solving.ppt.
ALGORITHMS AND FLOWCHARTS
PROGRAMMING LANGUAGES Prof. Lani Cantonjos. PROGRAM - set of step-by-step instructions that tells or directs the computer what to do. PROGRAMMING LANGUAGE.
1 Introduction to Flowcharting. 2 Writing a program Defining the problem –Write down what the program will do Planning –Write down the steps, draw a flowchart.
1 Introduction to Flowcharting. 2 Writing a program Defining the problem –Write down what the program will do Planning –Write down the steps, draw a flowchart.
Algorithm & Flow Charts
C++ Programming Language Lecture 2 Problem Analysis and Solution Representation By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
NA2204.1jcmt CSE 1320 Intermediate Programming C Program Basics Structure of a program and a function type name (parameters) { /* declarations */ statement;
ALGORITHMS AND FLOWCHARTS CSCI 105 – Computer Fluency.
Basic problem solving CSC 111.
Chapter 1 Pseudocode & Flowcharts
CSC 111. Solving Problems with Computers Java Programming: From Problem Analysis to Program Design, Third Edition3 Solving Problems Stages 1.Problem.
INTRODUCTION TO PROGRAMMING. Program Development Life Cycle The program development life cycle is a model that describes the stages involved in a program.
The Hashemite University Computer Engineering Department
Problem, Problem Solving, Algorithm & Flow Charts –Part 1 Presented By Manesh T Course:101 CS 101CS Manesh T1.
WHAT IS THIS? Clue…it’s a drink SIMPLE SEQUENCE CONTROL STRUCTURE Introduction A computer is an extremely powerful, fast machine. In less than a second,
Flowchart. a diagram of the sequence of movements or actions of people or things involved in a complex system or activity. a graphical representation.
ALGORITHMS AND FLOWCHARTS. A typical programming task can be divided into two phases: Problem solving phase  produce an ordered sequence of steps that.
Lecture 2: Introduction to Programming EEE2108: 공학프로그래밍 서강대학교 전자공학과 2011 학년도 2 학기 - Algorithms and Flowcharts -
Chapter One Problem Solving
ALGORITHMS AND FLOWCHARTS
Algorithm & Flow Charts Week 1
Lesson 2 Flowcharting.
Programming Languages
Chapter One Problem Solving
Problem , Problem Solving, Algorithm & Flow Charts –Part 1
Computer Programming.
COVERED BASICS ABOUT ALGORITHMS AND FLOWCHARTS
Computer Programming Flowchart.
Algorithms and Flowcharts
Chapter 5: Control Structure
Lecture 2 Introduction to Programming
Introduction To Flowcharting
2.0 Problem Solving PROGRAM DESIGN
Programming Logic n Techniques
Control Statement Examples
ALGORITHMS AND FLOWCHARTS
IDENTIFIERS CSC 111.
Chapter 1 Pseudocode & Flowcharts
Unit# 9: Computer Program Development
PROGRAM STRUCTURE CSC 111.
ALGORITHMS AND FLOWCHARTS
Pseudocode & Flowcharts
Inequalities 12/3/2018.
Programming Funamental slides
6.5 Inequalities 12/3/2018.
1) C program development 2) Selection structure
ALGORITHMS AND FLOWCHARTS
Chapter 1 Pseudocode & Flowcharts
Introduction to Algorithms and Programming
CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING
Faculty of Computer Science & Information System
ME 142 Engineering Computation I
Chapter 5: Control Structure
Introduction to Algorithms - 1
Chapter 3: Selection Structures: Making Decisions
Introduction to Programming
Chapter 3: Selection Structures: Making Decisions
Developing a Program.
Basic Concepts of Algorithm
Chapter 2 - Algorithms and Design
Chapter 1 Pseudocode & Flowcharts
Presentation transcript:

PROBLEM SOLVING CSC 111

Outline 1. Introduction 2. Problem Definition 3. Problem Analysis 4. Solution Design 4.1 Basic Steps 4.2 Algorithm 4.3 Flowchart 5. Examples

1. Introduction The basic steps to solve a problem are: Problem Definition: Define the main problem Problem Analysis: Identify the inputs Identify the outputs Identify the processing operations Solution Design: Identify the detailed steps that the computer will follow to reach the expected result

What is/are your target(s)? 2. Problem Definition What is/are your target(s)? Write a program that prints the sum of two numbers. Write a program that prints the average of three numbers. Write a program that prints the volume of a cube and the sum of its surfaces’ areas.

3. Problem Analysis – Example 1 Identify Input, Output and Processing steps Write a program that prints the sum of two numbers Input: 1st number (x1)  Value=?  entered by the user 2nd number (x2) Output: The summation (sum) Processing: (Input  Output) Summation = first + second sum = x1 + x2

3. Problem Analysis – Example 2 Write a program that prints the average of three numbers Input: 1st number (x1)  Value=?  entered by the user 2nd number (x2) 3rd number (x3) Output : The average (avg) Processing (Input  Output) Average = (first + second + third ) / 3 avg = (x1 + x2 + x3) / 3

3. Problem Analysis – Example 3 Write a program that prints the volume of a cube and the sum of its surfaces’ areas Input: Side length (L)  Value=?  entered by the user Output: The volume (V) The surface area (A) Processing (Input  Output) Volume = side * side * side V = L * L * L Surface area = ( side * side ) * 6 A = L * L * 6

4. Solution Design Problem Definition Problem Analysis Solution Design Algorithm Problem Analysis Solution Design Design a solution for the problem by writing an algorithm or drawing a flowchart Flowchart

4.1 Solution Design – Basic Steps Read all the inputs Calculate operations Print the output

4.2 Solution Design – Algorithm EXAMPLE 1 Write a program that prints the sum of two numbers Start the program 2. Read the first number and save it in the variable N1 3. Read the second number and save it in the variable N2 4. Add both numbers and save the result in the variable Sum  Sum = N1 + N2 5. Print the result (Sum) 6. End the program INPUT PROCESSING OUTPUT

4.2 Solution Design – Algorithm EXAMPLE 2 Write a program that prints the average of three numbers 1. Start the program 2. Read the first number and save it in the variable (num1) 3. Read the second number and save it in the variable (num2) 4. Read the third number and save it in the variable (num3) 5. Add the three numbers and save the result in the variable (sum)  sum = num1 + num2 + num3 6. Divide (sum) by 3 and save the result in the variable (avg)  avg = sum / 3 7. Print the result (avg) 8. End the program INPUT PROCESSING OUTPUT

4.2 Solution Design – Algorithm EXAMPLE 3 Write a program that prints the volume of a cube and the sum of its surfaces’ areas Start the program 2. Read the length of the side and save it in the variable (L) 3. Calculate the volume and save the result in the variable (V) V = L* L* L 4. Calculate the sum of surfaces’ areas and store the result in the variable (S) S = 6 * L* L 5. Print output1: the variable ( Volume ) 6. Print output2: the variable ( Surface ) 7. End the program INPUT PROCESSING OUTPUT

4.3 Solution Design – Flowchart A flowchart is the graphical representation of the algorithm. The flowchart illustrates the previously developed algorithm in more details. These details make the algorithm very close to the code implementation in any high level programming language. Worth to mention, that both algorithm and flowchart are independent of the used programming language. The flowchart uses standard symbols to illustrate each action. These symbols are shown in the next slide.

4.3 Solution Design – Flowchart Start End Start/End of the program Read n1 Print n1 Read/Print the variable named n1 x = 5 Assignment operation: the variable x is given the value 5 N2 = n1+3 Arithmetic operation: the variable N2 equals the value of n1 plus 3 n1 > 3 True False Decision: is n1 greater than 3? If True (yes) follow the left arrow. If False (no) follow the right arrow Connector arrows: connects between symbols 1 2 Connector points: marking points to sub-flowcharts // my name Comment: the compiler discards comments. Here my name is written as a comment

4.3 Solution Design – Flowchart EXAMPLE 1 Draw a flowchart that adds two numbers Start Start the program Read n1 Read the variable n1 from user Read n2 Read the variable n2 from user Sum = n1 + n2 Add n1 to n2 and store the result in Sum Print Sum Display Sum End End Note that variables should have different names

4.3 Solution Design – Flowchart EXAMPLE 2 Write a program that prints the average of three numbers Start Start the program Read x1 Read the variable x1 Read x2 Read the variable x2 Read x3 Read the variable x3 R=x1+x2+x3 Add x1, x2, x3 and store the result in R Avr=R/3 Divide R by 3 and store the result in Avr Continuation point 1 (in the next slide) 1

4.3 Solution Design – Flowchart EXAMPLE 2 (cnt’d) 1 Continuation point 1 (from previous slide) Print Avr Display Avr End End Note that variables in the Right Hand Side of any formula should have values. These are marked in blue in the flowchart. Also the variables to be printed should already have values.

4.3 Solution Design – Flowchart EXAMPLE 2 (cnt’d) This is a more simplified way of the flowchart of Example 2 Start Start the program Read x1, x2, x3 Read the variables x1, x2, x3 Avr=(x1+x2+x3)/3 Divide the sum of x1, x2, x3 over 3 and store the result in Avr Print Avr Display Avr End End

4.3 Solution Design – Flowchart EXAMPLE 3 Write a program that prints the volume of a cube and the sum of its surfaces’ areas Start Start the program Read L Read the variable L V=L*L*L Calculate the volume and store in V Calculate the sum of surface areas. Store the result in S S=6*L*L Print V Display V Print S Display S End End

5. More Examples EXAMPLE 4 - ALGORITHM Write a program that calculates the required tax based on the employer’s salary. If the salary is less than 5,000 SAR then tax = 10% of the salary; otherwise, the tax = 8%. Input: Employer’s salary (salary)  Value=?  entered by the user Output: The required tax (tax) Processing (Input  Output) If salary < 5000 then tax = 0.1 If salary >= 5000 then tax = 0.08

5. More Examples EXAMPLE 4 - FLOWCHART Start the program Read salary Read the variable salary from the user True False salary < 5000? Is salary < 5000? tax = 0.1 If salary < 5000 then tax = 0.1 If salary >= 5000, tax= 0.08 tax =0.08 Display tax Print tax End End

5. More Examples EXAMPLE 5 - ALGORITHM Write a program that prints the absolute value of a number x Input: A number (x)  Value=?  entered by the user Output: The absolute value of x (|x|) Processing (Input  Output) If x >= 0 then do nothing If x < 0 then x = x * -1

5. More Examples EXAMPLE 5 - FLOWCHART Start the program Read x Read the variable x from the user True x < 0? False Is x negative? If x is negative then multiply x by -1. Store the new value in x x = x * -1 If x is not negative then do nothing Print x Display x End End

Self-Check Exercises (1) Write the algorithm and draw the flowchart of the following problems: Convert a temperature from ⁰C into ⁰F. Find how many seconds are there in a number of minutes? hours? Specify if a student passes a course or not. Add the same number to itself n times. W2.2 Problem Solving

Self-Check Exercises (2) Detect the faults in the following flowcharts: Start Read x1 Read x2 Product= x1 * x2 Print Product End Start Read n1 Read n2 Sum = n1 + n2 Print n1 End Start Read n1 Read n2 Diff = n1 – n2 Print Sum End FC 1 FC 2 FC 3 W1.3 Problem Solving