首页 > > 详细

辅导COMP 2160、辅导C/C++程序语言

COMP 2160 Assignment 2
Material covered:
basic C: syntax, types, and functions
more advanced C: arrays, strings, structs, and I/O
simple dynamic memory allocation
contracts
Notes:
You must follow the programming standards and best practices not doing so will result in a loss of marks. For this assignment, respect all standards and practices.
You must accept and follow all of the academic integrity requirements for the course, including the Expectations for Individual Work in Computer Science.
You must fill in an honesty declaration for this assignment before you submit your solution.
Your assignment code must be handed in electronically on the CS Linux system as the assignment a2; see the hand-in guidelines for details.
The C code you submit must compile and run on that system without warnings or errors.
Question 1: A simple database
Implement a simple relational database in C, consisting of three tables related by two key fields. For this problem, the information consists of provincial and industrial data related by province abbreviation and an "industry code". For example:

-+-----------+------+ +------+------+--------+----------+ +----------+-------------+
| Prov Name | Prov | | Prov | Year | Income | Ind Code | | Ind Code | Name |
-+-----------+------+ +------+------+--------+----------+ +----------+-------------+
... | Manitoba | MB | | ON | 2017 | 35700 | 112500 | | 113 | Forestry |
-+-----------+------+ +------+------+--------+----------+ +----------+-------------+
| Ontario | ON | | MB | 2013 | 62200 | 113 | | 112500 | Aquaculture |
-+-----------+------+ +------+------+--------+----------+ +----------+-------------+
| | | | | | | | | | |
... ... ...
From these tables, we can determine, for example, that Manitoba's income from Forestry was 62200 (thousands of dollars) in 2013, and Ontario's income from Aquaculture was 35700 (thousands of dollars) in 2017.

For this question you will read the data for each table from a different data file and store the result in an exactly-sized array of structs. To make it "exactly" sized you need to read each file first, count the number of lines, and then dynamically allocate an array of the correct size. Then read the data into the array. Values in the table are separated by commas (CSV without quotes). Note that the first line is "header" data indicating what value is contained in each column underneath. Ignore it while reading input. Define your C structs with appropriately-named and typed fields by consulting these headers.

Once you have read the data from the tables, your program will read commands from standard input, one per line, and perform the requested processing. You will handle these commands:

FIND [Province],[Year],[Ind] will find all the income for that named province, in the given year, for every industry whose name starts with the letters "Ind" (no spaces). For example, using the data above, FIND Ontario,2017,Aqua will output Aquaculture $35700 (one line of output for each matching industry). Output lines are in alphabetical order by industry.
SUM [Province] BY Year will show the total income for the given province for every year in the data. For example, using the data above, SUM Manitoba BY Year will output 2013 $62200 (one line of output for every year). Output lines must be ordered by year.
SUM [Ind] IN [Year] BY Province will show the total income for every industry whose name starts with the letters "Ind" (no spaces), in the given year, grouped by province. For example, using the data above, SUM A IN 2017 BY Province will output Ontario 2017: $35700, along with one line for each of the other provinces showing $0 as the income. Provinces must be in alphabetical order by name.
Exit the program on end of file.

Notes:
Data files for each of the three tables are found on UMLearn. Use those filenames. Data comes from Statistics Canada, slightly modified for this assignment.
You must keep the tables as separate arrays of structs, as read from the file. Do not use parallel arrays, and do not "join" or "sum" the data before storing it.
The data set you are given is incomplete. More data will be added closer to the assignment due date.
The data sets are large! Use an efficient join technique if necessary. See lab #2 for implementation ideas.
There may be one more command added before the due date as well.
Handle all errors as gracefully as possible. Only exit if an error is not recoverable (e.g. input file not found). Blank or missing fields from an input file can be filled with empty string or zero. Errors in commands can print an error message, and continue to the next command.
Assume a reasonable maximum length for each text field, by examining the text files.
Assume a maximum length of 80 characters for input command lines.
All commands are case-sensitive.
All the C code for this question must be in a single source file.
If you need to sort, use qsort() from stdlib.h. See the sample program qsort_demo.c for an example of using it to sort structs.
Question 2: Matrices
Write a C program that will read, print, transpose, and add matrices (2D arrays) of "double" values.

When your program runs, it will read two matrices (A and B) in the format below from standard input. It will add the two matrices (A + B) and print the result, and then add the first matrix with the transpose of the second (A + BT), and print that result. Output will be printed to standard output. Both results will be printed in the same format as the original input.

The format of the matrices for both input and output is as follows:

2 3
173.5 77.9 0.2
0.0 15.0 15692.7
where the first line is the number of rows r followed by the number of columns c. Then there will be r lines of data, with c numbers on each line. Both r and c must be greater than zero. Values are separated by one or more spaces. A line may start with spaces.

You can add matrices of different dimensions by augmenting missing values with zeroes as necessary. For example, if you are adding a matrix with 3 rows and 2 columns to a matrix with 2 rows and 4 columns, your output will have 3 rows and 4 columns.

Programming practices:
Your program must use contracts internally to validate both parameters and state, such as the state of matrices, in any function that accesses that state.

Your program must be separated into functions for performing major tasks, such as input, output, and addition. These functions must then use contracts.

Your program must also implement functions for performing elementary tasks on matrices, including a "getter" and a "setter" for getting/changing a value at a specific row and column of a matrix. These functions must also use contracts.

This means you must have at least six functions (with contracts) for performing tasks in your program, plus main(), plus any functions you need to validate state.

At each step of your implementation, focus on code safety: if at any point during development, your program crashes for any reason other than a failed assertion, consider what went wrong. What did you miss, and how could you have caught it?

Your final program must not crash or produce invalid results under any circumstances (except perhaps cosmic rays, but don't try to blame those).

Notes:
Assume maximum dimensions of 100 rows by 100 columns for all matrices. Do not use dynamic memory allocation for this question.
For this question only, you can declare your matrices at global scope to avoid putting them on the stack. But you must still pass them as parameters to functions where appropriate.
Structs are recommended.
The program only needs to read and print a single set of results every time it is run. Do not repeat.
If there is any error in the input, you can exit immediately with an error message.
Print to one decimal place in the output. Use the format code %7.1f to neatly print the double values. They will be displayed in rows and columns.
Convert an input token from a string to a double using strtod(). Check the result for errors!
The format of the output is the same as the input; it should be possible to use the output as the input the next time the program is run.
There is no extra data in the input. The input will be exactly one matrix followed immediately by another matrix with no additional text (including blank lines) in between.
An input line can contain no more than 801 characters (including'\n' and '\0').
All the C code for this question must be in a single source file.

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!