Problem Solving and Program Design Programming. COMP104 Lecture 3 / Slide 2 Problem Solving Process l Define and analyze the problem. l Develop a solution.

Slides:



Advertisements
Similar presentations
The Software Lifecycle. Example Problem: Update a Checkbook Write a program that allows the user to enter a starting balance, a transaction type, D or.
Advertisements

LECTURE 1 CMSC 201. Overview Goal: Problem solving and algorithm development. Learn to program in Python. Algorithm - a set of unambiguous and ordered.
CS 6301 Lecture 2: First Program1. CS Topics of this lecture Introduce first program  Explore inputs and outputs of a program Arithmetic using.
 C++ programming facilitates a disciplined approach to program design. ◦ If you learn the correct way, you will be spared a lot of work and frustration.
Problem Solving and Program Design Programming. COMP102 Prog Fundamentals I : Problem Solving and Program Design/Slide 2 Problem Solving Process l Define.
Problem Solving and Program Design. COMP104 Problem Solving / Slide 2 Our First Program // a simple program #include using namespace std; int main() {
Your First C++ Program Aug 27, /27/08 CS 150 Introduction to Computer Science I C++  Based on the C programming language  One of today’s most.
C++ Basics. COMP104 C++ Basics / Slide 2 Introduction to C++ * C++ is a programming language for manipulating numbers and user-defined objects. * C++
Three types of computer languages
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
Computer Science 1620 Programming & Problem Solving.
 2003 Prentice Hall, Inc. All rights reserved. 1 Machine Languages, Assembly Languages, and High-level Languages Three types of computer languages 1.Machine.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 1: An Overview of Computers and Programming Languages C++ Programming:
Chapter 6: Functions.
Software Engineering 1 (Chap. 1) Object-Centered Design.
Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming.
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering.
COMPUTER SCIENCE I C++ INTRODUCTION
By Dr. Awad Khalil Computer Science & Engineering Department
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
1 Chapter 1 Overview of Programming and Problem Solving Dale/Weems Slides based on work by Sylvia Sorkin, Community College of Baltimore County - Essex.
COS120 Software Development Using C++ AUBG Fall semester 2010 Ref book: Problem Solving, Abstraction and Design Using C++ Authors: Frank Friedman, Elliot.
Chapter 2 Overview of C Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
Chapter 2 Overview of C++ Lecture Notes Prepared By: Blaise W. Liffick, PhD Department of Computer Science Millersville University Millersville, PA
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Overview of C++ Problem Solving, Abstraction, and Design using.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
Chapter 3: Completing the Problem- Solving Process and Getting Started with C++ Introduction to Programming with C++ Fourth Edition.
Lecture 6: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept.
Overview of C++ Chapter C++ Language Elements t Comments make a program easier to understand t // Used to signify a comment on a single line.
Overview of Programming and Problem Solving Textbook Chapter 1 1.
1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.
Week 1 Algorithmization and Programming Languages.
COMPUTER PROGRAMMING. A Typical C++ Environment Phases of C++ Programs: 1- Edit 2- Preprocess 3- Compile 4- Link 5- Load 6- Execute Loader Primary Memory.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Control Structures (B) Topics to cover here: Sequencing in C++ language.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
1 8/31/05CS150 Introduction to Computer Science 1 Hello World!
Chapter 1 Computers, Compilers, & Unix. Overview u Computer hardware u Unix u Computer Languages u Compilers.
C++ Basics Programming. COMP104 Lecture 5 / Slide 2 Introduction to C++ l C is a programming language developed in the 1970s with the UNIX operating system.
Principles of Programming CSEB134 : BS/ CHAPTER Fundamentals of the C Programming Language.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 2 C++ Basics.
Arithmetic, Functions and Input 9/16/13. Arithmetic Operators C++ has the same arithmetic operators as a calculator: * for multiplication: a * b – Not.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
CHAPTER 3 COMPLETING THE PROBLEM- SOLVING PROCESS AND GETTING STARTED WITH C++ An Introduction to Programming with C++ Fifth Edition.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
COMP 2710 Software Construction C++ Basics 2 and Exercises Dr. Xiao Qin Auburn University These slides.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output Samples.
Problem Solving and Program Design. Problem Solving Process Define and analyze the problem. Develop a solution. Write down the solution steps in detail.
Bill Tucker Austin Community College COSC 1315
Chapter Topics The Basics of a C++ Program Data Types
Completing the Problem-Solving Process
Basic Elements of C++.
C++ Programming: From Problem Analysis to Program Design
Basic Elements of C++ Chapter 2.
Programming Funamental slides
Programming Funamental slides
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Programs written in C and C++ can run on many different computers
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Life is Full of Alternatives
Arithmetic Operations
Capitolo 1 – Introduction C++ Programming
Chapter 2: Overview of C++
Presentation transcript:

Problem Solving and Program Design Programming

COMP104 Lecture 3 / Slide 2 Problem Solving Process l Define and analyze the problem. l Develop a solution. l Write down the solution steps in detail. l Test the solution and revise if necessary. l Document and maintain the solution.

COMP104 Lecture 3 / Slide 3 Example 1 l Problem Definition: n To find the best way to travel from HKUST to Central.

COMP104 Lecture 3 / Slide 4 Example 1 l Problem Analysis: n We want to find a way to make the trip quickly and cheaply. l Preliminary Design: n Find the best way to travel from HKUST to Central by considering all possible routes and modes of transportation.

COMP104 Lecture 3 / Slide 5 Example 1 l Refined Design: n Evaluate all possible routes and modes of transportation n Select the route that meets our goal (fastest and cheapest)

COMP104 Lecture 3 / Slide 6 Example 1 l Testing: n Test the recommended route and mode of transportation. Make sure the conditions have not changed (e.g., increased KMB bus fares, road construction). l Documentation: n Give a detailed description of your solution, and explain why you chose it. l Maintenance: n Test the program periodically. Adjust for changes by revising the recommended route and updating the documentation.

COMP104 Lecture 3 / Slide 7 Programming as a Problem Solving Process l Define and analyze the problem. n What is the input & output? n What constraints must be satisfied? n What information is essential? l Develop an algorithm. n What steps must be done? l Implement a program. l Compile, test, and debug the program. l Document and maintain the program.

COMP104 Lecture 3 / Slide 8 Example 2 l Problem Statement: n Convert US dollars into Hong Kong dollars. l Problem Analysis: n Input: Amount in US$ n Output: Amount in HK$ n Apply official currency exchange rates.

COMP104 Lecture 3 / Slide 9 Example 2 l Algorithm n Read in amount in US$ n Calculate the HK$ amount n Display the results

COMP104 Lecture 3 / Slide 10 Example 2 // converts US$ to HK$ #include using namespace std; int main(){ double usdollars; double hkdollars; // read in amount in US$ cout <<"Enter US$ amount and press return: "; cin >> usdollars; // calculate the HK$ amount hkdollars = 7.8 * usdollars; // display the results cout << "US$" << usdollars << " = HK$" << hkdollars << endl; return 0; }

COMP104 Lecture 3 / Slide 11 Example 2 l Program Implementation: Program comments: // Library reference: #include Function type: int Function name and (lack of) parameters: main( ) Statement braces: { } Variable declaration: double Input/output functions: cin, cout

COMP104 Lecture 3 / Slide 12 What Makes a Good Program? l Correctness n Meets the problem requirements n Produces correct results l Easy to read and understand l Easy to modify l Easy to debug l Efficient n Fast n Requires less memory

COMP104 Lecture 3 / Slide 13 Example 3 l Problem Statement n Given a collection of nickels (US 5-cent piece) and pennies (US 1-cent piece), find the equivalent number of Hong Kong dollars and 10-cent pieces (houji). l Problem Analysis n Input: –nickels (integer) - number of US nickels –pennies (integer) - number of US pennies n Output: –dollars (integer) - number HK dollar coins to return –houji (integer) - number HK 10-cent coins to return l Constraints: None

COMP104 Lecture 3 / Slide 14 Example 3: Initial Algorithm 1. Read in the numbers of nickels and pennies 2. Compute the total value in US dollars 3. Compute the total value in HK dollars to exchange 4. Find the number of HK dollar coins and houji coins 5. Display the numbers well-labeled

COMP104 Lecture 3 / Slide 15 Example 3: Program Skeleton // File: excoin.cpp // Determines the number of HK coins to exchange for US coins #include using namespace std; int main(){ int nickel; // number of nickels int penny; // number of pennies int dollar; // number of HK dollar coins int houji; // number of HK 10-cent coins double total_USD; // total value in US$ double total_HKD; // total value in HK$ // 1) Read in the number of nickels and pennies // 2) Compute the total value in US$ // 3) Compute the total value in HK$ to exchange // 4) Find the numbers of HK dollar and 10-cent coins // 5) Display the numbers of HK dollar and 10-cent coins return 0; }

COMP104 Lecture 3 / Slide 16 Example 3: Refined Algorithm 1. Read in the number of nickels and pennies 2. Compute the total value in US dollars 2.1 total_USD = (5 * nickel + penny)/ Compute the total in HK dollars to exchange 3.1. total_HKD = total_USD * US2HK 4. Find the number of HK dollar coins and 10-cent coins 4.1. total_HK_cent = total_HKD * dollar = total_HK_cent / houji = (total_HK_cent % 100) / Display the number of HK dollar and 10-cent coins

COMP104 Lecture 3 / Slide 17 Illustration of Step 4 l Suppose total_HKD in step 3 is $ total_HK_cent = total_HKD * 100 = dollar = total_HK_cent / 100 = 748 / 100 = 7 (no of HK dollar coins) 4.3. houji = (total_HK_cent % 100) / 10 = (748 % 100) / 10 = 48 / 10 = 4 (no of HK houji)

COMP104 Lecture 3 / Slide 18 C++ Arithmetic Operators The four operators +, -, *, and / work as we expect with the “normal” precedence rules (e.g., 5+2*3 = 11) l Parenthesis can be inserted to change the order of operations (e.g., (5+2)*3 = 21) l Be careful of integer division -- any remainder is discarded l The % (modulo) operator gives the remainder of integer division

// File: excoin.cpp // Determines the number of HK coins to exchange for US coins #include using namespace std; int main(){ const double US2HK = 7.8;// assume exchange rate is US$1 = HK$7.8 int nickel;// number of nickels int penny;// number of pennies int dollar;// number of HK dollar coins int houji;// number of HK 10-cent coins double total_USD;// total value in US$ int total_HK_cent;// total value in HK cents double total_HKD;// total value in HK$ // Read in the number of nickels and pennies cout << "Enter the number of nickels and press return: "; cin >> nickel; cout << "Enter the number of pennies and press return: "; cin >> penny;

// Compute the total value in US$ total_USD = (5 * nickel + penny) / 100.0; // Compute the total value in HK$ using the assumed exchange rate total_HKD = total_USD * US2HK; // Find the value in HK dollars and change total_HK_cent = total_HKD * 100; dollar = total_HK_cent / 100; houji = (total_HK_cent % 100)/10; // Display the number of HK dollar and 10-cent coins cout << "The change is HK " << dollar << " dollars and " << houji << " 10-cent coins." << endl; return 0; }

COMP104 Lecture 3 / Slide 21 Overview on Using Visual C++ l Enter source code (by typing or bringing in a file) l Compile source code n If there are error or warning messages, double-clicking on the error message will cause an arrow to point to the line where the suspected error is. Edit the source code and recompile. l Build execution module l Execute n If there are errors, edit the source code and recompile l Save source files on your floppy disk (A:) l Remove project directory (or mess) from hard disk (C:) in the computer if you work in lab