Presentation is loading. Please wait.

Presentation is loading. Please wait.

COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez

Similar presentations


Presentation on theme: "COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez"— Presentation transcript:

1 COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez djrg@cise.ufl.edudjrg@cise.ufl.edu http://cise.ufl.edu/~djrg/http://cise.ufl.edu/~djrg/ https://ufcprog2015.wordpress.com/

2 ADMINISTRATIVE STUFF Not so new rule: whenever you participate tell me your name! Reminder: Second quiz this Friday (29 th ) Format likely similar to 1 st quiz. If you have concerns please send them my way. Homework #2 due Monday (June 1 st ) Homework #3 likely to be assigned on Friday (29 th )

3 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. between % and the letter. As in %.2d.

4 BUNCH OF STUFF WE HAVEN’T COVERED AKA Programming in C Potpurri

5 SWITCH STATEMENTS switch( ) { case : break; case : break; … case : break; default: }

6 #include 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; }

7 #include 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; }

8 TERNARY OPERATOR - CONDITIONAL OPERATOR ? : ; 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) ;

9 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) ;

10 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.

11 ASCII TABLE American Standard Code for Information Interchange (ASCII). Starndard character-encoding scheme. Not the only one! By far the most common in C

12

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

14 ++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.

15 #include int main(void) { int x = 3; int n = 5%3 – (x++) + 2; printf("x = %d\n", x); printf("n = %d\n", n); return 0; }

16 #include 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; }

17 #include 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; }

18 TYPE SPECIFIERS Specifier long short unsigned signed So, can I have a infinitely large number stored in a variable of type int?

19 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);

20 WHAT DO COMPUTERS DO BETTER THAN HUMANS?

21 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

22 LOOPING Three structures. Equivalent in computational power. for statement for( ; ; ) while statement while( ) Do-while statement do while ( );

23 BETTER LOOPING – SERIOUSLY GUYS USE PROTECTION Three structures. Equivalent in computational power. for statement for( ; ; ) { } while statement while( ) { } Do-while statement do { }while ( ); BRACKETS BETTER LOOPING – SERIOUSLY GUYS USE PROTECTION


Download ppt "COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez"

Similar presentations


Ads by Google