COP 3725 Computer Programming Using C

advertisement
COP 3275
COMPUTER
PROGRAMMING
USING C
Instructor: Diego Rivera-Gutierrez
djrg@cise.ufl.edu http://cise.ufl.edu/~djrg/
https://ufcprog2015.wordpress.com/
ADMINISTRATIVE STUFF
• Not so new rule: whenever you participate tell me your
name!
• Reminder: First quiz will be on Friday (5/22). It will cover
everything we’ve covered until today! (Just what is
actually covered in class, extra slides not necessary)
• Do we have any runners in the class?
• This includes:
•
•
•
•
•
Treadmill
Outside
Long distance
Short distance
But no one who uses the ‘j’ word
HOMEWORK #2
• Pace calculator:
• Inputs:
• Race distance (char) (a. 5k, b. 10k, c. 15k, d. Half
marathon, e. Full marathon).
• Distance unit (char) (k for km, m for miles)
• Expected finish time (3 ints – hours, minutes, seconds)
(hh:mm:ss)
• Output
• Constant pace required to finish that race in that time
(using the selected unit)
• Due date: Monday June 1st (11:59pm)
• Distance conversions and expected format detailed on
the homework specification
• By Friday, we will have all the knowledge required to
code this program
Aprox
10k
ASSIGNMENT AND
OPERATIONS
• Sometimes we want to do a quick operation on a
variable and save the result in the variable itself.
• For example:
int a = 8;
a = a * 2; // get the double of 8.
• A quick shortcut for this is:
a *=
2; //same as a = a * 2;
• This also works for other operators!
ASSIGNMENT AND
OPERATIONS
• An even more specific common operation that has it’s
own operand is:
• Adding or subtracting 1 from a variable!
• For example:
int a = 8;
a = a + 1; // adding 1
•
Of course we could do:
a += 1; // adding 1
•
However an even easier way is
a++; // adding 1
• Same story for subtracting 1 (--).
MAKING DECISIONS
• Sometimes we need to make decisions based on inputs
and provide different output based on the decision.
• Examples:
•
•
•
•
•
Odd/Even program
Which one is the larger of two numbers.
Letter grade based on numeric grade
Others?
Homework #2!
• Decision making in C:
• if statement
• switch statement
• Conditional operator
IF STATEMENT
if(<condition>)
<program statement>
• When <condition> is true,
<program statement> is executed.
• What does <condition> looks like?
• We have relational operators!
Operator
Meaning
Example
==
Equal to
count == 10
!=
Not equal to
a != b
<
Less than
i<0
<=
Less than or equal to
i <= 100
>
Greater than
pointer > end
>=
Greater than or equal to
j >= 0
#include <stdio.h>
int main(void) {
int num = -1;
printf("Input a number");
scanf("%d", &num);
if(num % 2 == 0)
printf("The number is even.\n");
return 0;
}
#include <stdio.h>
int main(void) {
int num = -1;
printf("Input a number");
scanf("%d", &num);
if(num % 2 != 0)
printf("The number is odd.\n");
return 0;
}
#include <stdio.h>
int main(void) {
int num = -1;
printf("Input a number");
scanf("%d", &num);
if(num % 2 == 0)
printf("The number is even.\n");
if(num % 2 != 0)
printf("The number is odd.\n");
return 0;
}
ELSE
if(<condition>)
<program statement-1>
else
<program statement-2>
• Whenever the <condition> is true
<program statement-1> is executed
• Whenever the <condition> is false
<program-statement-2> is executed
#include <stdio.h>
int main(void) {
int num = -1;
printf("Input a number");
scanf("%d", &num);
if(num % 2 == 0)
printf("The number is even.\n");
else
// much more efficient than if(num % 2 != 0)
printf("The number is odd.\n");
return 0;
}
#include <stdio.h>
int main(void) {
int num = -1;
printf("Input a number");
scanf("%d", &num);
if(num % 2 == 0) {
printf("The number is even.\n");
} else {
printf("The number is odd.\n");
}
return 0;
}
#include <stdio.h>
int main(void) {
char a = '\0';
Why doesn’t this work??
char b = '\0';
printf("Input your first letter");
scanf("%c", &a);
printf("Input your second letter");
scanf("%c", &b);
if(a < b) {
printf("%c comes before %c.\n", a,b);
} else if(a == b) {
printf("You input %c twice!.\n", a);
} else {
printf("%c comes before %c.\n", b,a);
}
return 0;
}
(SIDENOTE) GOING
DEEPER INTO SCANF
• Spoiler alert: If you miss this part in class you will likely
struggle with HW2.
• Why didn’t our example work?
• Scanf doesn’t ignore the newline character that pressing
“Enter” generates.
• The second scanf takes that newline character, so how
do we ignore it?
• How to fix it. There is a function called getchar().
• getchar() consumes one character from the console. That
includes the newline character!
#include <stdio.h>
int main(void) {
char a = '\0';
char b = '\0';
printf("Input your first letter");
scanf("%c", &a); getchar();
printf("Input your second letter");
scanf("%c", &b); getchar();
if(a < b) {
printf("%c comes before %c.\n", a,b);
} else if(a == b) {
printf("You input %c twice!.\n", a);
} else {
printf("%c comes before %c.\n", b,a);
}
return 0;
}
GOING DEEPER INTO
SCANF
• Remember how I said we wouldn’t need to read more
than one input per line using scanf?
• I lied…
or at least I didn’t expect my HW idea to require it.
• Expected finish time (3 ints – hours, minutes, seconds)
(hh:mm:ss)
• So, how do we do that?
• Read two or more variables in a particular format in one
scanf?
• We add more characters in the string and more
parameters! (very similar to how we print more than one
variable using printf)
#include <stdio.h>
int main(void) {
char a = '\0';
char b = '\0';
//
//
printf("Input two letters separated by a hyphen: ");
scanf("%c-%c", &a, &b); getchar();
printf("Input your second letter");
scanf("%c", &b); getchar();
if(a < b) {
printf("%c comes before %c.\n", a,b);
} else if(a == b) {
printf("You input %c twice!.\n", a);
} else {
printf("%c comes before %c.\n", b,a);
}
return 0;
}
ADMINISTRATIVE STUFF
• We have the first Quiz today! (5/22)
• It will 11:40am. You are free to leave as soon as you are
done.
• This Monday (5/25) is Memorial Day! UF Holiday!
No class
• Second Quiz will still be (5/29)
(BACK TO CONDITIONALS)
COMPOUND RELATIONAL
TESTS
• Sometimes we want to check more than just one
condition.
• We already have some of that with <= (less than or
equal) and >= (greater than or equal)
COMPOUND
RELATIONAL TESTS
• Sometimes we want to check more than just one
condition.
• We already have some of that with <= (less than or
equal) and >= (greater than or equal)
• How can we test if a number is in a range?
• For example if a float grade is between 92 and 88 and is
therefore a B+.
(Such an strict instructor!)
• We use the logical AND (&&) and logical OR(||)
operators.
if( grade >= 88 && grade < 92)
printf("You got a B+\n");
NESTED IFS
Remember an if statement looks like this:
if(<condition>)
<program statement>
• Just happens that the if statement is also a program
statement. So we can have things that look like this:
if(<condition>)
if(<condition>)
<program statement>
• This is what we call a nested if! An if statement inside
another one.
NESTED IFS
• When? Why?
• Let’s go back to:
if(grade >= 88 && grade < 92)
printf("You got a B+\n");
• We could change it for:
if(grade >= 88)
if(grade < 92)
printf("You got a B+\n");
• Same result, the printf only happens if grade is both
greater than or equal to 88 but less than 92.
NESTED IFS
if(grade >= 88)
if(grade < 92)
printf("You got a B+\n");
• What happens if I want to do something else? Like
assign an A- or A?
if(grade >= 88)
if(grade < 92)
printf("You got a B+\n");
else
printf(“Either an A- or A, right?\n”);
NESTED IFS
• Curly brackets/parenthesis are your friends. Use them! The
previous ifs are the same as:
if(grade >= 88) {
if(grade < 92) {
printf("You got a B+\n");
} else {
printf(“Either an A- or A, for sure!\n”);
}
}else
<program statement>
But just in case, know how C works if they are not there.
[Confusing brackets will never be a part of my quizzes]
SWITCH STATEMENTS
• What happens when the variable of interest can take
multiple values and we need to do something different
for each one?
• For example, let’s take a simple two operand
calculator.
#include <stdio.h>
int main(void) {
float operand1 = 0.0f, operand2 = 0.0f, result = 0.0f;
char operator = '\0';
printf("Input the operation you want to evaluate: ");
scanf(“%f %c %f", &operand1, &operator, &operand2);
//
do the calculations here…
printf("The result is: %f\n“, result);
return 0;
}
#include <stdio.h>
int main(void) {
float operand1 = 0.0f, operand2 = 0.0f, result = 0.0f;
char operator = '\0';
printf("Input the operation you want to evaluate: ");
scanf(“%f %c %f", &operand1, &operator, &operand2);
//
with what we know, we could do:
if(operator == '+') {
result = operand1 + operand2;
} else if(operator == '-') {
result = operand1 + operand2;
} else if(operator == '*') {
result = operand1 * operand2;
} else if(operator == '/') {
result = operand1 / operand2;
}
printf("The result is: %f\n“, result);
return 0;
}
SWITCH STATEMENTS
switch(<expression>) {
case <value-1>:
<program-statement>
<program-statement>
break;
case <value-2>:
<program-statement>
<program-statement>
break;
…
…
case <value-n>:
<program-statement>
<program-statement>
break;
default:
<program-statement>
}
#include <stdio.h>
int main(void) {
float operand1 = 0.0f, operand2 = 0.0f, result = 0.0f;
char operator = '\0';
printf("Input the operation you want to evaluate: ");
scanf(“%f %c %f", &operand1, &operator, &operand2);
//
with what we know, we could do:
switch(operator) {
case '+':
result = operand1 + operand2;
break;
case '-':
result = operand1 - operand2;
break;
case '*':
result = operand1 * operand2;
break;
case '/':
result = operand1 / operand2;
break;
}
printf("The result is: %f\n“, result);
return 0;
}
#include <stdio.h>
int main(void) {
float operand1 = 0.0f, operand2 = 0.0f, result = 0.0f;
char operator = '\0';
printf("Input the operation you want to evaluate: ");
scanf(“%f %c %f", &operand1, &operator, &operand2);
//
with what we know, we could do:
switch(operator) {
case '+':
result = operand1 + operand2;
break;
case '-':
result = operand1 - operand2;
break;
case '*':
result = operand1 * operand2;
break;
case '/':
result = operand1 / operand2;
break;
default:
printf(“Unknown operator %c\n“, operator);
}
printf("The result is: %f\n“, result);
return 0;
}
#include <stdio.h>
int main(void) {
float operand1 = 0.0f, operand2 = 0.0f, result = 0.0f;
char operator = '\0';
printf("Input the operation you want to evaluate: ");
scanf(“%f %c %f", &operand1, &operator, &operand2);
//
with what we know, we could do:
switch(operator) {
case '+':
result = operand1 + operand2;
break;
case '-':
result = operand1 - operand2;
break;
case '*':
case 'x':
result = operand1 * operand2;
break;
case '/':
result = operand1 / operand2;
break;
default:
printf(“Unknown operator %c\n“, operator);
}
printf("The result is: %f\n“, result);
return 0;
}
#include <stdio.h>
int main(void) {
float operand1 = 0.0f, operand2 = 0.0f, result = 0.0f;
char operator = '\0';
printf("Input the operation you want to evaluate: ");
scanf(“%f %c %f", &operand1, &operator, &operand2);
//
with what we know, we could do:
switch(operator) {
case '+':
result = operand1 + operand2;
break;
case '-':
result = operand1 - operand2;
break;
case 'x':
printf(“Warning the recommended operator is * not x\n“);
case '*':
result = operand1 * operand2;
No
break;
break;
case '/':
result = operand1 / operand2;
break;
default:
printf(“Unknown operator %c\n“, operator);
}
printf("The result is: %f\n“, result);
return 0;
}
CONDITIONAL
OPERATOR
<condition>? <expression if true> : <expression if false>;
• This is useful to assign a variable a value for example:
float result = operator == ‘+’? operand1 + operand2 :
operand1 – operand2;
float result = (operator == ‘+’)?(operand1 + operand2)
: (operand1 – operand2) ;
Download