Programming basics using real life examples
Download
1 / 28

Programming Basics using Real-life examples - PowerPoint PPT Presentation


  • 145 Views
  • Uploaded on

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 ? Sequence. Programming problem: Using sequence structure.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Programming Basics using Real-life examples' - anka


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Programming basics using real life examples

Programming Basics usingReal-life examples


Activities
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?

    Sequence


Programming problem using sequence structure
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 95

    Output: 96.5%


Pseudocode
Pseudocode

Prompt & get the score for assignment1

Prompt & get the score for assignment2

Prompt & get the score for assignment3

Prompt & get the score for exam1

Prompt & get the score for exam2

weightedScore = (assignment1 + assignment2 + assignment3) * 0.1 + (exam1 + exam2) * .35

output weightedScore


Activities1
Activities

  • Drive car or take DART bus?

  • Party or study?

  • Fly or drive?

    What is the common idea for all these activities?

    Decision


Programming problem using if statement
Programming problem:using IF statement

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

Sample inputs:


Pseudocode1
Pseudocode

Prompt & get hourly pay rate & # of hours

IF hours <= 50

pay = hours * payRate;

ELSE

pay = 50 * payRate;

ENDIF

output pay


C code
C code

Prompt & get hourly pay rate & # of hours

if (hours <= 50)

pay = hours * payRate;

else

pay = 50 * payRate;

output pay


Programming problem using decision structure
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: payRate

Next 10 hours: payRate * 1.5

Beyond 50 hours: 0


Pseudocode2
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;


Pseudocode 2
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;


Pseudocode 3
pseudocode #3

IF hours > 50

hours= 50;

IF hours <= 40

pay = payRate * hours;

ELSE

pay = payRate * 40 + payRate * 1.5 * (hours – 40);


Pseudocode 4
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;


Activities2
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?


Programming problem using repetition structure
Programming problem: Using repetition structure

Compute the average score for the whole class.

Enter # of students: 5

Enter scores:

91

92

92

93

94

Average score is: 92.4



Guessing game
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 >


Pseudocode3
Pseudocode

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


Detailed pseudocode
Detailed pseudocode

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

do {

compute mid = (low + high) / 2

Ask the user: Is it mid?

Get user response

if (response == ‘<‘)

high = mid-1;

else if (response == ‘>’)

low = mid+1;

while (response != ‘=‘);



Are we ready to code it2
Are we ready to code it?

  • Google for “Java random number generation”


Guessing game v2 role reversal
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.


Pseudocode4
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 =


Are we ready to code it3
Are we ready to code it?

  • How to make the computer guess a number?


Summary
Summary

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

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


C strings
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 *******:

    s

    letters in s******:


Pseudocode for guessword
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);


ad