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: Second quiz this Friday (29th) • Format likely similar to 1st quiz. If you have concerns please send them my way. • Homework #2 due Monday (June 1st) • Homework #3 likely to be assigned on Friday (29th) ADMINISTRATIVE STUFF • Homework #2: • We will go into detail of my post later today (with examples). • The basics: • When reading the hours, minutes, and seconds use %d not %i. • To display a particular number of characters use .<number> between % and the letter. As in %.2d. BUNCH OF STUFF WE HAVEN’T COVERED AKA Programming in C Potpurri 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 'x': printf(“Warning the recommended operator is * not x\n“); 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 'x': printf(“Warning the recommended operator is * not x\n“); case '*': result = operand1 * operand2; break; case '/': result = operand1 / operand2; break; default: printf(“Unknown operator %c\n“, operator); } printf("The result is: %.2f\n“, result); return 0; } TERNARY OPERATOR 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) ; REGARDING PARENTHESIS AND THE TERNARY OPERATOR • I said last week that confusing parenthesis wouldn’t be part of quizzes and I stand by that. • Given an exercise there will always be parenthesis around expressions to tell you what gets executed first, but you will need to be able to follow it. • For example: float result = (operator == ‘+’) ? (operand1 + operand2):(operand1 – operand2) ; float result = (op == ‘+’) ? (o1 + o2):(o1 – o2) ; Could also be: float result = (op == ‘+’) ? (o1 + o2) : ( (op == '-') ? (o1 – o2) : (o1 * o2) ) ; And I expect you to be able to follow the logic. ASCII TABLE • American Standard Code for Information Interchange (ASCII). • Starndard character-encoding scheme. • Not the only one! • By far the most common in C DIFFERENCE BETWEEN ++X AND X++ • Last week we covered the operators ++ and --. • The simple explanation was that x++ is roughly equivalent to x = x + 1; y-- is roughly equivalent to y = y – 1; • Turns out the expressions ++x and --y are also valid. • What’s the difference? • Let’s use it in a more complicated expression: int x = 3; int n = 5 % 3 – (x++) + 2; ++X VS X++ int x = 3; int n = 5 % 3 – (x++) + 2; • • • • • • • • 5%3=? 5%3=2 x++ = ? Could be x++ = 3 or x++ = 4. We only know that after the expression x will be 4 So n could be 2–3+3=2 or 2–4+3=1 Let’s test it. #include <stdio.h> int main(void) { int x = 3; int n = 5%3 – (x++) + 2; printf("x = %d\n", x); printf("n = %d\n", n); return 0; } #include <stdio.h> int main(void) { int x = 3; int n = 5%3 – (x++) + 2; printf("x = %d\n", x); //this should be x = 4 printf("n = %d\n", n); //what should this be? return 0; } #include <stdio.h> int main(void) { int x = 3; int n = 5%3 – (++x) + 2; printf("x = %d\n", x); //this should be x = 4 printf("n = %d\n", n); //what should this be? return 0; } TYPE SPECIFIERS So, can I have a infinitely large number stored in a variable of type int? Specifier long long long short unsigned signed SIZE IN MEMORY OF DIFFERENT TYPES • There is an operator called sizeof. • One parameter: a type. • Returns an unsigned value that uses the %zu value to printf. sizeof • For example: • sizeof(int); WHAT DO COMPUTERS DO BETTER THAN HUMANS? LOOPING • Repetitive stuff. • • • • • • • • Compute sum of many numbers Generate a list of powers of a number Work with an array of numbers Work with an array of strings Turn based games Display a video Video games Execute a program LOOPING • Three structures. Equivalent in computational power. • for statement for(<init statement> ; <loop condition> ; <loop expression>) <program statement> • while statement while(<loop condition>) <program statement> • Do-while statement do <program statement> while (<loop condition>); BETTER LOOPING – SERIOUSLY GUYS USE PROTECTION • Three structures. Equivalent in computational power. • for statement BRACKETS for(<init statement> ; <loop condition> ; <loop expression>) { <program statements> } • while statement while(<loop condition>) { <program statements> } • Do-while statement do { <program statements> }while (<loop condition>);