Final Exam

Topics to Know

You should assume that all of the foundational things you have learned from the class will be on the exam. This included but is not limited to:

Content that will be especially focused on for your assignment implementation with be:

Practice Problems

Practice 1:

Write a program that processes payroll.

Your program will use command line options to read input from two text files. The first text file is employee data that will be in the following format:

a 10.67
b 15.71 
c 1.52

Note that each subsequent row of the data represents an employee’s id and hourly rate of pay in this order. Each employee should be represented with a struct. The second text file will contain the hours that each employee works in the following format:

a 10
b 30
a 5
c 80

Note that each row of the data represents an employee’s id and hours worked in this order. Also, an employee may have worked their hours non-consecutively in the case of employee 1 who worked 15 hours total, but spread out over multiple entries.

You cannot assume how many records are in either data file.

Your program should consist of three functions.

main()

The main function will focus on processing the command line options, checking for errors, opening/closing files, and freeing memory.

create_employees()

This function will create and resize your array of employee structs with the data from the text file.

calculate_hours()

This function will read hour data from the second input file and update the correct employee’s struct with the hours they worked.

display_payroll()

This function will write out the payroll information for each employee to an output file:

a $160.05
b $471.30
c $121.60

Note the dollar sign and the maximum of 2 decimals of precision. There is no need to round, just limit the output to two decimals.

Program Usage

Run the program using: ./program_name employees.txt hours.txt output.txt

You should check for:

Practice Problem 2

Read in data from a file containing x and y coordinates. The format for the data is:

A x10 y12
B x14 y1.4
C y15 x16.7 D x1 y22.789

Note that the first letter is the identifying name of the point, and the next sets of data represents the x and y coordinate values. Note that all data records will be complete with a name and (x, y) coordinate data, but the data may not be newline separated, and the order of the (x, y) values could be swapped. Create a function to calculate the distance for all combinations of points.

For example:

Assume we have points
A B C D

The combinations that need distances calculated are:
AB AC AD BC BD CD

Output should be written to a file in the format:

A B 11.330
A C 7.341
...

Where point names are shown and the distance between them to 3 decimal places. The elipsis ... is not a part of the output, but rather indicates that the rest of the combinations and distance data would follow.

Practice Problem 3

You are given an input file with a series of integer numbers (whitespace separated). You are given another file with a series of integer numbers (whitespace separated). Write a program that tell you how many often a number in the second file appears in the first. Write out the numbers and the number of times they appear.

Usage:

./program in1.txt in2.txt out.txt

Output:

1 10
2 0
565 1
-33 11

Practice Problem 4

Suppose we have a file that contains an M x M “map” of numbers to represent cities that are connected. This grid below represents 5 cities. The intersection of a row and column in a grid indicates there is a connection between two cities. For example (assume zero based numbering), city 1 (row 1) shows that it connects to itself (at column 1) and city 2 (at column 2). All other cities in the row show a zero indicating they are not directly connected.

1 0 0 0 0
0 1 1 0 0
0 1 1 0 1
0 0 0 1 0
0 0 1 0 1

Write a program that can read in and store data in this format from a file and give two city values can determine if there exist a path between the two. IT IS NOT NECESSARY TO KNOW WHAT TRAVEL PATH WILL GET US TO OUR DESTINATION. WE ONLY NEED TO KNOW IF IT IS POSSIBLE TO REACH IT.

The program usages (with the example map data above) should be:

./program_name map.txt 1 4
True

./program_name map.txt 4 1
True

.program_name map.txt 0 4
False

Practice 5:

Write a program that reads in text from a file and reverses all the words in the document while leaving spaces and punctuation in place. This means that a “word” is defined as any set of alphanumeric characters (letters or numbers). The modified text is written to an output file.

Example program execution:

./my_program input.txt output.txt

Sample input:

hello world,
nice to see you today!

Sample output:

olleh dlrow,
ecin ot ees uoy yadot!

Program 6:

Write a program to find the sum of the digits of a positive integer.

Hint: You can get the rightmost digit of a positive integer n using n % 10, and you can remove the rightmost digit of a positive integer using n / 10.