Presentation Material for Class Meeting #6 on Feb 4

Download Report

Transcript Presentation Material for Class Meeting #6 on Feb 4

CS 108 Computing Fundamentals
Notes for Thursday, February 4, 2016
GHP #6 and #7 Review
• Let's go to our GHP Web page and look at samples that were
taken from among those of you who submitted (some of you
will recognize you work)
Relational Operators
Operator
==
!=
<
<=
>
>=
Meaning
Equal to
Not Equal to
Less Then
Less Then or Equal to
Greater Then
Greater Then or Equal to
• Note difference between = and = =
Relational Operators
The variable choice is assigned the
value of 2... 2 is not 0 therefore this
condition is ALWAYS evaluated as
TRUE (never FALSE)
Example :
if ( choice = 2)
{
answer = perimeter_cal (base, height);
printf(" The perimeter is %f ", answer );
}
Note the condition above is incorrect… compare:
if ( choice = = 2)
{
answer = perimeter_cal (base, height);
printf(" The perimeter is %f ", answer );
}
Relational Operator Demo (1)
Let’s develop a short program that prompts the user to enter two
numbers, and once the numbers are entered the program
determines (tests) which of the two numbers is larger. After the
determination is made (as to which is larger), a message is printed
to the screen indicating the result of the test.
Start with an algorithm!!
A Possible Algorithm, Not "The" Algorithm
1.
2.
3.
4.
1.
5.
Prompt user to provide a number (num1)
Record the user's input for num1
Prompt user to provide another number (num2)
Record user's input for num2
Test to see if the the numbers are unequal (note: requires a "test")
a. If test is true
i. Test to see if num1 is greater than num2 (note: requires a "test")
1) If test is true
a) Inform the user that num1 is greater than num2
b) Go to step 6.
2) If test is false (note: does not require a "test")
a) Inform the user that num2 is greater than num1
b) Go to step 6.
b. If test is false (note: does not require a "test")
i. Inform the user that num1 and num2 are equal
ii. Go to step 6.
Terminate
Now Lets Code It!!
Relational Operator Demo (2)
#include <stdio.h> // My file 1.c… also demonstrates "nested"
int main (void)
// if statements just to do it
{
float num1 = 0.0 , num2 = 0.0 ;
printf( "\n Enter a number: " );
scanf( "%f" , &num1 );
printf( "\n Enter a second number: " );
scanf( "%f" , &num2 );
if ( num1 != num2 )
{
if ( num1 > num2 )
printf("\n The first number s greater than the second.\n\n\n");
else
printf("\n The second number is greater than the first.\n\n\n");
}
else
printf("\n The numbers you entered are equal.\n\n\n");
return 0;
}
Relational Operator Demo (3)
#include <stdio.h> // My file 1a.c… condition evaluation issues
int main (void)
{
float num1 = 0.0 , num2 = 0.0 ;
printf( "\n Enter a number: " );
scanf( "%f" , &num1 );
printf( "\n Enter a second number: " );
scanf( "%f" , &num2) ;
if ( 0 )
// After running add a ! before ( 0 ) and 0
{
// and rerun
if (num1 > num2)
printf("\n The first number s greater than the second.\n\n\n");
else
printf("\n The second number is greater than the first.\n\n\n");
}
else
printf("\n The numbers you entered are equal.\n\n\n");
return 0;
}
Relational Operator Demo (4)
#include <stdio.h>
// My file 1b.c… demonstration of
// condition evaluation resultant values
int main (void)
{
float answer = 0.0 ;
answer = 1 < 4 && 4 < 7 ;
printf("\n\n Answer: %f " , answer ) ;
answer = ! (1 < 3) || ( 2 < 4 ) ;
printf("\n\n Answer: %f " , answer ) ;
answer = ! ( (1 < 3) || ( 2 < 4 ) ) ;
printf("\n\n Answer: %f " , answer ) ;
answer = ! (372) ;
printf("\n\n Answer: %f \n\n\n" , answer ) ;
return 0;
}
Relational Operator Demo (5)
#include <stdio.h>
int main (void)
{
int answer = 0 ;
// My file 1c.c… demonstration of
// condition evaluation resultant values
// using data type int
// The result of all relational and Boolean
// logical operations is always an int
answer = 1 < 4 && 4 < 7 ;
printf("\n\n Answer: %d " , answer ) ;
answer = ! (1 < 3) || ( 2 < 4 ) ;
printf("\n\n Answer: %d " , answer ) ;
answer = ! ( (1 < 3) || ( 2 < 4 ) ) ;
printf("\n\n Answer: %d " , answer ) ;
answer = ! (372) ;
printf("\n\n Answer: %d \n\n\n" , answer ) ;
return 0;
}
Logical Operators
Operator
!
&&
||
Condition A ! (NOT)
true
false
false
true
Meaning
NOT
(Highest Priority)
AND
OR
(Lowest Priority)
Condition A Condition B &&(AND)
true
true
false
false
true
false
true
false
true
false
false
false
||(OR)
true
true
true
false
Compound Logical Expressions
Write an if statement that doubles a number if the
number is greater than 0 yet does not exceed 25, triples
the number if it’s greater than 25 but less than 50, and
assigns the value 0 if the previous conditions aren’t met.
Hint: use the if … else if … else construction
Compound Logical Expressions
if ( (entry > 0) && (entry <= 25) )
{
entry = entry * 2;
}
else if ( (entry > 25) && (entry < 50) )
{
entry = entry * 3;
{
else
{
entry = 0;
}
Compound Logical Expression Demo (1)
Let’s develop a program that transforms a numeric
grades entered by the user into a letter grade and then
displays that letter grade to the screen.
What do we do first?
A Possible Algorithm, Not "The" Algorithm
1.
2.
3.
4.
5.
6.
7.
8.
Prompt user to enter a test score
Record user's input number for test score
Test the test score to see if it is less than 65
i. If test is true
a.
Display a message indicating the grade earned is F
b.
Go to step 8
Test the test score to see if it is greater than or equal to 65 but less than 70
i. If test is true
a.
Display a message indicating the grade earned is D
b.
Go to step 8
Test the test score to see if it is greater than or equal to 70 but less than 80
i. If test is true
a.
Display a message indicating the grade earned is C
b.
Go to step 8
Test the test score to see if it is greater than or equal to 80 but less than 90
i. If test is true
a.
Display a message indicating the grade earned is B
b.
Go to step 8
Display a message indicating the grade earned is A (note: no test required)
Terminate
Now Lets Code It!!
Compound Logical Expression Demo (2)
#include <stdio.h>
int main (void)
{
float score = 0.0 ;
printf( "\n Enter your test score: " );
scanf( "%f" , &score ) ;
if (score < 65)
{
printf("\n You earned an F.\n\n");
}
else if ( (score >= 65) && (score < 70) )
printf("\n You earned a D.\n\n");
else if ( (score >= 70) && (score < 80) )
printf("\n You earned a C.\n\n");
else if ( (score >= 80) && (score < 90) )
printf("\n You earned a B.\n\n");
else
printf("\n You earned an A.\n\n");
return 0;
}
//My file 2.c
// Braces
// No Braces… why
switch … case Statement.
• Simple if has only one action part, and if … else
statement has only two action parts... else if can be
messy sometimes.
• Often in life three or more cases of actions can be used,
based on the condition.
• Use either nested else if statements or use a switch …
case statement
switch … case Statement.
switch (decision criteria or condition)
{
case Label_1 :
Action Block 1;
// Decision criteria must be
break;
// data type int/char only.
case Label2 :
// Labels must be integer
Action Block 2;
// constants only.
break;
// The ASCII helps here…
…
// characters are included as
default : // catch-all // integer constants
Action Block else;
break;
}
switch … case Statement.
expression
Label 1
Action Block 1
Label 2
Action Block 2
Label n
Action Block n
default
Action Default
Let’s See a switch Statement Example
#include <stdio.h>
/* Let’s calculate a worker’s pay… 3.c*/
int main ( void )
{
int status = 0; double hours = 0.0; double pay = 0.0;
printf("\n Enter the hours worked : "); scanf( "%lf" , &hours );
printf("\n Enter the status (7 for full-timers and 9 for part-timers): ");
scanf( "%d" , &status );
switch (status)
{
case 7:
pay = hours * 15.0; // full-time rate is $15 per hour
break;
case 9:
pay = hours * 10.0; // part-time rate is $10 per hour
break;
default:
printf("\n\n\n >>>Wrong Employment Status<<<\n");
break;
}
printf("\n The payment amount is: %4.2lf\n\n\n", pay);
return ( 0 );
}
Remember: Steps to Build a Program
• What does it need to do (functionality): algorithm
• Can it be broken down into simpler pieces
• Code the simpler pieces
–
Code and test one piece at a time
• Integrate, test and fix
• Finished
• Start with a complete algorithm before writing one line of code
• Comment as you go
• "Urban Inside-Out Method"…one step at a time
Pico Shortcuts
• ctrl-w followed by ctrl-t produces a prompt that asks
you on which line you like the cursor to be positioned
• ctrl-c produces the number of the line on which the
cursor is currently positioned
• ctrl-y moves the cursor up one page
• ctrl-v moves the cursor down one page
Exam #1 (1)
• Tuesday, February 16, 2016
• E-Book chapters 1, 2, and 3, hardcopy textbook 1, 2, and
4, plus UNIX/the UNIX tutorial only for closed-book
portion… up through and including GHP #7 for open-book
part
• Use "Quick Check Exercises" and "Review Questions"
• Use Online Interactive Tests at the following link to
prepare:
http://ftp.tuwien.ac.at/languages/c/programmingbbrown/onlinet.htm
Exam #1 (2)
• If you need to take the exam in the Learning Center
– Send me an e-mail reminding me
– Visit the Learning Center ASAP to make sure they are ready
for you
• On Thursday, February 11, 2016 I will collect official
paperwork for accommodations
• Last semester’s Exam #1 will be distributed on Tuesday, February
9, 2016 to be completed as homework (GHP #9)
– We will review the answers on Thursday, February 11
– Do not make the mistake of thinking that your Exam #1 will
be a copy of last semester's Exam #1
I'm showing you last semester's exam to give you a flavor
of what could be on the exam and to give you an idea of
what an exam looks like and what to expect (generally)
GHP #8
• Because it's Super Bowl weekend, the homework is
pretty easy
– Take your submission for GHP #7 as the starting
point
Fix the algorithm (if necessary)
Make adjustments/improvements (if necessary)
Replace the if / else if / else construction with the
switch / case construction
GHP #7 Help
• For those who did not complete GHP #7
– I'm available now!!
– I'm the best tutor for CS 108 on the campus.