+ All Categories
Home > Documents > Week13 - Home - Clark Science CenterExercise • Write a program that reads the file data.txt,...

Week13 - Home - Clark Science CenterExercise • Write a program that reads the file data.txt,...

Date post: 30-Jul-2020
Category:
Upload: others
View: 1 times
Download: 0 times
Share this document with a friend
62
mith College Computer Science Dominique Thiébaut [email protected] CSC231 Week 13— Introduction to C Fall 2019
Transcript
Page 1: Week13 - Home - Clark Science CenterExercise • 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

mith College

Computer Science

Dominique Thiébaut [email protected]

CSC231Week 13— Introduction to C

Fall 2019

Page 2: Week13 - Home - Clark Science CenterExercise • 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

Complete the code

shown here

Computer Science Dominique Thiebaut

getcopy C/decrementArray.c

Exercise

Page 3: Week13 - Home - Clark Science CenterExercise • 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

Complete the code

shown here

Computer Science Dominique Thiebaut

Solution

Page 4: Week13 - Home - Clark Science CenterExercise • 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

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 5: Week13 - Home - Clark Science CenterExercise • 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

Solutiongetcopy C/findMaxUsingFunctionSol.c

Computer Science Dominique Thiebaut

Page 6: Week13 - Home - Clark Science CenterExercise • 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

Exercise

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

Computer Science Dominique Thiebaut

Page 7: Week13 - Home - Clark Science CenterExercise • 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

Solutiongetcopy C/findMaxUsingFunction2sol.c

Computer Science Dominique Thiebaut

Page 8: Week13 - Home - Clark Science CenterExercise • 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

Input/OutputIO

Page 9: Week13 - Home - Clark Science CenterExercise • 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

Input From User

Computer Science Dominique Thiebaut

Page 10: Week13 - Home - Clark Science CenterExercise • 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

(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 11: Week13 - Home - Clark Science CenterExercise • 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

Dynamic Variables

Computer Science Dominique Thiebaut

Page 12: Week13 - Home - Clark Science CenterExercise • 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

• Dynamic: think "new" in Java

• malloc() = Memore Allocation for new data structure

Computer Science Dominique Thiebaut

Page 13: Week13 - Home - Clark Science CenterExercise • 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

RAM

Computer Science Dominique Thiebaut

user

pro

g

malloc()

Page 14: Week13 - Home - Clark Science CenterExercise • 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

user

pro

g

malloc()

RAM

Computer Science Dominique Thiebaut

OperatingSystem

Page 15: Week13 - Home - Clark Science CenterExercise • 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

RAM

Computer Science Dominique Thiebaut

OperatingSystem

dyn

user

pro

g

malloc()

Page 16: Week13 - Home - Clark Science CenterExercise • 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

RAM

Computer Science Dominique Thiebaut

OperatingSystem

dyn

(void*) p

user

pro

g

malloc()

Page 17: Week13 - Home - Clark Science CenterExercise • 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

RAM

Computer Science Dominique Thiebaut

OperatingSystem

dyn

(void*) p

p

user

pro

g

malloc()

Page 18: Week13 - Home - Clark Science CenterExercise • 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

Computer Science Dominique Thiebaut

getcopy C/malloc1.c

Malloc

Page 19: Week13 - Home - Clark Science CenterExercise • 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

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 20: Week13 - Home - Clark Science CenterExercise • 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

Solution

Computer Science Dominique Thiebaut

getcopy C/findMaxUsingFunction3sol.c

Page 21: Week13 - Home - Clark Science CenterExercise • 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

sizeof()can be tricky…

Computer Science Dominique Thiebaut

Page 22: Week13 - Home - Clark Science CenterExercise • 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

sizeof ()

Computer Science Dominique Thiebaut

Page 23: Week13 - Home - Clark Science CenterExercise • 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

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 24: Week13 - Home - Clark Science CenterExercise • 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

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 25: Week13 - Home - Clark Science CenterExercise • 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

File IO

Page 26: Week13 - Home - Clark Science CenterExercise • 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

File I/O: Output

Computer Science Dominique Thiebaut

C/fileIOOutput.c

Page 27: Week13 - Home - Clark Science CenterExercise • 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

File I/O: Input

Computer Science Dominique Thiebaut

C/fileIOInput.c

Page 28: Week13 - Home - Clark Science CenterExercise • 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

Computer Science Dominique Thiebaut

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

Page 29: Week13 - Home - Clark Science CenterExercise • 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

Computer Science Dominique Thiebaut

File I/O: Reading Ints

C/fileIOReadNumbers.c

Page 30: Week13 - Home - Clark Science CenterExercise • 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

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 31: Week13 - Home - Clark Science CenterExercise • 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

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

Page 32: Week13 - Home - Clark Science CenterExercise • 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

Function Prototypes

and Multi-File Projects

Computer Science Dominique Thiebaut

Page 33: Week13 - Home - Clark Science CenterExercise • 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

Original

Computer Science Dominique Thiebaut

Page 34: Week13 - Home - Clark Science CenterExercise • 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

myfuncs.c

smallestLargestSum3.c

Computer Science Dominique Thiebaut

myfuncs.h

Page 35: Week13 - Home - Clark Science CenterExercise • 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

Computer Science Dominique Thiebaut

Page 36: Week13 - Home - Clark Science CenterExercise • 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

Summary• C has a lot in common with Java

• C programmers must know how to use memory (malloc, pointers)

• No garbage collector: free dynamic structures yourself!

• Explicitly pass arguments by value or by reference to functions

Computer Science Dominique Thiebaut

Page 37: Week13 - Home - Clark Science CenterExercise • 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

D. Thiebaut, Computer Science, Smith College

End of Video Lecturehttps://www.youtube.com/watch?v=LFpoUFIfV0M&a=youtu.be

Page 38: Week13 - Home - Clark Science CenterExercise • 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

D. Thiebaut, Computer Science, Smith College

Logistics and Update

• Video lecture for Monday's snow day + Quiz

• No office hours today

• Office hours tomorrow (12/5) 12-1 p.m.

• Homework extended to 12/5 11:55 p.m.

• Homework 11 (optional) released tomorrow morning

Page 39: Week13 - Home - Clark Science CenterExercise • 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

D. Thiebaut, Computer Science, Smith College

And now, something completely different…

Executablegcc addition.c

Page 40: Week13 - Home - Clark Science CenterExercise • 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

D. Thiebaut, Computer Science, Smith College

And now, something completely different…

Executablegcc addition.c

gcc -S -m32 -masm=intel addition.c

Page 41: Week13 - Home - Clark Science CenterExercise • 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

D. Thiebaut, Computer Science, Smith College

ReviewMalloc

Page 42: Week13 - Home - Clark Science CenterExercise • 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

D. Thiebaut, Computer Science, Smith College

ReviewMalloc

Page 43: Week13 - Home - Clark Science CenterExercise • 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

Linked-Listsin C

Computer Science Dominique Thiebaut

Page 44: Week13 - Home - Clark Science CenterExercise • 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

D. Thiebaut, Computer Science, Smith College

Outline

• Linked Lists in C

• Structures in C

• Creating a Node

• Adding a Node

• Removing a Node

‘Alpha’

‘Zero’

‘Beta’

‘Apple’

head

Page 45: Week13 - Home - Clark Science CenterExercise • 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

"Alpha"

"Zero"

"Beta"

"Apple"

head

Computer Science Dominique Thiebaut

Page 46: Week13 - Home - Clark Science CenterExercise • 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

Computer Science Dominique Thiebaut

NULL

((void *)0)

"Alpha"

"Zero"

"Beta"

"Apple"

head

Page 47: Week13 - Home - Clark Science CenterExercise • 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 Node in C"Alpha"

Computer Science Dominique Thiebaut

http://www.gnu.org/software/gnu-c-manual/gnu-c-manual.html#Structures

Page 48: Week13 - Home - Clark Science CenterExercise • 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

C Structs

• Early attempt at encapsulating data

• Members are accessed using the dot notation:

http://www.gnu.org/software/gnu-c-manual/gnu-c-manual.html#Structures

Computer Science Dominique Thiebaut

Page 49: Week13 - Home - Clark Science CenterExercise • 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

Struct Example

Computer Science Dominique Thiebaut

Page 50: Week13 - Home - Clark Science CenterExercise • 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

Struct Example

Computer Science Dominique Thiebaut

Page 51: Week13 - Home - Clark Science CenterExercise • 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

Struct Example

Computer Science Dominique Thiebaut

getcopy C/structExample.c

Page 52: Week13 - Home - Clark Science CenterExercise • 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 Head of the List

Computer Science Dominique Thiebaut

head

Page 53: Week13 - Home - Clark Science CenterExercise • 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 Head of the List

Computer Science Dominique Thiebaut

head

That’s anempty list!

Page 54: Week13 - Home - Clark Science CenterExercise • 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

Displaying the List

Computer Science Dominique Thiebaut

"Alpha" "Zero" "Apple"

head

"Beta"

Page 55: Week13 - Home - Clark Science CenterExercise • 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

Creating the List

Computer Science Dominique Thiebaut

"Apple"

head

"Beta"

Page 56: Week13 - Home - Clark Science CenterExercise • 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

Computer Science Dominique Thiebaut

Page 57: Week13 - Home - Clark Science CenterExercise • 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

Computer Science Dominique Thiebaut

Page 58: Week13 - Home - Clark Science CenterExercise • 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

Computer Science Dominique Thiebaut

getcopy C/linkedListStrings.c

Page 59: Week13 - Home - Clark Science CenterExercise • 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

Exercise

• Add code to the linkedListStrings.c program that will remove all the nodes from the list, one by one (and free them).

• Make your code display the list after each deletion

Computer Science Dominique Thiebaut

Page 60: Week13 - Home - Clark Science CenterExercise • 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

Exercise

• Use functions for adding and removing nodes…

Computer Science Dominique Thiebaut

Main ProgramCalling

Functions

Page 61: Week13 - Home - Clark Science CenterExercise • 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

Solution

Computer Science Dominique Thiebaut

getcopy C/linkedListStrings2.c

Page 62: Week13 - Home - Clark Science CenterExercise • 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

D. Thiebaut, Computer Science, Smith College

We stopped herelast time!


Recommended