Pseudo Code Practice Problems:



Similar documents
Conditionals (with solutions)

Boolean Expressions 1. In C++, the number 0 (zero) is considered to be false, all other numbers are true.

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

Chapter 8 Selection 8-1

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

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Data Structures. Algorithm Performance and Big O Analysis

J a v a Quiz (Unit 3, Test 0 Practice)

Topic 11 Scanner object, conditional execution

This loop prints out the numbers from 1 through 10 on separate lines. How does it work? Output:

Algorithm Design and Analysis Homework #1 Due: 5pm, Friday, October 4, 2013 TA === Homework submission instructions ===

1 Description of The Simpletron

PIC 10A. Lecture 7: Graphics II and intro to the if statement

Solving Equations. How do you know that x = 3 in the equation, 2x - 1 = 5?

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015

Lecture 2 Notes: Flow of Control

Software Testing. Definition: Testing is a process of executing a program with data, with the sole intention of finding errors in the program.

1. a procedure that you perform frequently. 2. Create a command. 3. Create a new. 4. Create custom for Excel.

1. Use the class definition above to circle and identify the parts of code from the list given in parts a j.

The C++ Language. Loops. ! Recall that a loop is another of the four basic programming language structures

Chapter 6 - Condor finder

Domain of a Composition

Digital indicator 3011

NF5-12 Flexibility with Equivalent Fractions and Pages

Algorithm and Flowchart Structured Programming 1

Outline. Conditional Statements. Logical Data in C. Logical Expressions. Relational Examples. Relational Operators

CS1010 Programming Methodology A beginning in problem solving in Computer Science. Aaron Tan 20 July 2015

Notes for EER #4 Graph transformations (vertical & horizontal shifts, vertical stretching & compression, and reflections) of basic functions.

>

Curriculum Design for Mathematic Lesson Probability

Chapter 5. Selection 5-1

Homework Activities for Kindergarten

3/13/2012. Writing Simple C Programs. ESc101: Decision making using if-else and switch statements. Writing Simple C Programs

Scientific Notation. Section 7-1 Part 2

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Volunteer Manual Cueto Event Management System January 2014

Adding and Subtracting Integers Unit. Grade 7 Math. 5 Days. Tools: Algebra Tiles. Four-Pan Algebra Balance. Playing Cards

Volunteer Manual CrossFit Games Regionals

Line and Polygon Clipping. Foley & Van Dam, Chapter 3

Pseudo code Tutorial and Exercises Teacher s Version

Introduction to Software Testing Chapter 8.1 Building Testing Tools Instrumentation. Chapter 8 Outline

Linear functions Increasing Linear Functions. Decreasing Linear Functions

Introduction to Computer Programming, Spring Term 2014 Practice Assignment 3 Discussion

OMBU ENTERPRISES, LLC. Process Metrics. 3 Forest Ave. Swanzey, NH Phone: Fax: OmbuEnterprises@msn.

Stacks. Linear data structures

(Refer Slide Time: 2:03)

Chapter 2: Elements of Java

Passing 1D arrays to functions.

MA 1125 Lecture 14 - Expected Values. Friday, February 28, Objectives: Introduce expected values.

Effects of changing slope or y-intercept

Roman Numerals. Symbol Decimal Value I One (1) V Five (5) X Ten (10) L Fifty (50) C Hundred (100) D Five Hundred (500) M Thousand (1000)

CS170 Lab 11 Abstract Data Types & Objects

CS177 MIDTERM 2 PRACTICE EXAM SOLUTION. Name: Student ID:

Boolean Expressions & the if Statement

Excel 2003: Ringtones Task

CS101 Lecture 26: Low Level Programming. John Magee 30 July 2013 Some material copyright Jones and Bartlett. Overview/Questions

Expressingbusiness checks -BCDL - Business Checks

Subtopics - Functions Function Declaration Function Arguments Return Statements and values. By Hardeep Singh

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

SUPERVISOR USER GUIDE

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

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

Digital Design. Assoc. Prof. Dr. Berna Örs Yalçın

Below is a diagram explaining the data packet and the timing related to the mouse clock while receiving a byte from the PS-2 mouse:

Math 3C Homework 3 Solutions

LINEAR INEQUALITIES. less than, < 2x + 5 x 3 less than or equal to, greater than, > 3x 2 x 6 greater than or equal to,

16. Recursion. COMP 110 Prasun Dewan 1. Developing a Recursive Solution

The Little Man Computer

Recursion. Slides. Programming in C++ Computer Science Dept Va Tech Aug., Barnette ND, McQuain WD

Part 1 Expressions, Equations, and Inequalities: Simplifying and Solving

Variables, Constants, and Data Types

INDUSTRIAL AUTOMATION Interactive Graphical SCADA System INSIGHT AND OVERVIEW. IGSS Online Training. Exercise 8: Creating Templates

Adding and Subtracting Fractions. 1. The denominator of a fraction names the fraction. It tells you how many equal parts something is divided into.

Building Java Programs

MEMORY WORK - MATH FACTS 1

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

External Sorting. Chapter 13. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1

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

Idle Data Analysis & Charts for the Draft 3 ENERGY STAR Computer Server Specification 10/31//08

Linear Programming Refining Transportation

Network Planning and Analysis

Green = 0,255,0 (Target Color for E.L. Gray Construction) CIELAB RGB Simulation Result for E.L. Gray Match (43,215,35) Equal Luminance Gray for Green

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

INTERPRETING THE ONE-WAY ANALYSIS OF VARIANCE (ANOVA)

Lesson 1: Readiness for Fractions (5 Bars and Fraction Bingo) Your group may select elements from one or both lesson plans.

Answers to Review Questions Chapter 7

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

Please Excuse My Dear Aunt Sally

HowThe Most Effective Way To Get Rich Fast!

PowerWorld Simulator

AC Measurements Using the Oscilloscope and Multimeter by Mr. David Fritz

Five daily lessons. Page 23. Page 25. Page 29. Pages 31

Training Manual. Pre-Employment Math. Version 1.1

External Sorting. Why Sort? 2-Way Sort: Requires 3 Buffers. Chapter 13

Building Java Programs

NCTM Strands. NCTM Strands. NCTM Strands. Data Analysis & Probability. Number and Operations Algebra Geometry Measurement. Data Analysis & Probability

Linear Programming Notes V Problem Transformations

Advertising Spillovers: Implications for Returns from Advertising

ALGORITHMS AND FLOWCHARTS

Transcription:

Pseudo Code Practice Problems: Listed below is a brief explanation of Pseudo code as well as a list of examples and solutions. Pseudo code Pseudo code can be broken down into five components. Variables: Assignment: Input/output: Selection: Repetition: A variable has a name, a data type, and a value. There is a location in memory associated with each variable. A variable can be called anything or be given any name. It is considered good practice to use variable names that are relevant to the task at hand. Assignment is the physical act of placing a value into a variable. Assignment can be shown using set = 5; set = num + set; The left side is the variable a value is being stored in and the right side is where the variable is being accessed. When a variable is assigned a value, the old value is written over with the new value so the old value is gone. x = 5 does not mean that x is equal to 5; it means set the variable x to have the value 5. Give x the value 5, make x equal to 5. Input / Output both deal with an outside source (can be a user or another program) receiving or giving information. An example would be assuming a fast food restaurant is a program. A driver (user) would submit their order for a burger and fries (input), they would then drive to the side window and pick up their ordered meal (output.) Output Write / display / print Input Read / get / input Selection construct allows for a choice between performing an action and skipping it. It is our conditional statements. Selection statements are written as such: if ( conditional statement) statement list statement list Repetition is a construct that allows instructions to be executed multiple times (IE repeated). In a repetition problem Count is initialized Tested incremented Repetition problems are shown as: while ( condition statement) statement list

Examples Example 1: Write pseudo code that reads two numbers and multiplies them together and print out their product. Example 2: Write pseudo code that tells a user that the number they entered is not a 5 or a 6. Example 3: Write pseudo code that performs the following: Ask a user to enter a number. If the number is between 0 and 10, write the word blue. If the number is between 10 and 20, write the word red. if the number is between 20 and 30, write the word green. If it is any other number, write that it is not a correct color option. Example 4: Write pseudo code to print all multiples of 5 between 1 and 100 (including both 1 and 100). Example 5: Write pseudo code that will count all the even numbers up to a user defined stopping point. Example 6: Write pseudo code that will perform the following. a) Read in 5 separate numbers. b) Calculate the average of the five numbers. c) Find the smallest (minimum) and largest (maximum) of the five entered numbers. d) Write out the results found from steps b and c with a message describing what they are Homework 1: Write pseudo code that reads in three numbers and writes them all in sorted order. Homework 2: Write pseudo code that will calculate a running sum. A user will enter numbers that will be added to the sum and when a negative number is encountered, stop adding numbers and write out the final result.

Solutions Example 1: Write pseudo code that reads two numbers and multiplies them together and print out their product. Pseudo code Ch code Read num1, num2 Set multi to num1*num2 Write multi int num1, num2, multi; cin>>num1>>num2; multi = num1 * num2; cout<<multi<<endl; Example 2: Write pseudo code that tells a user that the number they entered is not a 5 or a 6. Example 2 Solution 1: Read isfive If(isfive = 5) Write "your number is 5" if (isfive = 6) Write "your number is 6" Write "your number is not 5 or 6" Example 2 Solution 2: Read isfive If(isfive = 5 or isfive = 6) Write "your number is a 5 or 6" Write "your number is not 5 or 6" Example 2 Solution 3: Read isfive If(isfive is not 5 and isfive is not 6) Write "your number is not 5 or 6" int isfive; cin>> isfive; if(isfive == 5) { cout<<"your number is 5"; if(isfive == 6) { cout<<"your number is 6"; { cout<<"your number is not 5 or 6"; int isfive; cin>> isfive; if(isfive == 5 isfive == 6) { cout<<"your number is 5 or 6"; { cout<<"your number is not 5 or 6"; int isfive; cin>> isfive; if(isfive!= 5 && isfive!= 6) { cout<<"your number is not 5 or 6";

Example 3: Write pseudo code that performs the following: Ask a user to enter a number. If the number is between 0 and 10, write the word blue. If the number is between 10 and 20, write the word red. if the number is between 20 and 30, write the word green. If it is any other number, write that it is not a correct color option. Write "Please enter a number" Read colornum If (colornum >0 and colornum <= 10) Write blue If (colornum >0 and colornum <= 10) Write blue If (colornum >0 and colornum <= 10) Write blue Write "not a correct color option" int colornum; cout<<"please enter a number" cin>> colornum; if(colornum > 0 && colornum <= 10) { cout<<"blue"; if(colornum > 0 && colornum <= 10) { cout<<"blue"; if(colornum > 0 && colornum <= 10) { cout<<"blue"; { cout<<"not a correct color option" Example 4: Write pseudo code to print all multiples of 5 between 1 and 100 (including both 1 and 100). Set x to 1 While(x < 20) write x x = x*5 int x = 1; cin>>x; while(x < 20) { cout<<x; x = x*5;

Example 5: Write pseudo code that will count all the even numbers up to a user defined stopping point. For example, say we want to see the first 5 even numbers starting from 0. well, we know that evens numbers are 0, 2, 4, etc. The first 5 even numbers are 0, 2, 4, 6, 8. The first 8 even numbers are 0, 2, 4, 6, 8,10,12, 16 Example 5 solution 1: Read count Set x to 0; While(x < count) Set even to even + 2 x = x + 1 write even Example 5 solution 2: Read count Set x to 0; While(x < count) Set even to even + 2 x = x + 1 write even int x, count, even; x = 0; even = 0; cin>>count; while(x < count) { cout<<even; even = even+2; x = x+1; int x, count, even; cout<<"0"; x = 1; cin>>count; while(x < count) { cout<<x*2; x = x+1;

Example 6: Write pseudo code that will perform the following. a) Read in 5 separate numbers. b) Calculate the average of the five numbers. c) Find the smallest (minimum) and largest (maximum) of the five entered numbers. d) Write out the results found from steps b and c with a message describing what they are. Write "please enter 5 numbers" Read n1,n2,n3,n4,n5 Write "The average is" Set avg to (n1+n2+n3+n4+n5)/5 Write avg If(n1 < n2) Set max to n2 Set max to n1 If(n3 > max) Set max to n3 If(n4 > max) Set max to n4 If(n5 > max) Set max to n5 Write "The max is" Write max If(n1 > n2) Set min to n2 Set min to n1 If(n3 < min) Set min to n3 If(n4 < min) Set min to n4 If(n5 < min) Set min to n5 Write "The min is" Write min cout<<"please enter 5 numbers"; int n1,n2,n3,n4,n5; cin>>n1>>n2>>n3>>n4>>n5; int avg = (n1+n2+n3+n4+n5)/5; cout<<"the average is "<<avg; int min, max; if(n1<n2) max=n2; max=n1; if(n3>max) max=n3; if(n4>max) max=n4; if(n5>max) max=n5; cout<<"the max is "<<max; if(n1>n2) min=n2; min=n1; if(n3<min) min=n3; if(n4<min) min=n4; if(n5<min) min=n5; cout<<"the min is "<<min;

Homework 1: Write pseudo code that reads in three numbers and writes them all in sorted order. Homework 1 solution 1: Read num1, num2, num3 If (num1 < num2) If(num2 < num3) Write num1, num2, num3 If(num3 < num1) Write num3, num1, num2 Write num1, num3, num2 If(num1 < num3) Write num2, num1, num3 If(num3 < num2) Write num3, num2, num1 Write num2, num3, num1 int num1, num2, num3; cin>> num1>>num2>>num3; if (num1 < num2) { if(num2 < num3) { Cout<< num1<<" "<<num2<<" "<<num3; { if(num3 < num1) { Cout<< num3<<" "<<num2<<" "<<num2; { Cout<< num1<<" "<<num3<<" "<<num2; { If(num1 < num3) { Cout<< num2<<" "<<num1<<" "<<num3; { If(num3 < num2) { Cout<< num3<<" "<<num2<<" "<<num1; { Cout<< num2<<" "<<num3<<" "<<num1; Homework 1 solution 2: Read num1, num2, num3 If (num1 < num2 < num3) Write num1, num2, num3 If (num1 < num3 < num2) Write num1, num2, num3 If (num2 < num1 < num3) Write num2, num1, num3 If (num2 < num3 < num1) Write num2, num3, num1 If (num3 < num1 < num2) Write num3, num1, num2 If (num3 < num2< num1) Write num3, num2, num1 int num1, num2; cin>> num1>>num2>>num3; if(num1 < num2 && num2 < num3 && num1 < num3) { cout<<num1<<" "<<num2<<" "<<num3; if(num1 < num2 && num2 > num3 && num1 < num3) { cout<<num1<<" "<<num3<<" "<<num2; if(num1 > num2 && num2 < num3 && num1 < num3) { cout<<num2<<" "<<num1<<" "<<num3; if(num1 > num2 && num2 < num3 && num1 > num3) { cout<<num2<<" "<<num3<<" "<<num1; if(num1 < num2 && num2 > num3 && num1 > num3) { cout<<num3<<" "<<num1<<" "<<num2; if(num1 > num2 && num2 > num3 && num1 > num3) { cout<<num3<<" "<<num2<<" "<<num1;

Homework 2: Write pseudo code that will calculate a running sum. A user will enter numbers that will be added to the sum and when a negative number is encountered, stop adding numbers and write out the final result. Read x Set sum to 0; While(x >= 0) Set sum to x + sum Read x int x, sum; sum = 0; cin>>x; while(x >= 0) { sum = sum + x cin>>x;