Practice Programming Exam 1 Review: Part 2 1. Programming Example Bags Fly Fee!!! 2 XYZ is a commercial airline that asked you to create a MATLAB program.

Slides:



Advertisements
Similar presentations
Chapter 4 - Control Statements
Advertisements

Detecting Bugs Using Assertions Ben Scribner. Defining the Problem  Bugs exist  Unexpected errors happen Hardware failures Loss of data Data may exist.
Chapter 3 Program Design And Branching Structures.
MATLAB Examples. CS 1112 MATLAB Examples Find the number of positive numbers in a vector x = input( 'Enter a vector: ' ); count = 0; for ii = 1:length(x),
Input and Output ENGR 1181 MATLAB 5. Input and Output In The Real World Script files (which provide outputs given inputs) are important tools in MATLAB.
LECTURE 1 CMSC 201. Overview Goal: Problem solving and algorithm development. Learn to program in Python. Algorithm - a set of unambiguous and ordered.
Algorithms. Software Development Method 1.Specify the problem requirements 2.Analyze the problem 3.Design the algorithm to solve the problem 4.Implement.
ENGG 1801 Engineering Computing MATLAB Lecture 2: Background for Week 4 Tutorial Simple Programming in MATLAB.
TrueBASIC Ch 4 & 5 Sample Problems. What are the errors? (4 total) INPUT age IF age > 7 THEN PRINT "rollercoaster" ELSE PRINT "ferris wheel" IF age
11.3 Function Prototypes A Function Prototype contains the function’s return type, name and parameter list Writing the function prototype is “declaring”
Algorithms. Software Development Method 1.Specify the problem requirements 2.Analyze the problem 3.Design the algorithm to solve the problem 4.Implement.
JavaScript with Input & Output Step 1: Use tags JavaScript Template.
BBS Yapısal Programlama (Structured Programming)1 From problem to program In “real world”… Problem in Natural language Top Down Design in pseudo-code.
19-Aug-15 About the Chat program. 2 Constraints You can't have two programs (or two copies of the same program) listen to the same port on the same machine.
Exam 1: Review 1.History of computers 2.Fundamentals of computer architecture 3.Developing algorithms and programs 4.Variables and data types 5.User I/O.
General Programming Introduction to Computing Science and Programming I.
Developing a Solution How to create the computer-based solution for a real-world problem. 1.
Library Functions... 1.Old functions 2.Vocabulary 3.Rounding numbers 4.Generating random numbers 5.mod() 6.Properties of mod() 7.Ex1: even or odd? 8.Ex2:
Designing Programs with Branches CSIS 1595: Fundamentals of Programming and Problem Solving 1.
1. Definition and General Structure 2. Small Example 1 3. Simplified Structure 4. Short Additional Examples 5. Full Example 2 6. Common Error The for loop.
1. Understanding the Problem 2. Brainstorming 3. Drawing an I/O (Input/Output) diagram 4. 5-step Process (or: Small iPods Make Copying Tough) Developing.
1. Exam Topics Difference between computers and calculators John creates a new device. It will compute the orbit of all the planets in the solar system.
Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language.
EXERCISE IN CLASS CHAPTER 2. PART 1 SEQUENCE SCENARIO 1 Write an algorithm for a C program, that prompts user to enter total number of umbrellas he/she.
Review for Exam2 Key Ideas 1. Key Ideas: Boolean Operators (2 > 3) || (3 < 29.3) A.True B.False C.Impossible to determine (22 > 3) && (3 > 29.3) A.True.
CSE1222: Lecture 6The Ohio State University1. Common Mistakes with Conditions (1)  Consider the following code: int age(26); if (age = 18) { cout
Multi Way Selection You can choose statement(s) to run from many sets of choices. There are two cases for this: (a)Multi way selection by nested IF structure.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Conditionals CS 103 February 16, Blast from the Past: C14 Dating Problem Statement: Calculate the age of a fossil from its C-14 radioactivity Problem.
Software Development Method Reference : Problem Solving & Program Design in C ; Jeri R.Hanly, Elliot B.Koffman.
TK 1914 : C++ Programming Control Structures I (Selection)
Library Functions... 1.Old functions 2.Vocabulary 3.Rounding numbers 4.Generating random numbers 5.mod() 6.Properties of mod() 7.Ex1: even or odd? 8.Ex2:
Asking the USER for values to use in a software 1 Input.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
Intro to Nested Looping Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Introduction to Engineering MATLAB - 13 Agenda Conditional statements  If – end  If – else – end  if – elseif – else - end.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Designing While Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Review while loops Control variables Example Infinite loop
Asking the USER for values to use in a software 1 Input.
Topic Review for Exam 1 0.Infrastructure: You must be able to use MATLAB, Blackboard Exams, and Blackboard submissions 1. Computer Developing algorithms.
Lecture 6: Output 1.Presenting results in a professional manner 2.semicolon, disp(), fprintf() 3.Placeholders 4.Special characters 5.Format-modifiers 1.
Algorithm Discovery and Design Objectives: Interpret pseudocode Write pseudocode, using the three types of operations: * sequential (steps in order written)
31/01/ Selection If selection construct.
1. Comparing Strings 2. Converting strings/numbers 3. Additional String Functions Strings Built-In Functions 1.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
Topics: Selection Statements. Processing Involving Selecting Instructions An instruction that allows deviation and selection to take place uses the ‘IF’
Intro to Loops 1.General Knowledge 2.Two Types of Loops 3.The WHILE loop 1.
3.1.3 Program Flow control Structured programming – SELECTION in greater detail.
Algorithms and Pseudocode
LO: We’re learning to outline a program using Pseudo Code.
CS 101 – Oct. 7 Solving simple problems: create algorithm Structure of solution –Sequence of steps (1,2,3….) –Sometimes we need to make a choice –Sometimes.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Selection Using IF THEN ELSE CASE Introducing Loops.
Lesson Objectives Aims To be able to write an algorithm in Pseudo Code
ENGG 1801 Engineering Computing
When I want to execute the subroutine I just give the command Write()
Conditions and Ifs BIS1523 – Lecture 8.
Use proper case (ie Caps for the beginnings of words)
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
SELECTION STATEMENTS (2)
Intro to Nested Looping
Intro to Nested Looping
Programming Concepts and Database
Making decisions with code
Basic Lessons 5 & 6 Mr. Kalmes.
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
Presentation transcript:

Practice Programming Exam 1 Review: Part 2 1

Programming Example Bags Fly Fee!!! 2 XYZ is a commercial airline that asked you to create a MATLAB program that can calculate the baggage fees for their passengers. They charge $10 for every bag and $.10 for every pound. Program # of bags Total Weight in lb Total Charge

3 fprintf('Your total charge is %.2f dollars \n',Charge); Any ideas …… Do we know how to solve manually? Write Algorithm… Code using MATLAB Test. % collect the inputs from the user (#of bags&total weight) % Calculate the charge=#of bags*10+Total weight*.10) % Print the total charge clc clear NmbrBags = input('How many bags you have? '); TotalWeight = input('Enter the total weight : '); Charge = NmbrBags*10 + TotalWeight*.10;

4 Test, Test, Test….

Life is easy!.. No it is not! There are other constraints: First class customers DON’T pay for baggage fees. (display $0.00) Business and Economy pay according to the formula given before. Regardless of the class (First/Business/Economy), no passenger is allowed: More than 3 bags. (3 is okay but not more) Total weight exceeding 100 lb. (100 lb. is okay but not more) Give an error message, if any of the 2 conditions happen. What if, by mistake, we have 0 bags and a non zero value for total weight?!!!! Propose a solution (Give an error message). 5 Program # of bags Total Weight in lb Total Charge Error Message Class Error Message

6 clear clc % collect the inputs from the user (bags,weight,class) NmbrBags = input('How many bags you have? '); TotalWeight = input('Enter the total weight: '); Class = input('Enter 1=First, 2=Business, 3=Economy: '); %SWITCH Decide what class they are flying if Class == 1 % In case of First, Check the limits if NmbrBags > 3 || TotalWeight > 100 %if true, error msg fprintf ('You are over the limit, can''t fly!'); else %if not then 0.00 dollars fprintf ('Your total charge is 0.00 dollars \n'); end else if NmbrBags > 3 || TotalWeight > 100 fprintf ('You are over the limit, can''t fly!\n'); % check for invalid inputs elseif NmbrBags == 0 && TotalWeight ~= 0 % print error msg fprintf('Check your inputs!\n'); else % Calculate the total charge Charge = NmbrBags*10 + TotalWeight*.10; % Print the total charge fprintf('Your total charge is %.2f dollars \n',Charge); end

7 How about we ask the user to enter the number of bags and check if the input is zero or not, that’s before we give the prompt to enter the total weight. Because regardless of the class that the customer is traveling, 0 bags = 0 dollars. This way if the user put 0 for bags, they will not be prompted to enter the weight. If bags =0 True Print total is 0.00 False Ask for the weight, and the class, then continue….

8 clear clc % check number of bags NmbrBags = input('How many bags you have? '); if NmbrBags == 0 && TotalWeight ~0 fprintf ('Check your inputs \n'); else TotalWeight = input('Enter the total weight: '); Class = input('Enter 1=First, 2=Business, 3=Economy: '); % Check for the limits if NmbrBags > 3 || TotalWeight > 100 fprintf ('You are over the limit, can''t fly!\n'); else if Class == 1 % In case of First, Check the limits fprintf ('Your total charge is 0.00 dollars \n'); else Charge = NmbrBags*10 + TotalWeight*.10; % Print the total charge fprintf('Your total charge is %.2f dollars \n',Charge); end Undefined variable and unrelated output

9 clear clc % check number of bags NmbrBags = input('How many bags you have? '); if NmbrBags == 0 fprintf ('Your total charge is 0.00 dollars \n'); else TotalWeight = input('Enter the total weight: '); Class = input('Enter 1=First, 2=Business, 3=Economy: '); % Check for the limits if NmbrBags > 3 || TotalWeight > 100 fprintf ('You are over the limit, can''t fly!\n'); else if Class == 1 % In case of First, Check the limits fprintf ('Your total charge is 0.00 dollars \n'); else Charge = NmbrBags*10 + TotalWeight*.10; % Print the total charge fprintf('Your total charge is %.2f dollars \n',Charge); end

Test, Test, Test 10

How to tackle Exam (1) 11 RUN home STUDY hard SLEEP well SHOW-UP for the exam. Good Luck!