/
COP 3275 COP 3275

COP 3275 - PowerPoint Presentation

alexa-scheidler
alexa-scheidler . @alexa-scheidler
Follow
397 views
Uploaded On 2016-12-02

COP 3275 - PPT Presentation

Computer Programming Using C Instructor Diego RiveraGutierrez djrgciseufledu httpciseufledudjrg httpsufcprog2015wordpresscom Administrative Stuff Not so new rule whenever you participate tell me your name ID: 495844

result operator operand1 statement operator result statement operand1 operand2 int printf program case break loop expression condition float amp

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "COP 3275" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

COP 3275 Computer Programming Using C

Instructor: Diego Rivera-Gutierrez

djrg@cise.ufl.edu

http://cise.ufl.edu/~djrg/

https://ufcprog2015.wordpress.com

/

Slide2

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

)Slide3

Administrative Stuff

Homework #2:

We will go into detail of my post later today

(with examples

).

T

he 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.Slide4

Bunch of stuff we haven’t covered

AKA Programming in C

PotpurriSlide5

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>

}Slide6

#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

;

}Slide7

#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

;

}Slide8

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

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

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

ASCII Table

American Standard Code for Information Interchange (ASCII).

Starndard

character-encoding scheme.

Not the only one!

By far the most common in CSlide12
Slide13

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

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

#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

;

}Slide16

#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

;

}Slide17

#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

;

}Slide18

Type Specifiers

Specifier

long

long

long

short

unsigned

signed

So, can I have a infinitely large number stored in a variable of type

int

?Slide19

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

);Slide20

What do computers do better than humans?Slide21

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 programSlide22

Looping

Three structures. Equivalent in computational power.

f

or 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>);Slide23

Better Looping – Seriously Guys use Protection

Three structures. Equivalent in computational power.

f

or statement

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

BRACKETS

Better Looping – Seriously Guys use

Protection