BITG 1113: Problem Solving

Similar documents
Chapter One Introduction to Programming

Simple C++ Programs. Engineering Problem Solving with C++, Etter/Ingber. Dev-C++ Dev-C++ Windows Friendly Exit. The C++ Programming Language

PROG0101 Fundamentals of Programming PROG0101 FUNDAMENTALS OF PROGRAMMING. Chapter 3 Algorithms

ALGORITHMS AND FLOWCHARTS

Scanner sc = new Scanner(System.in); // scanner for the keyboard. Scanner sc = new Scanner(System.in); // scanner for the keyboard

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

An Incomplete C++ Primer. University of Wyoming MA 5310

2 SYSTEM DESCRIPTION TECHNIQUES

Chapter 1 An Introduction to Computers and Problem Solving

Ch 7-1. Object-Oriented Programming and Classes

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 3: Input/Output

ChE-1800 H-2: Flowchart Diagrams (last updated January 13, 2013)

Chapter 8 Selection 8-1

Sequential Program Execution

Chapter 3 Problem Solving

C Programming Language

QUIZ-II QUIZ-II. Chapter 5: Control Structures II (Repetition) Objectives. Objectives (cont d.) 20/11/2015. EEE 117 Computer Programming Fall

McGraw-Hill The McGraw-Hill Companies, Inc.,

Chapter 2: Algorithm Discovery and Design. Invitation to Computer Science, C++ Version, Third Edition

7.7 Case Study: Calculating Depreciation

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Test #4 November 20, True or False (2 points each)

How do you compare numbers? On a number line, larger numbers are to the right and smaller numbers are to the left.

COMPUTER SCIENCE (5651) Test at a Glance

Sources: On the Web: Slides will be available on:

Basics of I/O Streams and File I/O

Comp151. Definitions & Declarations

9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

Advanced LED Controller (LED Chaser)

Algorithm & Flowchart & Pseudo code. Staff Incharge: S.Sasirekha

Chapter 12 Programming Concepts and Languages

Chapter 5 Functions. Introducing Functions

Chapter 13: Program Development and Programming Languages

S-Parameters and Related Quantities Sam Wetterlin 10/20/09

COMPUTER SCIENCE. Paper 1 (THEORY)

DRIVING LOOOONG CABLES

ADVANCED SCHOOL OF SYSTEMS AND DATA STUDIES (ASSDAS) PROGRAM: CTech in Computer Science

Chapter 13 Computer Programs and Programming Languages. Discovering Computers Your Interactive Guide to the Digital World

Computer Programming Lecturer: Dr. Laith Abdullah Mohammed

Basics of C++ and object orientation in OpenFOAM

Florida Math Correlation of the ALEKS course Florida Math 0018 to the Florida Mathematics Competencies - Lower

Ubuntu. Ubuntu. C++ Overview. Ubuntu. History of C++ Major Features of C++

History of U.S. Measurement

TECH TIP # 37 SOLVING SERIES/PARALLEL CIRCUITS THREE LAWS --- SERIES CIRCUITS LAW # THE SAME CURRENT FLOWS THROUGH ALL PARTS OF THE CIRCUIT

Moving from C++ to VBA

Oct: 50 8 = 6 (r = 2) 6 8 = 0 (r = 6) Writing the remainders in reverse order we get: (50) 10 = (62) 8

C++ Language Tutorial

Visual Studio 2008 Express Editions

Zero: If P is a polynomial and if c is a number such that P (c) = 0 then c is a zero of P.

Programming Languages The McGraw-Hill Companies, Inc. All rights reserved.

Scope and Sequence KA KB 1A 1B 2A 2B 3A 3B 4A 4B 5A 5B 6A 6B

Instructor Özgür ZEYDAN (PhD) CIV 112 Computer Programming

Lab 2 - CMPS 1043, Computer Science I Introduction to File Input/Output (I/O) Projects and Solutions (C++)

Electrical Fundamentals Module 3: Parallel Circuits

Charlesworth School Year Group Maths Targets

WESTMORELAND COUNTY PUBLIC SCHOOLS Integrated Instructional Pacing Guide and Checklist Computer Math

Autumn - 12 Weeks. Spring 11 Weeks. Summer 12 Weeks. Not As We Know It Limited 2014

Programming and Software Development CTAG Alignments

PAYCHEX, INC. BASIC BUSINESS MATH TRAINING MODULE

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Magnets. Electromagnets. and. Thomas Jefferson National Accelerator Facility - Office of Science Education

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

I PUC - Computer Science. Practical s Syllabus. Contents

CSI 333 Lecture 1 Number Systems

Using Visual Logic with Pseudocode to Teach an Introductory Programming Course

Notes on Algorithms, Pseudocode, and Flowcharts

VB.NET Programming Fundamentals

Algorithms, Flowcharts & Program Design. ComPro

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Tristan s Guide to: Solving Series Circuits. Version: 1.0 Written in Written By: Tristan Miller Tristan@CatherineNorth.com

PROGRAMMING IN C PROGRAMMING IN C CONTENT AT A GLANCE

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

Tristan s Guide to: Solving Parallel Circuits. Version: 1.0 Written in Written By: Tristan Miller Tristan@CatherineNorth.com

Topics. Introduction. Java History CS 146. Introduction to Programming and Algorithms Module 1. Module Objectives

Total Quality Management (TQM) Quality, Success and Failure. Total Quality Management (TQM) vs. Process Reengineering (BPR)

PS-6.2 Explain the factors that determine potential and kinetic energy and the transformation of one to the other.

Calculating Area, Perimeter and Volume

Introduction to the Smith Chart for the MSA Sam Wetterlin 10/12/09 Z +

Series and Parallel Circuits

2010/9/19. Binary number system. Binary numbers. Outline. Binary to decimal

Series and Parallel Resistive Circuits

Outline. hardware components programming environments. installing Python executing Python code. decimal and binary notations running Sage

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Robotics & Automation

Introduction to Programming (in C++) Loops. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

Appendix K Introduction to Microsoft Visual C++ 6.0

Electrical Fault Level Calculations Using the MVA Method

Example. Introduction to Programming (in C++) Loops. The while statement. Write the numbers 1 N. Assume the following specification:

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

JavaScript: Control Statements I

The C Programming Language course syllabus associate level

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

Transformer circuit calculations

MS Visual C++ Introduction. Quick Introduction. A1 Visual C++

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be...

F ahrenheit = 9 Celsius + 32

C++ INTERVIEW QUESTIONS

Transcription:

BITG 1113: Problem Solving 1

Learning Outcomes At the end of this lecture, you should be able to: explain the software development methodology identify basic problem solving techniques describe the steps involved in problem solving identify the input, the process and the output of a given problem. design algorithms to solve programming problems by using flow chart and pseudocode 2

Program Development The development process critically determines the overall quality and success of the program. If any program is designed carefully using good structured development techniques, the program will be efficient, error-free and easy to maintain. Most programming projects are built using system development life cycle. One of the popular system development life cycle is known as the waterfall model. 3

System Development Life Cycle (SDLC) 4

Program Development Procedural programming : A programming approach that focus on the process. Normally, a big problem is divided into smaller sub problems. In the program, each sub problem process (instruction) is coded in a function. This programming approach employs a topdown design model to define its program structure. Program development is a multi-step process that requires you to understand the problem, design a solution, code the program, and test the program. 5

Program Development 4 important steps involved in the programming process (solving a problem by developing program): Step 1 - Analysis : Understand the problem. Then, clearly define what is needed to solve the problem. If approach uses top down design, then draw a structure chart. Step 2 - Design the algorithm : Algorithm is a set of well defined and precise instructions (or steps) that leads to a solution. Check the algorithm for logic errors. Step 3 - Code : Type, save and compile the program code. Program is an algorithm expressed in a language that the computer can understand. Correct any syntax errors found during compilation. Repeat Step 3 as many times as necessary. Step 4 - Test the program : Run the program on sample data, and correct any run-time and logic errors found. Errors may require modification of the algorithm and program code. Repeat Step 4 as many times as neccessary. Validate the result of the 6 program.

Problem Solving Problem 1 example Problem Statement (or systems requirements) : Your summer surveying job requires you to study some maps that give distances in miles. You and your co-workers prefer to deal in metric measurements (km). Write a program that performs the necessary conversion.the program will display the distance in km. Formula: (1 mile = 1.609 kilometers) 7

Step 1: Understand the problem Highlight the problem statement. Your summer surveying job requires you to study some maps that give distances in miles. You and your co-workers prefer to deal in metric measurements (km). Write a program that performs the necessary conversion. The program will display the distance in km. Formula: (1 mile = 1.609 kilometers) 8

Identify : What are the INPUTs, PROCESSes, OUTPUTs? Input the distance in miles : M Process (include the formula) Convert distance in miles to kilometers (Formula given : 1 mile = 1.609 kilometers) Therefore to calculate distance in kilometers: K = M x 1.609 Output the distance in kilometers : K 9

Draw a Structure Chart (only if using topdown design) Main Problem Input the distance in miles Convert the distance in miles to kilometers Display the distance in kilometers 10

Step 2: Design the Algorithm. Program design - To determine how to take inputs and convert them into the output that have been specified in the program requirements statement. Then test the algorithm for its correctness (no logical errors). Algorithm can be designed using: a) Pseudocode or b) Flowchart 11

a) Pseudocode Precise algorithmic description of program logic. Its purpose is to describe, in precise algorithm detail, what the program being design is to do. Requires the defining the steps to accomplish the task in sufficient detail so that they can be converted into a computer program. 12

Algorithm using Pseudocode 1.0 Start 2.0 Call input function 2.1 Read distance in miles : M 2.2 Return M 3.0 Call convert function 3.1 Calculate distance in kilometer : K= M x 1.609 3.2 Return K 4.0 Call display function 4.1 Display the distance in kilometers : K 5.0 Stop 13

b) Flow Chart: Basic Symbols Represent Various Operation Direction Preparation Terminal beginning / end Decision Connector Process Input / Output Predefined Process (Module call) 14

Algorithm using Flow Chart input () convert (M) Start Read distance in miles (M) K = M * 1.609 M = input() Return M Return K K = convert(m) display (K) display(k) Display distance in kilometers (K) Stop Return 15

Step 3: Code the program /* Problem Solving Exercise : Introduction Comments to C++ */ #include <iostream> using namespace std; Preprocessor directives Namespace double input(); //global declaration void display(double K); double convert(double M); int main() { double M, K; //local declaration //M represents distance in miles //K represents distance in kilometers } M = input(); K = convert(m); display(k); return 0; Global Declaration Area Main Function 16

double input() { double miles; cout<<"please enter distance in miles :"; cin>>miles; return miles; } double convert(double distance_in_miles) { double km; km = distance_in_miles * 1.609; return km; } User- Defined Function User- Defined Function void display(double distance_in_km) { cout<< "Distance in kilometre is "; cout<< distance_in_km; cout<< "\n"; } User- Defined Function 17

Step 4: Test the program. Sample program run and its result (output): 18

ANOTHER APPROACH TO SOLVING PROBLEM 1 (without using top down design) Step 1: Understand the problem Highlight the problem statement. Your summer surveying job requires you to study some maps that give distances in miles. You and your co-workers prefer to deal in metric measurements (km). Write a program that performs the necessary conversion. The program will display the distance in km. Formula: (1 mile = 1.609 kilometers) 19

Identify : What are the INPUTs, PROCESSes, OUTPUTs? Input the distances in miles : miles Process (include the formula) Calculate kilometers from miles. (Formula given : 1 mile = 1.609 kilometers) kms = miles x 1.609 Output the distance in kilometers : kms 20

Pseudocode 1.0 Start Step 2: Design the Algorithm. Flowchart Start Get distance in miles, miles 2.0 Get the distance in miles : miles 3.0 Calculate the distance in kilometer : kms = miles x 1.609 4.0 Display the distance in kilometers : kms 5.0 Stop Calculate distance in kilometers kms = miles x 1.609 Display distance in kilometers, kms Stop 21

Step 3: Code the program #include <iostream> using namespace std; #define mile_to_km 1.609 int main( ) { double miles,kms; cout << "Enter the distance in miles that need "; cout << "to be converted to kilometers : "; cin >> miles; kms = mile_to_km * miles; } cout << endl << miles <<" in miles is "<< kms; cout <<" in kilometers "<< endl << endl; return 0; 22

Step 4: Test the program. Sample program run and its result (output): 23

EXAMPLE OF PROBLEM 2 (without using top down design) Write a program that inputs 2 integers n1 and n2. Calculate and prints the value of n1 2 and n2 3 Step 1: Understand the problem Highlight the problem statement. Write a program that inputs 2 integers n1 and n2. Calculate and prints the value of n1 2 and n2 3. Identify : Input : n1, n2 Process - Calculate n1 2 : ans1 = n1 * n1 - Calculate n2 3 : ans2 = n2 * n2 * n2 Output : ans1, ans2 24

Step 2 : Design the algorithm Flowchart Pseudocode 1. Start 2. Input n1 and n2 3. Calculate n1 2 : ans1 = n1 * n1 4. Calculate n2 3 : ans2 = n2 * n2 * n2 4. Print ans1, ans2 5. Stop Start Input n1,n2 Calculate ans1=n1*n1, ans2 = n2*n2*n2 Print ans1, ans2 Stop 25

Step 3: Code the program #include <iostream> #include <cmath> using namespace std; Step 4: Test the program. Sample program run and its result (output): int main() { double n1,n2,ans1,ans2; cin>>n1>>n2; ans1 = pow(n1,2); ans2 = pow(n2,3); } cout<<ans1<<endl; cout<<ans2<<endl; return 0; 26

EXAMPLE OF PROBLEM 3 (without using top down design) Step 1: Understand the problem Highlight the problem statement. Ohm's law is often expressed in the form V=IR, where V is the voltage measured in volts, I is the current measured in amps, and R is the resistance measured in ohms. Write a program that can calculate how much current (I) would flow through an input ohm of resistor (R) if you were to connect the resistor to an input volts of power supply (V). However you need to check for the input volts of the power supply. If it is > 10 volts, don t calculate the current, you just print out a message The volts is too big. If the volt is <= 10, calculate and print the current (I) and if the volts is negative, print out a message Not a valid input. 27

Identify : Input : V, R Process ( include the Formula) If V>10, print message The volts is too big If V<=10, I = V/R If V<0, print message Not a valid input Output : I 28

Step 2 : Design the algorithm Flowchart Pseudocode Start 1.0 Start Get V,R 2.0 Get the input data for V, R V > 10? yes Print msg The volts is too big 3.0 If V > 10 print message The volts is too big else If V<0, print msg Not a valid input else If V<=10, I = V/R, print I no V < 0? no V <=10? yes yes Print msg Not a valid input I=V/R 4.0 Stop no Print I Stop 29

Step 3: Code the program #include <iostream> using namespace std; int main( ) { double V,R,I; cout << "Enter V : "; cin >> V ; cout << "Enter R : "; cin >> R; if (V > 10) cout << "The volts is too big\n"; else if (V < 0) cout << "The input is invalid\n"; else if (V < 10) { I = V/R; cout << "I = " << I << endl; } return 0; } 30

Step 4: Test the program. Sample 1 of the program run and its result (output): 31

Sample 2 of the program run and its result (output): 32

Sample 3 of the program run and its result (output): 33

Identify Input, Process and Output to solves each problem, write pseudocode and draw the flowchart for each one : Problem 1 Calculate the area of a circle based on a given radius. Formula : Area of a circle = 3.14 * radius * radius Problem 2 In a maths subject Sam has taken 5 monthly tests. Input the marks Sam has secured in each of the tests and find out his average mark. Then display the result. Problem 3 Solve the problem using top down design approach to calculate the sum, average, difference, product, quotient and remainder of two integer numbers given by the user. Use the main function to display their results. -THE END- 34