Lab #12
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
Lab #12
This lab exercise contributes to your lab mark (3.67%).
1. We reserve the right to ask you to explain and demo your lab solutions.
2. You need to declare that you write the code all by yourself at the submission webpage.
3. We use the lab machines in CSC 159 for all marking purposes; make sure your C programs can be compiled (no warning messages, or any notes) and run on these machines.
4. Pack all your files ( ex12q1.c , ex12q2.c , lab12.h , makefile , spreadsheet.txt ,
s01a.txt , and other files if any) into by tar . You can execute the command
to validate that you can extract files successfully and that they work
well (all files in the same directory), where is a marking program we provided under
"Week 14" and you should download it in the same directory.
5. Submit your to the designated submission webpage as early as possible; no
late submission is accepted after the due date.
Objectives:
1. Practice on program design — free style programming, but your style is not marked
2. Practice on calling from stdlib.h ;
3. Practice on conditional compilation, Makefile, command-line arguments, FILE I/O, string I/O;
4. Old major items still to be practiced: arrays and pointers, function pass-by-value, recursion, struct
Descriptions:
Part 1: Sorting by calls
Download the file (http://webdocs.cs.ualberta.ca/~ghlin/CMPUT201/Lab12/spr
eadsheet.txt), which contains the term-work marks for the students in the CMPUT 201 class. There are four components: Assignments, Quizzes, Labs and Exams, with their maximum marks and percentages (21, 8%), (14, 20%), (30, 33%) and (30, 39%), respectively. For every component, each student (their name in the format of "lastname firstname", in the spreadsheet) has a mark which is a floating-point number in the range , max_mark (the max_mark is the maximum mark
specified as in the above for the component). Design two C programs (i.e., executables called
and respectively) using conditional compilation to have the following
specified functionalities, and to determine the grade a student is to earn. Your programs must
use the following definition (which should be included in the required header file
struct student { char *lastname; char *firstname;
float assignments; // range [0, 21]; percentage 8%
float quizzes; // range [0, 14]; percentage 20%
float labs; // range [0, 30]; percentage 33%
float exams; // range [0, 30]; percentage 39%
float total; // range [0, 100]
char grade; // range {'F', 'D', 'C', 'B', 'A'}
}; |
For this part, the required source file is
and it contains the
function. You can
have other source files, and if any, name them as ex12q11.c , ex12q12.c , and so on. You are not recommended to have any other header file.
From (and other source files if any), compile a C program called sorting .
Run in the following way
./sorting spreadsheet.txt |
the program reads in the data from the file (Note that we might use different
filenames and file contents, while keeping the number of students to be ; and so your program must accept command-line arguments and use FILE I/O.) and then display the following user interface:
Sort all students by: XXX |
The user enters XXX, which is one of {Name, Assignments, Quizzes, Labs, Exams, Total} (case sensitive), and your program sorts the students in ascending order of the values of XXX. Note that the student names are all distinct from each other (i.e., no ties), and sorting by name follows the usual dictionary order using last name first and then first name; sorting by another struct member value might have ties, and in such a case breaks the ties using the student name.
Note that if XXX is not one of the defined strings, then your program terminates.
After the is read in, your program should calculate the total for each
student, but the grade is not assigned.
After the sorting, your program prints the sorted list of students, one line per student to
contain the values of all their members (use conversion specification point numbers), using the order specified in the above
that the first line of printing should be similar to the first line of
for all floating- definition. Note
to contain
the struct member names. In this way, if we save your program output (such as by redirection), then it will be very similar to spreadsheet.txt , except that it contains values for one or two more members .total (column "Total") and .grade (column "Grade") and the students are sorted in the ascending order of the given member value.
Note that if grades haven't been assigned yet, the column "Grade" should not be printed (since it is meaningless).
Your program terminates after the printing.
Sorting must be done by function calls to from stdlib.h . You do NOT implement
any sorting algorithm, but define the comparison functions to prepare the calls to qsort() .
Also from (and other source files if any), compile a C program called grading .
Run in the following way
./grading spreadsheet.txt
the program reads in the data from the file interface:
and then display the following user
Enter four cutoffs for grade assignment: DDD CCC BBB AAA
The user enters four floating-point numbers DDD, CCC, BBB, AAA, which are in the range and satisfy DDD CCC BBB AAA, and your program assigns a student the
grade F if their .total < DDD, or the grade D if DDD their .total < CCC, or the grade C if CCC their .total < BBB, or the grade B if BBB their .total < AAA, or the grade A if their .total AAA.
Next, using the grade point values (F = 0, D = 1.0, C = 2.0, B = 3.0, A = 4.0), your program calculates the class average grade point, and prints the following (where f.ff is replaced by the actual value with 2 digits after the decimal point)
The class average grade point is: f.ff |
(For your self-checking, when the cutoffs are , the average grade point should be
.)
Afterwards, your program sorts the students in ascending order of their totals and prints the sorted list as described in Part 1 (i.e., using XXX = Total) into a text file named
(i.e., using FILE I/O, not through redirection). Note that now with grades being assigned, the column "Grade" should be printed out.
Your program terminates afterwards.
Both programs
contains the
and
are compiled out of the same set of files, among which function. Running these two programs should have the required
appearance as stated in the above. Particularly, does not ask for the cutoffs for grade
assignment, while does not provide options to sort.
You are responsible to compose a working (or makefile ), such that by executing the
command compiled.
both targets
and
(and the target
in Part 2) will be
Part 2: Finding all solutions to a Sudoku puzzle
We have introduced Sudoku puzzles when we talked about recursions. In general, a Sudoku puzzle has a unique solution, but in this exercise we generalize a puzzle to have any number of solutions (i.e., , , or more). According to Wikipedia, the number of Sudoku solution grids is approximately .
In this exercise, we are given a puzzle to compute its number of solutions, and to print out some solutions.
The required source file
should contain the
function, in which we use an int -
type array game[9][9] to represent a puzzle where each element has a value from
(or otherwise the puzzle is invalid) and an element represents an entry to be
filled. For example (which is saved as under Week 14),
0 |
0 |
0 |
0 |
0 |
0 |
1 |
7 |
9 |
0 |
0 |
2 |
0 |
0 |
8 |
0 |
5 |
4 |
0 |
0 |
6 |
0 |
0 |
5 |
0 |
0 |
8 |
0 |
8 |
0 |
0 |
7 |
0 |
9 |
1 |
0 |
0 |
5 |
0 |
0 |
9 |
0 |
0 |
3 |
0 |
0 |
1 |
9 |
0 |
6 |
0 |
0 |
4 |
0 |
3 |
0 |
0 |
4 |
0 |
0 |
0 |
0 |
0 |
5 |
7 |
0 |
1 |
0 |
0 |
0 |
0 |
0 |
9 |
2 |
8 |
0 |
0 |
0 |
0 |
0 |
0 |
You can have other source files, and if any, name them as ex12q21.c , ex12q22.c , and so on. You are not recommended to have any other header file.
Use the command
to compile your program called
(see
in Part 1). You
need to resolve any warning messages or notes during compilation. To run your program, use command
./ex12q2 inputfile k |
where
stores the puzzle and
is the number of solutions to be printed out. Note
that we will use different filenames and puzzles; and so your program must accept command-line
arguments and use FILE I/O. You can use to test your program, which has solutions
(another puzzle the following:
under Week 14 should have solutions). Your program prints out
The number of solutions to the puzzle is: n.
where is replaced by the actual number.
By the above command, your program prints out the smaller number, between and , of solutions, each into a separate file. Assume is a -digit integer (w/o leading 's) below. The solution filename convention is solXX...X.txt ; there are X 's in the index XX...X , which
ranges from to . For example, when (then ), then the solution filenames are sol001.txt , sol002.txt , …, and so on; when (then ), then the solution filenames are sol1.txt , sol2.txt , …, and so on. If , then the solution filename convention does not apply (i.e., no need to print out any solution).
Use to make sure each of your three programs runs correctly, without any
memory leak or error. At least mark deduction applies to leak or error.
2021-11-29