programming basics using real-life examples

28
Programming Basics using Real-life examples

Upload: anisa

Post on 23-Feb-2016

26 views

Category:

Documents


0 download

DESCRIPTION

Programming Basics using Real-life examples. Activities. Recipe Assembly instructions for a toy Map out the plan at amusement park A busy day schedule What is the common idea for all these activities?. Programming problem: Using sequence structure. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Programming Basics  using Real-life examples

Programming Basics using

Real-life examples

Page 2: Programming Basics  using Real-life examples

Activities

• Recipe• Assembly instructions for a toy• Map out the plan at amusement park• A busy day schedule

What is the common idea for all these activities?

Page 3: Programming Basics  using Real-life examples

Programming problem: Using sequence structure

• Compute the weighted score based on individual assignments’ scores. Let us say there are only 3 assignments & 2 exams, each with max score of 100. Respective weights are (10%, 10%, 10%, 35% and 35%)

• Sample input & output:Input: 100 100 100 95 95Output: 96.5%

Page 4: Programming Basics  using Real-life examples

Pseudocode

Prompt & get the score for assignment1Prompt & get the score for assignment2Prompt & get the score for assignment3Prompt & get the score for exam1Prompt & get the score for exam2weightedScore = (assignment1 + assignment2 + assignment3) * 0.1 + (exam1 + exam2) * .35output weightedScore

Page 5: Programming Basics  using Real-life examples

Activities

• Drive car or take DART bus?• Party or study?• Fly or drive?

What is the common idea for all these activities?

Page 6: Programming Basics  using Real-life examples

Programming problem:using decision structure

Get hourly pay rate & # of hours, compute the weekly pay, but do not pay for hours beyond 50.

Sample inputs:Input: Pay Rate

Input:Hours

Output

150 30 Rs. 4500

150 60 Rs. 7500

Page 7: Programming Basics  using Real-life examples

Pseudocode

Prompt & get hourly pay rate & # of hoursIF hours <= 50 pay = hours * payRate;ELSE pay = 50 * payRate;ENDIFoutput pay

Page 8: Programming Basics  using Real-life examples

C code

Prompt & get hourly pay rate & # of hours

if (hours <= 50) pay = hours * payRate;else pay = 50 * payRate;

output pay

Page 9: Programming Basics  using Real-life examples

Programming problem:using decision structure

V2: Get hourly pay rate & # of hours, compute the weekly pay, but do not pay for >50 hours. Also, pay 1.5 times regular pay for overtime hours (that is, # of hours beyond regular 40 hours).

First 40 hours: payRateNext 10 hours: payRate * 1.5Beyond 50 hours: 0

Page 10: Programming Basics  using Real-life examples

pseudocode

IF hours <= 40 pay = payRate * hours;ELSE IF hours <= 50 pay = payRate * 40 + payRate * 1.5 * (hours – 40);ELSE pay = payRate * 40 + payRate * 1.5 * 10;

Page 11: Programming Basics  using Real-life examples

pseudocode #2

overHours = hours – 40;IF hours <= 40 pay = payRate * hours;ELSE IF hours <= 50 pay = payRate * 40 + payRate * 1.5 * overHours;ELSE pay = payRate * 40 + payRate * 1.5 * 10;

Page 12: Programming Basics  using Real-life examples

pseudocode #3

hours = (hours > 50 ? 50 : hours);IF hours <= 40 pay = payRate * hours;ELSE pay = payRate * 40 + payRate * 1.5 * (hours – 40);

Page 13: Programming Basics  using Real-life examples

pseudocode #4

hours = (hours > 50 ? 50 : hours);IF hours <= 40 pay = payRate * hours;ELSE basePay = payRate * 40; overPay = payRate * 1.5 * (hours – 40); pay = basePay + overPay;

Page 14: Programming Basics  using Real-life examples

Activities

• Bring in tons of purchased items from car to house

• Load up uhaul truck when cleaning up apartment

• Eat cookies from a box • Taking an exam that has several questions

What is the common idea for all these activities?

Page 15: Programming Basics  using Real-life examples

Programming problem: Using repetition structure

Compute the average score for the whole class.

Page 16: Programming Basics  using Real-life examples

Are we ready to code it?

Page 17: Programming Basics  using Real-life examples

Guessing game

• Guess a number between 1 and 100 in your mind. Write a program so that the computer will ask you a series of questions and determine that number based on your answers.

Repeat the following steps as many times as needed:• Computer asks, “Is it NN?”• User responds with <, =, or >

Page 18: Programming Basics  using Real-life examples
Page 19: Programming Basics  using Real-life examples

Range – 2 variables: low = 1 and high = 100• compute mid = (low + high) / 2• Ask the user: Is it mid?• Get user response• adjust low or high based on response• repeat as needed

Pseudocode

Page 20: Programming Basics  using Real-life examples

Detailed pseudocode

Initialize range – 2 variables: low = 1 and high = 100do {

compute mid = (low + high) / 2Ask the user: Is it mid?Get user responseif (response == ‘<‘)

high = mid-1;else if (response == ‘>’)

low = mid+1;

while (response != ‘=‘);

Page 21: Programming Basics  using Real-life examples

Are we ready to code it?

Page 22: Programming Basics  using Real-life examples

Are we ready to code it?

• Google for “Java random number generation”

Page 23: Programming Basics  using Real-life examples

Guessing game V2 – Role reversal

• Let the computer guess a number between 1 and 100. Write a program so that the computer will answer a series of your questions and you will determine the number based on computer’s responses.

Page 24: Programming Basics  using Real-life examples

Pseudocode

• Generate a random number between 0 and 100: assign rand() % 101 to a variable.

• then enter the loop– get a guess from the user– output <, >, or =

repeat until user enters =

Page 25: Programming Basics  using Real-life examples

Are we ready to code it?

• How to make the computer guess a number?

Page 26: Programming Basics  using Real-life examples

Summary

• All programs have only 3 control structures: Sequence, decision & repetition

• Problem description High level idea Detailed Pseudocode Implement in specific language Executable program

Page 27: Programming Basics  using Real-life examples

C++ strings

• Similar functionality to hangman game• Write a method to return # of tries to guess all

the letters in a given word.• Sample run:Guess the letters in *******:sletters in s******:

Page 28: Programming Basics  using Real-life examples

Pseudocode for guessWord()• bool guessed[100]; initialize to false using loop• int exposedCount = 0;• int len = word.length();for(int i = 0; i < len ; i++)

guessed[i] = false;do { for(int i = 0; i < len ; i++)

cout << (guessed[i] ? word[i] : “*”); include code for getting next guess from the user and updating guessed[] array and exposedCount.} while (exposedCount < len);