pointers in real life

32
Pointers In Real Life Peter Thedens and Landon Woerdeman

Upload: peter-thedens

Post on 11-Jan-2017

86 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Pointers in real life

Pointers In Real LifePeter Thedens and Landon Woerdeman

Page 2: Pointers in real life

Problem

A professor has an unsorted text file containing student “netid”s and scores.

The professor wants the data to be ordered both alphabetically and by score.

Additionally, the professor wants to know the top scorer(s) and the mean and median score.

Page 3: Pointers in real life

Goal of program● Read a file containing students’ grades● Sort grades by username and score (and output)● Find top scoring student(s) (and output)● Compute mean and median (and output)

Page 4: Pointers in real life

grades.txtnportman, 83thanks, 73mdamon, 100sstallone, 67bpitt, 83ajolie, 75tcruise, 75jfranco, 30jdepp, 82ldicaprio, 95jlawrence, 97sjohansson, 88mkunis, 93dwashington, 78cbale, 47

Page 5: Pointers in real life

Process

Read user input Read and parse file

Sort data Calculations

Output file

Read input

Processing

Page 6: Pointers in real life

Process

Read user input Read and parse file

Sort data Calculations

Output file

Read input

Processing

Page 7: Pointers in real life

Read User input

● Command line arguments○ Consists of an array of C-strings (argv) and a number (argc)○ argc – Number of strings○ argv – C-strings contain each command line argument○ Note: argv[0] contains the name of the program

● fopen() for reading from file○ Takes a file path and returns a file pointer○ Returns NULL if file does not exist○ “r” specifies read operation

Page 8: Pointers in real life

Grades structure

● Using structs, we can create a grade● More on structs here

○ Access members with ‘.’○ grade1.score returns the user’s score

● Basically a holder containing:○ int score - integer containing student’s score○ char* netid - C-string containing netid

■ Character pointer■ Holds the address of the first character in the string

Page 9: Pointers in real life

Process

Read user input Read and parse file

Sort data Calculations

Output file

Read input

Processing

Page 10: Pointers in real life

Read and parse data loop

while(not end of file)

Get line of input

Parse line of input

Page 11: Pointers in real life

Add initial loop

● Initially don’t know number of entries● Can’t allocate correct space● To fix, iterate twice

○ Loop 1: Count entries○ Allocate space using malloc()○ Loop 2: Parse entries

Page 12: Pointers in real life

Read and parse data (revised)

while(not end of file)

Get line of input

Parse line of input

while(not end of file)

Increment count

Allocate

Page 13: Pointers in real life

Read and parse data (revised)

while(not end of file)

Get line of input

Parse line of input

while(not end of file)

Increment count

Allocate

Page 14: Pointers in real life

Counting lines of file

● We will use getLine() to read file● This returns -1 when end of file is reached● Increment counter until -1 is returned

Page 15: Pointers in real life

Read and parse data (revised)

while(not end of file)

Get line of input

Parse line of input

while(not end of file)

Increment count

Allocate

Page 16: Pointers in real life

Memory allocation

● Use malloc() to reserve space for array of grades● Discussed under advanced topics here● Does 2 things

○ Reserves desired amount of memory○ Returns pointer to that memory

Page 17: Pointers in real life

Read and parse data (revised)

while(not end of file)

Get line of input

Parse line of input

while(not end of file)

Increment count

Allocate

Page 18: Pointers in real life

Read line of input

● Call getLine() (described here)○ Pass file pointer, character pointer (for string), and size pointer○ Updates passed character pointer to read string○ Updates size pointer to size of string (disregarded)

Page 19: Pointers in real life

Read and parse data (revised)

while(not end of file)

Get line of input

Parse line of input

while(not end of file)

Increment count

Allocate

Page 20: Pointers in real life

Parsing

● Parse string● First part: netid

○ Use strsep() to separate the string based on the comma○ Described here○ Assign to grades[i].netid a character pointer to the string (returned by strsep())○ For our use, the pointer must be set to NULL before getting the line

● Second part: score○ Use atoi() (described here) to parse an integer from a string○ Assign to grades[i].score the score of element of the array grades

Page 21: Pointers in real life

Parsing code snippet

Page 22: Pointers in real life

Process

Read user input Read and parse file

Sort data Calculations

Output file

Read input

Processing

Page 23: Pointers in real life

Sort data

● We will use a selection sort (described here)○ Search through the unsorted part of array○ Find minimum element and swap with first element

■ Using pointers to swap is described here○ Decrease size of unsorted part of array

● To sort netids, we will use strcmp() to identify smallest element○ If the return value of strcmp() is negative, first parameter is smaller○ If the return value of strcmp() is positive, second parameter is smaller○ strcmp() is described here

● To sort scores, we will use ‘<’ to identify smallest element

Page 24: Pointers in real life

Sorting code snippet

Page 25: Pointers in real life

Process

Read user input Read and parse file

Sort data Calculations

Output file

Read input

Processing

Page 26: Pointers in real life

Calculations

● Median○ Use sorted-by-score array○ If number of scores is odd, use middle element○ If number of scores is even, use average of middle 2 elements

● Mean○ Add up total of scores in array and divide by number of elements

● Top scorer(s)○ Find highest score (end of sorted array)○ Iterate backwards until score is less than highest○ From this index to the end are the top scorers

Page 27: Pointers in real life

Calculations code snippet

Page 28: Pointers in real life

Process

Read user input Read and parse file

Sort data Calculations

Output file

Read input

Processing

Page 29: Pointers in real life

Output file

● At each step, the output file is being created○ After each sort and calculations print○ fprintf() is described here○ Is like printf to a file rather than the console

● File pointer is closed at termination of program○ fclose() writes the file

Page 30: Pointers in real life

Output code snippet

Page 31: Pointers in real life

Example output

Page 32: Pointers in real life

Why are pointers needed?

● File pointer● Dynamic memory allocation● Swap function

For more on pointers: http://cpointers.weebly.com