Follow
Publications: 88 | Followers: 0

Programming Basics using Real-life examples

Publish on Category: Birds 0

Programming BasicsusingReal-life examples
Activities
RecipeAssembly instructions for a toyMap out the plan at amusement parkA busy day scheduleWhat is the common idea for all these activities?
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%
Pseudocode
Prompt & get the score for assignment1Prompt & get the score forassignment2Prompt & get the score forassignment3Prompt & get the score forexam1Prompt & get the score forexam2weightedScore = (assignment1 + assignment2 + assignment3) * 0.1 + (exam1 + exam2) * .35output weightedScore
Activities
Drive car or take DART bus?Party or study?Fly or drive?What is the common idea for all these activities?
Programming problem:using decision structure
Get hourly pay rate & # of hours, compute the weekly pay, but do not payfor hours beyond 50.Sample inputs:
Pseudocode
Prompt & get hourlypay rate & # ofhoursIF hours <= 50pay =hours* payRate;ELSEpay = 50 * payRate;ENDIFoutput pay
C code
Prompt & get hourlypay rate & # ofhoursif (hours <= 50)pay =hours* payRate;elsepay = 50 * payRate;output pay
Programming problem:using decision structure
V2: Get hourly pay rate & # of hours, compute the weekly pay, but do not pay for &gt;50 hours. Also, pay 1.5 times regular pay for overtime hours (that is, # of hours beyond regular 40hours).First 40 hours: payRateNext 10 hours: payRate * 1.5Beyond 50 hours: 0
pseudocode
IF hours <= 40pay = payRate * hours;ELSEIF hours <= 50pay = payRate * 40 + payRate * 1.5 * (hours – 40);ELSEpay = payRate * 40 + payRate * 1.5 * 10;
pseudocode #2
overHours = hours – 40;IF hours <= 40pay = payRate * hours;ELSEIF hours <= 50pay = payRate * 40 + payRate * 1.5 * overHours;ELSEpay = payRate * 40 + payRate * 1.5 * 10;
pseudocode #3
hours = (hours > 50 ? 50 : hours);IF hours <= 40pay = payRate * hours;ELSEpay = payRate * 40 + payRate * 1.5 * (hours – 40);
pseudocode #4
hours = (hours > 50 ? 50 : hours);IF hours <= 40pay = payRate * hours;ELSEbasePay = payRate * 40;overPay = payRate * 1.5 * (hours – 40);pay = basePay + overPay;
Activities
Bring in tons of purchased items from car to houseLoad up uhaul truck when cleaning up apartmentEat cookies from a boxTaking an exam that has several questionsWhat is the common idea for all these activities?
Programming problem:Using repetition structure
Compute the averagescore for the whole class.
Are we ready to code it?
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 >
Range – 2 variables: low = 1 and high = 100compute mid = (low + high) / 2Ask the user: Is it mid?Get user responseadjust low or high based on responserepeat as needed
Pseudocode
Detailed pseudocode
Initialize range– 2 variables: low = 1 and high =100do {computemid = (low + high) / 2Ask the user: Is it mid?Get user responseif (response == ‘<‘)high = mid-1;else if (response == ‘>’)low = mid+1;while (response != ‘=‘);
Are we ready to code it?
Are we ready to code it?
Google for “Java random number generation”
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.
Pseudocode
Generate a random number between 0 and 100: assign rand() % 101 to a variable.then enter the loopget a guess from the useroutput <, >, or =repeat until user enters =
Are we ready to code it?
How to make the computer guess a number?
Summary
All programs have only 3 control structures: Sequence, decision & repetitionProblem description High level idea  DetailedPseudocode Implement in specific language  Executable program
C++ strings
Similar functionality to hangman gameWrite a method to return # of tries to guess all the letters in a given word.Sample run:Guess the letters in *******:sletters ins******:
Pseudocode for guessWord()
bool guessed[100]; initialize to false using loopint 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 userand updating guessed[] array and exposedCount.} while (exposedCount < len);

0

Embed

Share

Upload

Make amazing presentation for free
Programming Basics using Real-life examples