pointers in real life
TRANSCRIPT
Pointers In Real LifePeter Thedens and Landon Woerdeman
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.
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)
grades.txtnportman, 83thanks, 73mdamon, 100sstallone, 67bpitt, 83ajolie, 75tcruise, 75jfranco, 30jdepp, 82ldicaprio, 95jlawrence, 97sjohansson, 88mkunis, 93dwashington, 78cbale, 47
Process
Read user input Read and parse file
Sort data Calculations
Output file
Read input
Processing
Process
Read user input Read and parse file
Sort data Calculations
Output file
Read input
Processing
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
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
Process
Read user input Read and parse file
Sort data Calculations
Output file
Read input
Processing
Read and parse data loop
while(not end of file)
Get line of input
Parse line of input
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
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
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
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
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
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
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
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)
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
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
Parsing code snippet
Process
Read user input Read and parse file
Sort data Calculations
Output file
Read input
Processing
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
Sorting code snippet
Process
Read user input Read and parse file
Sort data Calculations
Output file
Read input
Processing
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
Calculations code snippet
Process
Read user input Read and parse file
Sort data Calculations
Output file
Read input
Processing
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
Output code snippet
Example output
Why are pointers needed?
● File pointer● Dynamic memory allocation● Swap function
For more on pointers: http://cpointers.weebly.com