quiz2 2 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Quiz2-2 PowerPoint Presentation
Download Presentation
Quiz2-2

Loading in 2 Seconds...

play fullscreen
1 / 23

Quiz2-2 - PowerPoint PPT Presentation


  • 92 Views
  • Uploaded on

Quiz2-2. // Declare float variables price and cost, and integer variable quantity float price, cost;  int quantity; // Input price with an input PROMPT cout << “Enter the price: ”;  cin >> price;  // Input quantity with an input PROMPT cout << “Enter the quantity: ”;  cin >> quantity; 

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 'Quiz2-2' - yardan


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
quiz2 2
Quiz2-2

// Declare float variables price and cost, and integer variable quantity

float price, cost; 

int quantity;

// Input price with an input PROMPT

cout << “Enter the price: ”; 

cin >> price; 

// Input quantity with an input PROMPT

cout << “Enter the quantity: ”; 

cin >> quantity; 

// If price is positive and quantity is not negative,

// Compute cost, which is the product of price and quantity

// Display cost at the beginning of next line with message “The cost is ”

// Otherwise, display message “Invalid input!”

if (price > 0 && quantity >= 0)

{

cost = price * quantity; 

cout << endl << The cost is ” << cost;

}

else

cout << endl << “Invalid input!”;

sentinel controlled loop
Sentinel-Controlled Loop

Finding MAX of n numbers

//const int END_VALUE = -1; -1 is not a magic number!

const int LOW_LIMIT = 0;

const int HIGH_LIMIT = 60;

int score, max;

max = LOW_LIMIT; // Range is known

cin >> score; // Prime read

while (score != END_VALUE)

{

if (score >= LOW_LIMIT && score <= HIGH_LIMIT)

{

if (score > max)

max = score;

}

else

cout << “Invalid score!”;

cin >> score;

}

loop control variable lcv
Loop Control Variable (LCV)

while (score != -1)

{

}

  • LCV: score
  • When to exit a loop
  • Avoid infinite loops
four parts of loops
Four Parts of Loops
  • Loop Initialization (LCV and others)
  • Testing LCV
  • Loop Body
  • Updating LCV
while loop
While Loop

Initialization (LCV and others)

while (LogicalExpression)

(Testing LCV)

false

true

Do Work

Update LCV

Statement after loop

four parts of loops1
Four Parts of Loops

max = LOW_LIMIT; // Range is known

cin >> score; // Prime read

while (score != -1)

{

if (score >= LOW_LIMIT && score <= HIGH_LIMIT)

{

if (score > max)

max = score;

}

else

cout << “Invalid score!”;

cin >> score;

}

computing count
Computing Count

Range is not Known

int score, max, count;

cin >> score; // Prime Read

while (score != -1)

{

count ++; // count = count + 1;

if (count == 1) // first score

max = score;

else

{

if (score > max)

max = score;

}

cin >> score;

}

if (count == 0)

cout << “No scores!”;

else

cout << “The maximal score is ” << max;

// Does it run?

// Run time error!

// Uninitialized value: count!

computing count1
Computing Count

Range is not Known

int score, max, count;

count = 0;

cin >> score;

while (score != -1)

{

count ++;

if (count == 1)

max = score;

else

{

if (score > max)

max = score;

}

cin >> score;

}

if (count == 0)

cout << “No scores!”;

else

cout << “The maximal score is ” << max;

// Works even there is no score

trace in hic
Trace in HiC

HiCTrace.cpp

HiCTraceII.cpp

Window/Console

View/Status Window

Line/Column number

Break Point

Step Over

Reset

trace execution
Trace Execution

Range unknown

int score, max, count;

count = 0;

cin >> score;

while (score != -1)

{

count ++;

if (count == 1)

max = score;

else

{

if (score > max)

max = score;

}

cin >> score;

}

if (count == 0)

cout << “No scores!”;

else

cout << “The maximal score is ” << max;

Input Scores:

45 55 39 -1

score max count

? ? ?

0

45

1

45

55

2

55

39

3

-1

tracing
Tracing

Tracing

Input: 48 54 66 53 59 -1

score max

? 0

48

48

54

54

66

53

59

59

-1

How to do it?

One statement each line

Do not cross old values

Blank when no new value

WILL BE ON QUIZ

And Test 1

And Final!

Range Known

const int LOW_LIMIT = 0;

const int HIGH_LIMIT = 60;

int score, max = LOW_LIMIT;

cin >> score;

while (score != -1)

{

if (score < LOW_LIMIT ||

score > HIGH_LIMIT)

cout << “Invalid score: ”

<< score;

else

{

if (score > max)

max = score;

}

cin >> score;

}

checking input range
Checking Input Range

max = 0;

cin >> score;

while (score != -1)

{

if (score >= 0 && score <= 60)

{

if (score > max)

max = score;

else

cout << “Invalid score!”;

}

cin >> score;

}

//else is paired with the last if!

max = 0;

cin >> score;

while (score != -1)

{

if (score >= 0 && score <= 60)

if (score > max)

max = score;

else

cout << “Invalid score!”;

cin >> score;

}

// Is it correct?

// Which if is else paired with?

checking input range1
Checking Input Range

const int LOW_LIMIT = 0;

const int HIGH_LIMIT = 60;

max = LOW_LIMIT;

cin >> score; // Prime read

while (score != -1)

{

if (score >= LOW_LIMIT && score <= HIGH_LIMIT)

{

if (score > max)

max = score;

}

else

cout << “Invalid score!”;

cin >> score;

}

getting valid values
Getting Valid Values

Pseudo Code

Input a value

While the input value is out of range

Display a message

Input a new value

cin >> score;

while (score < 0 || score > 60)

cout << “Invalid score!”;

cin >> score;

Correct?

getting valid values1
Getting Valid Values

Pseudo Code

Input a value

While the input value is out of range

Display a message

Input a value

cin >> score;

while (score < 0 || score > 60)

{

cout << “Invalid score!”;

cin >> score;

}

How about the end value -1?

getting valid values2
Getting Valid Values

Pseudo Code

Input a value

While the input value is out of range and not -1

Display a message

Input a value

cin >> score;

while ((score < 0 || score > 60) && score != -1)

{

cout << “Invalid score!”;

cin >> score;

}

// Can we have

while (score < 0 || score > 60 && score != -1)

// NO! It’s the same as the following:

while (score < 0 || (score > 60 && score != -1))

getting valid scores
Getting Valid Scores

int score, max = LOW_LIMIT;

cout << “Enter a score: ”;

cin >> score;

while (score != END_VALUE && (score < LOW_LIMIT || score > HIGH_LIMIT))

{

cout << “Invalid score: ” << score;

cout << “Enter a score: ”;

cin >> score;

}

while (score != END_VALUE)

{

if (score > max)

max = score;

cout << “Enter a score: ”;

cin >> score;

while (score != END_VALUE && (score < LOW_LIMIT || score > HIGH_LIMIT))

{

cout << “Invalid score: ” << score;

cout << “Enter a score: ”;

cin >> score;

}

}

data type bool
Data Type bool

bool badScore;

// Boolean variable with two possible values: true or false

badScore = false; // lower case

cin >> score;

badScore = (score != END_VALUE &&

(score < LOW_LIMIT || score > HIGH_LIMIT));

while (badScore == true)

{

cout << “Invalid score: “ << score;

cin >> score;

badScore = (score != END_VALUE &&

(score < LOW_LIMIT || score > HIGH_LIMIT));

}

data type bool ii
Data Type bool (II)

bool badScore;

badScore = false;

cin >> score;

badScore = (score != END_VALUE &&

(score < LOW_LIMIT || score > HIGH_LIMIT))

// Change (badScore == true) to badScore

while (badScore)

{

cout << “Invalid score: “ << score;

cin >> score;

badScore = (score != END_VALUE &&

(score < LOW_LIMIT || score > HIGH_LIMIT))

}

event controlled loops
Event-Controlled Loops

Sentinel-Controlled Loops

Boolean-Variable-Controlled Loops

Others

schedule
Schedule

Quiz3-1

5 PM Today

Lab1

Thursday

program 1
Program 1

Due February 16

Do it yourself!

NONE Differences

Style

You could lose up to 5 points on style!

Programming Rules

Checking Style in Lab Thursday.

slide23
Where to define constants?

Before main()

#include <iostream>

using namespace std;

const float MAX_HOURS = 40.0;

const float OVERTIME = 1.5;

// Cannot compile!

const string prompt = “Enter the hours: ”;

int main()

{

float payRate, hours, wages;

string prompt = “Enter the hours: ”;

// Not Magic number!

cout << “Enter the rate: ”;

. . .

return 0;

}