+ All Categories
Home > Documents > Week12 - Clark Science Center · 2019-11-25 · Exercise • Write a program that reads the file...

Week12 - Clark Science Center · 2019-11-25 · Exercise • Write a program that reads the file...

Date post: 31-Jul-2020
Category:
Upload: others
View: 0 times
Download: 0 times
Share this document with a friend
52
mith College Computer Science Dominique Thiébaut [email protected] CSC231 Week 12— Introduction to C Fall 2019
Transcript
Page 1: Week12 - Clark Science Center · 2019-11-25 · Exercise • Write a program that reads the file data.txt, which has the number of ints it contains on the first line, then one int

mith College

Computer Science

Dominique Thiébaut [email protected]

CSC231Week 12— Introduction to C

Fall 2019

Page 2: Week12 - Clark Science Center · 2019-11-25 · Exercise • Write a program that reads the file data.txt, which has the number of ints it contains on the first line, then one int

Pointers

Computer Science Dominique Thiebaut

Page 3: Week12 - Clark Science Center · 2019-11-25 · Exercise • Write a program that reads the file data.txt, which has the number of ints it contains on the first line, then one int

Concept

Computer Science Dominique Thiebaut

x6.5

0FFF1F0A

px

RAM

Page 4: Week12 - Clark Science Center · 2019-11-25 · Exercise • Write a program that reads the file data.txt, which has the number of ints it contains on the first line, then one int

Concept

Computer Science Dominique Thiebaut

x6.5

0FFF1F0A

px

RAM

Page 5: Week12 - Clark Science Center · 2019-11-25 · Exercise • Write a program that reads the file data.txt, which has the number of ints it contains on the first line, then one int

Example: Initialize an Array

Computer Science Dominique Thiebaut

Using

indexing

3 4 1 9 3 4 6 6 1 0

0 1 2 3 4 5 6 7 8 9

A

Page 6: Week12 - Clark Science Center · 2019-11-25 · Exercise • Write a program that reads the file data.txt, which has the number of ints it contains on the first line, then one int

Example: Initialize an Array

Computer Science Dominique Thiebaut

Using

indexing

Page 7: Week12 - Clark Science Center · 2019-11-25 · Exercise • Write a program that reads the file data.txt, which has the number of ints it contains on the first line, then one int

Example: Initialize an Array

Computer Science Dominique Thiebaut

3 4 1 9 3 4 6 6 1 0

0 1 2 3 4 5 6 7 8 9

A

Using

pointers

Page 8: Week12 - Clark Science Center · 2019-11-25 · Exercise • Write a program that reads the file data.txt, which has the number of ints it contains on the first line, then one int

Computer Science Dominique Thiebaut

Using

pointers

Example: Initialize an Array

Page 9: Week12 - Clark Science Center · 2019-11-25 · Exercise • Write a program that reads the file data.txt, which has the number of ints it contains on the first line, then one int

./a.outp=0x7fff88d54560 A[0] = 0.00 *p = 0.00p=0x7fff88d54564 A[1] = 1.00 *p = 1.00p=0x7fff88d54568 A[2] = 2.00 *p = 2.00p=0x7fff88d5456c A[3] = 3.00 *p = 3.00p=0x7fff88d54570 A[4] = 4.00 *p = 4.00p=0x7fff88d54574 A[5] = 5.00 *p = 5.00p=0x7fff88d54578 A[6] = 6.00 *p = 6.00p=0x7fff88d5457c A[7] = 7.00 *p = 7.00p=0x7fff88d54580 A[8] = 8.00 *p = 8.00p=0x7fff88d54584 A[9] = 9.00 *p = 9.00

Computer Science Dominique Thiebaut

Page 10: Week12 - Clark Science Center · 2019-11-25 · Exercise • Write a program that reads the file data.txt, which has the number of ints it contains on the first line, then one int

We Stopped HereLast Time

https://farm1.staticflickr.com/44/145211107_f2020ec804_z.jpg

We Stopped HereLast Time

Page 11: Week12 - Clark Science Center · 2019-11-25 · Exercise • Write a program that reads the file data.txt, which has the number of ints it contains on the first line, then one int

Why is fib3() soslow?

https://visualgo.net/en/recursion

(see

fib3.c

on marrax)

Page 12: Week12 - Clark Science Center · 2019-11-25 · Exercise • Write a program that reads the file data.txt, which has the number of ints it contains on the first line, then one int

https://visualgo.net/en/recursion

fib3 tree

Page 13: Week12 - Clark Science Center · 2019-11-25 · Exercise • Write a program that reads the file data.txt, which has the number of ints it contains on the first line, then one int

Outline• Pointers

• Array

• Functions

• Input/Output (IO)

• Dynamic Variables

• File IO

• Multi-File Projects

Page 14: Week12 - Clark Science Center · 2019-11-25 · Exercise • Write a program that reads the file data.txt, which has the number of ints it contains on the first line, then one int

Pointers: Quick Review

Computer Science Dominique Thiebaut

x6.5

0FFF1F0A

px

RAM

Page 15: Week12 - Clark Science Center · 2019-11-25 · Exercise • Write a program that reads the file data.txt, which has the number of ints it contains on the first line, then one int

Arrays

Computer Science Dominique Thiebaut

v[0]

6.5

0FFF1F00

pv

pv+1

pv+2

pv+3

v[1]

v[2]

v[3]

0FFF1F04

0FFF1F08

0FFF1F0C

0FFF1F10

Page 16: Week12 - Clark Science Center · 2019-11-25 · Exercise • Write a program that reads the file data.txt, which has the number of ints it contains on the first line, then one int

Arrays

• The name of an array is a pointer to the first cell of the array.

• name is the same as &(name[0])

Computer Science Dominique Thiebaut

Page 17: Week12 - Clark Science Center · 2019-11-25 · Exercise • Write a program that reads the file data.txt, which has the number of ints it contains on the first line, then one int

* and &• * has two meanings, depending on context

• “Pointer to”

• “Contents of”

• Similarly, & means “the address of” or the “binary and”-operator

Computer Science Dominique Thiebaut

Page 18: Week12 - Clark Science Center · 2019-11-25 · Exercise • Write a program that reads the file data.txt, which has the number of ints it contains on the first line, then one int

* and &

Computer Science Dominique Thiebaut

Page 19: Week12 - Clark Science Center · 2019-11-25 · Exercise • Write a program that reads the file data.txt, which has the number of ints it contains on the first line, then one int

Computer Science Dominique Thiebaut

Exercise• Write a C program that copies Array A into Array B using indexing first, and then using pointers.

getcopy C/copyArrays.c

Page 20: Week12 - Clark Science Center · 2019-11-25 · Exercise • Write a program that reads the file data.txt, which has the number of ints it contains on the first line, then one int

Exercise• Write a C program that finds the largest integer in Array A, using pointers.

Computer Science Dominique Thiebaut

Page 21: Week12 - Clark Science Center · 2019-11-25 · Exercise • Write a program that reads the file data.txt, which has the number of ints it contains on the first line, then one int

Functions

Computer Science Dominique Thiebaut

Page 22: Week12 - Clark Science Center · 2019-11-25 · Exercise • Write a program that reads the file data.txt, which has the number of ints it contains on the first line, then one int

Functions

• Functions are normally declared before they are used

• Functions can return values of simple types (int, char, floats), and even pointers.

• Functions get parameters of simple types, and pointers.

• Passing by value is automatic. Passing by reference requires passing a pointer.

Computer Science Dominique Thiebaut

Page 23: Week12 - Clark Science Center · 2019-11-25 · Exercise • Write a program that reads the file data.txt, which has the number of ints it contains on the first line, then one int

Back to Assembly…x dd 3

push dword[x] call f1 …

f1: push ebb mov ebp, esp inc dword[ebp+8] … ret 4

Page 24: Week12 - Clark Science Center · 2019-11-25 · Exercise • Write a program that reads the file data.txt, which has the number of ints it contains on the first line, then one int

z = 30z = 11sum( 11, 22) = 33

Computer Science Dominique Thiebaut

Example 1

Page 25: Week12 - Clark Science Center · 2019-11-25 · Exercise • Write a program that reads the file data.txt, which has the number of ints it contains on the first line, then one int

z = 30x = 11

Example 2 (Incomplete code… Add missing elements!)

Computer Science Dominique Thiebaut

Page 26: Week12 - Clark Science Center · 2019-11-25 · Exercise • Write a program that reads the file data.txt, which has the number of ints it contains on the first line, then one int

z = 30x = 11

Example 2 (Incomplete code… Add missing elements!)

Computer Science Dominique Thiebaut

Pass

by Reference!

Page 27: Week12 - Clark Science Center · 2019-11-25 · Exercise • Write a program that reads the file data.txt, which has the number of ints it contains on the first line, then one int

Complete the code

shown here

Computer Science Dominique Thiebaut

getcopy C/decrementArray.c

Exercise

Page 28: Week12 - Clark Science Center · 2019-11-25 · Exercise • Write a program that reads the file data.txt, which has the number of ints it contains on the first line, then one int

Exercise

Computer Science Dominique Thiebaut

• Write a C program that uses functions to find the largest integer in A. The result is passed back using a return statement.

Page 29: Week12 - Clark Science Center · 2019-11-25 · Exercise • Write a program that reads the file data.txt, which has the number of ints it contains on the first line, then one int

Exercise

• Write another program that does the same thing but the max is passed back by reference.

Computer Science Dominique Thiebaut

Page 30: Week12 - Clark Science Center · 2019-11-25 · Exercise • Write a program that reads the file data.txt, which has the number of ints it contains on the first line, then one int

Input/OutputIO

Page 31: Week12 - Clark Science Center · 2019-11-25 · Exercise • Write a program that reads the file data.txt, which has the number of ints it contains on the first line, then one int

Input From User

Computer Science Dominique Thiebaut

Page 32: Week12 - Clark Science Center · 2019-11-25 · Exercise • Write a program that reads the file data.txt, which has the number of ints it contains on the first line, then one int

(cont’d)

a.outEnter your name, please: MickeyEnter your age: 21Enter your version of pi: 3.14159Mickey is 21 years old, and thinks pi is 3.1415901184

Computer Science Dominique Thiebaut

Page 33: Week12 - Clark Science Center · 2019-11-25 · Exercise • Write a program that reads the file data.txt, which has the number of ints it contains on the first line, then one int

Dynamic Variables

Computer Science Dominique Thiebaut

Page 34: Week12 - Clark Science Center · 2019-11-25 · Exercise • Write a program that reads the file data.txt, which has the number of ints it contains on the first line, then one int

• Dynamic: think "new" in Java

• malloc() = Memore Allocation for new data structure

Computer Science Dominique Thiebaut

Page 35: Week12 - Clark Science Center · 2019-11-25 · Exercise • Write a program that reads the file data.txt, which has the number of ints it contains on the first line, then one int

RAM

Computer Science Dominique Thiebaut

user

pro

g

malloc()

Page 36: Week12 - Clark Science Center · 2019-11-25 · Exercise • Write a program that reads the file data.txt, which has the number of ints it contains on the first line, then one int

user

pro

g

malloc()

RAM

Computer Science Dominique Thiebaut

OperatingSystem

Page 37: Week12 - Clark Science Center · 2019-11-25 · Exercise • Write a program that reads the file data.txt, which has the number of ints it contains on the first line, then one int

RAM

Computer Science Dominique Thiebaut

OperatingSystem

dyn

user

pro

g

malloc()

Page 38: Week12 - Clark Science Center · 2019-11-25 · Exercise • Write a program that reads the file data.txt, which has the number of ints it contains on the first line, then one int

RAM

Computer Science Dominique Thiebaut

OperatingSystem

dyn

(void*) p

user

pro

g

malloc()

Page 39: Week12 - Clark Science Center · 2019-11-25 · Exercise • Write a program that reads the file data.txt, which has the number of ints it contains on the first line, then one int

RAM

Computer Science Dominique Thiebaut

OperatingSystem

dyn

(void*) p

p

user

pro

g

malloc()

Page 40: Week12 - Clark Science Center · 2019-11-25 · Exercise • Write a program that reads the file data.txt, which has the number of ints it contains on the first line, then one int

Computer Science Dominique Thiebaut

getcopy C/malloc1.c

Malloc

Page 41: Week12 - Clark Science Center · 2019-11-25 · Exercise • Write a program that reads the file data.txt, which has the number of ints it contains on the first line, then one int

Exercise• Modify the previous program that computed the

max of an array, but this time you allocate the array dynamically, with numbers provided by the user.

Computer Science Dominique Thiebaut

Page 42: Week12 - Clark Science Center · 2019-11-25 · Exercise • Write a program that reads the file data.txt, which has the number of ints it contains on the first line, then one int

sizeof()can be tricky…

Computer Science Dominique Thiebaut

Page 43: Week12 - Clark Science Center · 2019-11-25 · Exercise • Write a program that reads the file data.txt, which has the number of ints it contains on the first line, then one int

sizeof ()

Computer Science Dominique Thiebaut

Page 44: Week12 - Clark Science Center · 2019-11-25 · Exercise • Write a program that reads the file data.txt, which has the number of ints it contains on the first line, then one int

sizeof(A) = 20sizeof(A[0]) = 4sizeof(B) = 8sizeof(B[0]) = 4sizeof(p) = 8sizeof(*p) = 4sizeof(name) = 14strlen(name) = 13sizeof(a) = 4sizeof(x) = 4

sizeof ()

Computer Science Dominique Thiebaut

Page 45: Week12 - Clark Science Center · 2019-11-25 · Exercise • Write a program that reads the file data.txt, which has the number of ints it contains on the first line, then one int

sizeof(A) = 20sizeof(A[0]) = 4sizeof(B) = 8sizeof(B[0]) = 4sizeof(p) = 8sizeof(*p) = 4sizeof(name) = 14strlen(name) = 13sizeof(a) = 4sizeof(x) = 4

Different!sizeof ()

Computer Science Dominique Thiebaut

Page 46: Week12 - Clark Science Center · 2019-11-25 · Exercise • Write a program that reads the file data.txt, which has the number of ints it contains on the first line, then one int

File IO

Page 47: Week12 - Clark Science Center · 2019-11-25 · Exercise • Write a program that reads the file data.txt, which has the number of ints it contains on the first line, then one int

File I/O: Output

Computer Science Dominique Thiebaut

C/fileIOOutput.c

Page 48: Week12 - Clark Science Center · 2019-11-25 · Exercise • Write a program that reads the file data.txt, which has the number of ints it contains on the first line, then one int

File I/O: Input

Computer Science Dominique Thiebaut

C/fileIOInput.c

Page 49: Week12 - Clark Science Center · 2019-11-25 · Exercise • Write a program that reads the file data.txt, which has the number of ints it contains on the first line, then one int

Computer Science Dominique Thiebaut

File I/O: Reading Ints [~handout] cat fileOfInts.txt 41 2 34 5 67 8 910 11 12

Page 50: Week12 - Clark Science Center · 2019-11-25 · Exercise • Write a program that reads the file data.txt, which has the number of ints it contains on the first line, then one int

Computer Science Dominique Thiebaut

File I/O: Reading Ints

C/fileIOReadNumbers.c

Page 51: Week12 - Clark Science Center · 2019-11-25 · Exercise • Write a program that reads the file data.txt, which has the number of ints it contains on the first line, then one int

Computer Science Dominique Thiebaut

[~handout] gcc fileIOReadNumbers.c[~handout] a.out1, 2, 34, 5, 67, 8, 910, 11, 12

File I/O: Reading Ints

Page 52: Week12 - Clark Science Center · 2019-11-25 · Exercise • Write a program that reads the file data.txt, which has the number of ints it contains on the first line, then one int

Exercise• Write a program that reads the file data.txt, which

has the number of ints it contains on the first line, then one int per line for the rest of the file. Your program must use a dynamic array to store the numbers, and find the max of the array, and print it.

Computer Science Dominique Thiebaut


Recommended