New version page

UConn CSE 2102 - Homework #4

Documents in this Course
Load more

This preview shows page 1 out of 2 pages.

View Full Document
View Full Document

End of preview. Want to read all 2 pages?

Upload your study docs or become a GradeBuddy member to access this document.

View Full Document
Unformatted text preview:

CSE 3100 Systems ProgrammingHomework #4 Due: 10/11/2018Complete your work in the hw4 folder. Remember to pull, add, commit, and push. Do NOT add objectfiles, executables, the font file, or matrix files in your repo.Exercise 1. (50 points) strrenderVery often, computers display a character on screen as a matrix of tiny dots, called pixels. For thepurposes of this exercise, you can consider a pixel has either the background color or the font color (the colorof characters), and we will use ' ' (space) to display pixels that have the background color and '*' for theones that have the font color.A font file specifies how each character should be displayed. The font file you will be using is font8x8.dat,which can be generated by running generate-fontfile. This font displays each character as an 8x8 dotmatrix. A row of 8 pixels can be stored in a byte, with each pixel stored in a bit. If a bit is 0, the pixelhas the background color. Otherwise, the pixel has the font color. The left most pixel in a row is stored inthe right most bit, and the right most pixel in the left most bit. The following example shows how a row isdisplayed.Bits in the byte: 11101101Bits reversed: 10110111Characters (pixels) to be displayed: * ** ***Since each dot matrix has eight rows, the entire 8x8 matrix can be stored in 8 bytes. In font8x8.dat,starting from the beginning, every 8 bytes group is the matrix for a character. The first matrix is forcharacter '\x00', which has ASCII value 0, the second one is for character '\x01', which has ASCII value1, and so on. The file has the information for characters from '\x00' to '\x7F' (ASCII value 127). Studygenerate-fontfile.c for details on how the font file is written.Complete the functions in strrender.c. The program uses ' ' and '*' as pixels to display a string onscreen. The string is the first argument passed to the program (argv[1]). If the string is long, it is displayedin multiple lines. A line displays up to NUM_CHAR_PER_LINE characters in the string. NUM_CHAR_PER_LINE isdefined as 8 in the template file. In the following example, the first 8 characters are on the first line and theremaining 7 characters are on the second line.$./strrender 'Hello---CSE3100'** ** *** ***** ** ** **** ** **** ** ** ********** ** ** ** ** ** ** ****** ****** ******** ** ****** ** ** ** **** ** ** ** ** ** **** ** **** **** **** ******** **** ******* **** ** ***** ******* ** ** ** ** * ** ** *** ** ** ** **** *** ** * ** ** ** *** ** ***** *** **** *** ** ** **** ** ****** *** ** * ** ** **** ** **** **** ** ** ** ** * ** ** ** *** ** *** ****** **** ******* **** ****** ***** *****1Exercise 2. (50 points) Matrix ADTTo handle matrices of any size in C, we can define an abstract data type (ADT) by#define TElement inttypedef struct Matrix {unsigned int nrow ;unsigned int nc o l ;TElement ∗ ∗ data ;} TMatrix ;where nrow and ncol are numerical attributes representing the numbers of rows and columns of the matrixand data is a pointer to an array of row pointers. Each row pointer points to an array of elements ofTElement type. TElement is defined as int.One can design an API that has a number of C functions to operate on the ADT. For example, thefollowing API functions can create a new matrix, read a matrix from a file, free a matrix, transpose amatrix, and write a matrix to a file:TMatrix ∗ newMatrix ( unsigned int nrow , unsigned int n c o l ) ;TMatrix ∗ readMatrix ( char ∗ f il e n am e ) ;void f r e eM a t ri x ( TMatrix ∗ m) ;TMatrix ∗ tr a n s p o s e Matrix ( TMatrix ∗ m) ;void wr i t eMatrix ( TMatrix ∗m, char ∗ f i l en a m e ) ;Perhaps unsurprisingly, the ADT has overtones of object oriented programming. Indeed, newMatrix()and readMatrix() look like constructors (they create a TMatrix!) while freeMatrix() feels like a destruc-tor/finalizer. The other two functions that transpose a matrix and write a matrix to a file both take aTMatrix value as argument and therefore look and feel like methods.When reading a matrix from or writing a matrix to a file, the file has the following format:1. An unsigned integer nrows indicating the number of rows. It has 4 bytes.2. An unsigned integer ncols indicating the number of columns. It has 4 bytes.3. Rows, starting from row 0 to row nrows − 1. Each row has ncols integers. Each integer has 4 bytes.A program mf-generate is provided to generate matrices. Study the source code. It helps you understandthe format of the matrix data file. It also serves as an example of writing integers to a file.In this exercise, you must implement the above API functions for the matrix ADT. The functions arein mf-transpose.c. The main() function calls API functions to read a matrix from a file, transpose it,and write the transpose to a new file. It should not be modified. All your changes should be done to thefunctions manipulating the matrix ADT. Read the comments in the template carefully.The following session shows how a matrix is generated, transposed, and transposed again. mf-generategenerates matrix.out. mf-transpose transposes matrix.out to matrix.out.out. Then mf-transposetransposes matrix.out.out to matrix.out.out.out, which should be the same as matrix.out. This isconfirmed below by the matching md5sum hash codes of the matrix.out and matrix.out.out.out files.$ ./mf-generate 5000 300$ ./mf-transpose matrix.out$ ./mf-transpose matrix.out.out$ md5sum matrix.out*85d2b8a7563192bf1142afbd6d7e2efd matrix.oute8236f2e9e677b08647ade892fdc17c3 matrix.out.out85d2b8a7563192bf1142afbd6d7e2efd


View Full Document
Loading Unlocking...
Login

Join to view Homework #4 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Homework #4 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?