1 midterm 1 review. 2 midterm 1 on friday february 27 closed book, closed notes no computer can be...

39
1 Midterm 1 Review

Upload: stanley-casey

Post on 20-Jan-2018

216 views

Category:

Documents


0 download

DESCRIPTION

3 C Numeric Data Types

TRANSCRIPT

Page 1: 1 Midterm 1 Review. 2 Midterm 1 on Friday February 27 Closed book, closed notes No computer can be used…

1

Midterm 1 Review

Page 2: 1 Midterm 1 Review. 2 Midterm 1 on Friday February 27 Closed book, closed notes No computer can be used…

2

Midterm 1 on Friday February 27 Closed book, closed notes No computer can be used 50 minutes 4 or 5 questions

write full programs write partial programs find output of a program

Page 3: 1 Midterm 1 Review. 2 Midterm 1 on Friday February 27 Closed book, closed notes No computer can be used…

3

C Numeric Data Types

Page 4: 1 Midterm 1 Review. 2 Midterm 1 on Friday February 27 Closed book, closed notes No computer can be used…

4

C Character Data Type: char

In memory, everything is stored as binary value, which can be interpreted as char or integer. Examples of ASCII Codes

char result =‘Y’;

Page 5: 1 Midterm 1 Review. 2 Midterm 1 on Friday February 27 Closed book, closed notes No computer can be used…

5

Precedence of Arithmetic Operators

Page 6: 1 Midterm 1 Review. 2 Midterm 1 on Friday February 27 Closed book, closed notes No computer can be used…

6

Standard Input and Output Output: printf Input: scanf

Remember the program computing the distance between two points!

/* Declare and initialize variables. */ double x1=1, y1=5, x2=4, y2=7, side_1, side_2, distance; How can we compute distance for different points? It would be better to get new points from user, right?

For this we will use scanf To use these functions, we need to use

#include <stdio.h>

Page 7: 1 Midterm 1 Review. 2 Midterm 1 on Friday February 27 Closed book, closed notes No computer can be used…

7

Standard Output printf Function

prints information to the screen requires two arguments

control string Contains text, conversion specifiers or both

Identifier to be printed Example

double angle = 45.5;printf(“Angle = %.2f degrees \n”, angle);

Output:Angle = 45.50 degrees

Control String

Identifier

Conversion Specifier

Page 8: 1 Midterm 1 Review. 2 Midterm 1 on Friday February 27 Closed book, closed notes No computer can be used…

8

Math Functionsfabs(x) Absolute value of x.

sqrt(x) Square root of x, where x>=0.

pow(x,y) Exponentiation, xy. Errors occur if x=0 and y<=0, or if x<0 and y is not an integer.

ceil(x) Rounds x to the nearest integer toward (infinity). Example, ceil(2.01) is equal to 3.

floor(x) Rounds x to the nearest integer toward - (negative infinity). Example, floor(2.01) is equal to 2.

exp(x) Computes the value of ex.

log(x) Returns ln x, the natural logarithm of x to the base e. Errors occur if x<=0.

log10(x) Returns log10x, logarithm of x to the base 10. Errors occur if x<=0.

Page 9: 1 Midterm 1 Review. 2 Midterm 1 on Friday February 27 Closed book, closed notes No computer can be used…

9

Relational Operators == equality (x == 3) != non equality (y != 0) < less than (x<y) > greater than (y>10) <= less than equal to (x<=0) >= greater than equal to (x>=y)

!!! a==b vs. a=b !!!

Page 10: 1 Midterm 1 Review. 2 Midterm 1 on Friday February 27 Closed book, closed notes No computer can be used…

10

Logical Operators ! not !(x==0) && and (x>=0) && (x<=10) || or (x>0) || (x<0)A B A && B A || B !A !BFalse False False False True TrueFalse True False True True FalseTrue False False True False TrueTrue True True True False False

Page 11: 1 Midterm 1 Review. 2 Midterm 1 on Friday February 27 Closed book, closed notes No computer can be used…

11

Precedence for Arithmetic, Relational, and Logical Operators

Page 12: 1 Midterm 1 Review. 2 Midterm 1 on Friday February 27 Closed book, closed notes No computer can be used…

12

If statement if(Boolean expression)

statement; //single statement if(Boolean expression) { //more than one statement

statement1;.statement n;

}

Page 13: 1 Midterm 1 Review. 2 Midterm 1 on Friday February 27 Closed book, closed notes No computer can be used…

13

if - else statement if(Boolean expression)

statement;else

statement; if(Boolean expression)

{statement block

}else{

statement block}

Page 14: 1 Midterm 1 Review. 2 Midterm 1 on Friday February 27 Closed book, closed notes No computer can be used…

14

Loop (repetition) statements while statement do while statement for statement

Page 15: 1 Midterm 1 Review. 2 Midterm 1 on Friday February 27 Closed book, closed notes No computer can be used…

15

while statement while(expression)

statement; while(expression)

{ statement;statement;

.}

x=1;while (x<5) x = x + 1;

x = 1;while (x<5){ x = x+1; printf(“X=“%d”,x);}

Page 16: 1 Midterm 1 Review. 2 Midterm 1 on Friday February 27 Closed book, closed notes No computer can be used…

16

do while do

statement;while(expression);

do{statement1;statement2;.} while(expression);

note - the expression is tested after the statement(s) are executed, so statements are executed at least once.

x=1;do x = x + 1;while (x<5);

x = 1;do{ x = x+1; printf(“X=“%d”,x);} while (x<5);

Page 17: 1 Midterm 1 Review. 2 Midterm 1 on Friday February 27 Closed book, closed notes No computer can be used…

17

for statement for(initialization; test; increment/decrement)

statement;

for(initialization; test; increment/decrement){statement;statement;.}

for (x=1; x<5; x++) printf(“x=%d\n”,x);

for (x=1; x<5; x++){ printf(“X=“%d\n”,x); printf(“X^2 = %d\n”,x*x);}

Page 18: 1 Midterm 1 Review. 2 Midterm 1 on Friday February 27 Closed book, closed notes No computer can be used…

18

for statement

initalizetest

increment/decrement

true

statement(s)statement(s)

Page 19: 1 Midterm 1 Review. 2 Midterm 1 on Friday February 27 Closed book, closed notes No computer can be used…

19

Data FilesEach data file must have a file pointer

file pointer must be defined FILE *sensor1; FILE *balloon;

file pointer must be associated with a specific file using the fopen function

sensor1 = fopen(“sensor1.dat”, “r”); balloon = fopen(“balloon.dat”, “w”);

Read information from file

Write information

to a file

Page 20: 1 Midterm 1 Review. 2 Midterm 1 on Friday February 27 Closed book, closed notes No computer can be used…

20

I/O Statements Input file - use fscanf instead of scanf

fscanf(sensor1, “%1f %lf”, &t, &motion);

Output file - use fprint instead of printf fprintf(balloon, “%f %f %f\n”, time,

height, velocity);

Page 21: 1 Midterm 1 Review. 2 Midterm 1 on Friday February 27 Closed book, closed notes No computer can be used…

21

Reading Data Files counter controlled loop

First line in file contains count for loop

end of file controlled loop When file is created EOF is inserted while loop feof(fileptr) > 0 when EOF reached

Page 22: 1 Midterm 1 Review. 2 Midterm 1 on Friday February 27 Closed book, closed notes No computer can be used…

22

End of file controlled loop#include <stdio.h>

int main(){ FILE *scorefile; int score;

scorefile = fopen("scores.txt","r");

while (feof(scorefile) <= 0) { fscanf(scorefile,"%d",&score); printf("%d\n",score); }

fclose(scorefile); return(0);}

File

567893248563

Available on webpage as chapter3e11.c

Available on webpage as scores.txt

Page 23: 1 Midterm 1 Review. 2 Midterm 1 on Friday February 27 Closed book, closed notes No computer can be used…

23

Functions Defined to

return a single value to the calling function

perform a task change the value of the function

arguments (call by reference)

Page 24: 1 Midterm 1 Review. 2 Midterm 1 on Friday February 27 Closed book, closed notes No computer can be used…

24

Function Format

return_type function_name (parameter_declarations)

{declarations;statements;

}

Page 25: 1 Midterm 1 Review. 2 Midterm 1 on Friday February 27 Closed book, closed notes No computer can be used…

25

Example - factorial function

int fact(int n){ int factres = 1; while(n>1) {

factres = factres*n;n--;

} return(factres);}

n!=n*(n-1)*…*1, 0! = 1 by definition

Return Type Function name

Parameter DeclarationsDeclarations

Statements

Page 26: 1 Midterm 1 Review. 2 Midterm 1 on Friday February 27 Closed book, closed notes No computer can be used…

26

Example - factorial function

int main(void){ int t= 5,s; s=fact(t)

return 0;}

5t

s

Page 27: 1 Midterm 1 Review. 2 Midterm 1 on Friday February 27 Closed book, closed notes No computer can be used…

27

Example - factorial function

int main(void){ int t= 5,s; s=fact(t)

return 0;}

int fact(int n){ int factres = 1;

return(factres);}

5t

s

n

factres

5

1

Page 28: 1 Midterm 1 Review. 2 Midterm 1 on Friday February 27 Closed book, closed notes No computer can be used…

28

Example - factorial function

int main(void){ int t= 5,s; s=fact(t)

return 0;}

int fact(int n){ int factres = 1;

return(factres);}

5t

s

n

factres

5

120 120

Page 29: 1 Midterm 1 Review. 2 Midterm 1 on Friday February 27 Closed book, closed notes No computer can be used…

29

Example - factorial function

int main(void){ int t= 5,s; s=fact(t)

return 0;}

5t

s120

Page 30: 1 Midterm 1 Review. 2 Midterm 1 on Friday February 27 Closed book, closed notes No computer can be used…

30

Arrays

Page 31: 1 Midterm 1 Review. 2 Midterm 1 on Friday February 27 Closed book, closed notes No computer can be used…

31

Definition and Initialization An array is defined using a declaration statement. data type array_name[size];

allocates memory for size elements subscript of first element is 0 subscript of last element is size-1 size must be a constant

Page 32: 1 Midterm 1 Review. 2 Midterm 1 on Friday February 27 Closed book, closed notes No computer can be used…

32

Exampleint list[5];

allocates memory for 5 integer variables subscript of first element is 0 subscript of last element is 4 C does not perform any bounds checking on

arrayslist[0]

list[1]list[2]list[3]

list[4]

Page 33: 1 Midterm 1 Review. 2 Midterm 1 on Friday February 27 Closed book, closed notes No computer can be used…

33

Initializing Arrays Arrays can be initialized at the time they

are declared. Examples:

double taxrate[3] ={0.15, 0.25, 0.3};char list[5] = {‘h’,’e’,’l’,’l’,’o’};double vector[100] = {0.0}; /*assigns zero to all 100

elements*/int s[] = {5,0,-5}; /*the size of a s is 3 */

Page 34: 1 Midterm 1 Review. 2 Midterm 1 on Friday February 27 Closed book, closed notes No computer can be used…

34

Assigning values to an arrayfor loops are often used to assign values to an array

Example:

int list[5], i;for(i=0; i<5; i++){

list[i] = i;}

list[0]

list[1]list[2]list[3]

list[4]

01234

Page 35: 1 Midterm 1 Review. 2 Midterm 1 on Friday February 27 Closed book, closed notes No computer can be used…

35

Exercise int data[100] Store random numbers [0,99) in data

for (i=0; i<100; i++) data[i] = rand() % 100;

Page 36: 1 Midterm 1 Review. 2 Midterm 1 on Friday February 27 Closed book, closed notes No computer can be used…

36

Function Arguments Individual elements of an array can be

passed as regular arguments. void donothing(int a, int b){…}int main(void){ /* Declare variables and functions */int array[5] = {1,2,3,4,5};

donothing(array[2], array[4]);..}

Page 37: 1 Midterm 1 Review. 2 Midterm 1 on Friday February 27 Closed book, closed notes No computer can be used…

37

Passing Arrays to Functions Arrays are always pass by reference

Modifications to the array are reflected to main program

The array name is the address of the first element The maximum size of the array must be specified at

the time the array is declared. The actual number of array elements that are used

will vary, so the actual size of the array is usually passed as another argument to the function

Page 38: 1 Midterm 1 Review. 2 Midterm 1 on Friday February 27 Closed book, closed notes No computer can be used…

38

Exercise

int maximum(int fdata[], int n){ int i, fmax; fmax = fdata[0]; for (i=0; i<n; i++) if (fdata[i] > fmax) fmax = fdata[i]; return(fmax);}

int data[100]Write a function to find maximum value in the array data

int main(){ int data[100],i, max;

for (i=0; i<100; i++) data[i] = rand() % 100; max = maximum(data,100); printf("Max = %d\n",max); return(0);}

Page 39: 1 Midterm 1 Review. 2 Midterm 1 on Friday February 27 Closed book, closed notes No computer can be used…

39

Exercisevoid print(int pdata[], int n){ int i; for (i=0; i<n; i++) printf("data[%d]=%d\n",i,pdata[i]); return;}void modify(int fdata[], int n){ int i; for (i=0; i<n; i++) fdata[i] = 1; return;}

What is the output of the following program?int main(){ int data[10];

for (i=0; i<10; i++) data[i] = rand() % 100; print(data,10); modify(data,10); print(data,10);

return(0);}