Sei sulla pagina 1di 5

CSE 2050 - Programming in a Second Language (C++)

Homework Assignment 4

February 12, 2015

Due Date
February 17, by 11.59pm

Description

In this assignment, you will implement a number of small programs. Here, you will:
use if-else, switch-case statement, for loops, do-while loops, files, and dynamic memory
allocation
use the cout object to display results on the console. Use functions in iomanip to format
the output.
use cin for standard input only when it is explicitly requested.
Input validation should be performed in all programs. Hints on how to set the limits for
validation are inside the text of the question. If an invalid parameter is passed to your
function use assert statement to catch it (as an example of how it works you can take a
look at dummy_driver.h, you should be familiar with it already).
Your programs should contain a multi-line header comment similar to:

/*
PROGRAM : CIRCLE . CPP
Written by Bob the Great
This program calculates the circumference of a circle .
Last modification : 8/20/2010
*/

Implementation Instructions

We provide a template for source files and make file that you must use. You will find the files in
the directory src/ which contains:
a .cpp file for each question
the driver header file
an incomplete Makefile that builds multiple projects.
You will write your code inside the provided source files, and you will edit the makefile so it builds
all programs. Each program must be a separate executable file.
You will submit:
1. only the source files (i.e., .cpp and .h files) and the associated Makefile. Do not submit .o
files, executables, or test datasets.
2. files at point 1 (i.e. .cpp and .h files, and the Makefile) must be in the src directory
compressed in the src.tar.gz file.
To compress the src directory, move to its parent directory (i.e. if you are inside src type cd ..),
then type the following command on the linux terminal:
tar zcvf src . tar . gz src

Questions
1. Numerical Computations Dynamic Memory Allocation
Your program will compute the matrix multiplication of two square matrices Amm and Bmm .
These matrices are stored in two files: A.dat and B.dat. The files, in case of 4 4 matrices,
have this format:
1 2 3 4
4 6 7 8
9 10 11 12
13 14 15 16
that is elements of the the matrix are stored in the file as M rows and M columns. Each
element is separated by a space. Your program will be organized as follows:
a function load_matrix that accepts as parameters a file name, and a matrix (i.e. an
array of arrays) where store the loaded values. It returns M if everything was ok, a
negative integer otherwise.
a function product that accepts as parameters three matrices A, B, productMatrix,
and an integer M. It computes the product productMatrix = A B.
a function display_matrix that accepts as parameter a matrix and an integer M, and
displays the matrix in a row by column format.
a function matrix_multiplication that accepts as parameters two filenames. This
function will invoke the other functions to first load matrices from files, then compute
their product, and finally display the result.
Note: This is the exact same problem of Assignment 3: Numerical Computations, but this
time you have to detect the size M of matrices at runtime and allocate memory dynamically
using new, and do the appropriate checking to ensure that files exist, matrices are square,
and of the same size.
2. Numerical Computations Dynamic Array
You should have noticed how hard, cumbersome, and prone to errors is to use double pointers.
Hopefully, you will never try again use double pointers to simulate matrices. A much better
solution is to store the matrix in an array form matrix[COLUMNSROWS], then map 2D indices
to 1D indices through a simple linear transformation.
Once again, your program will compute the matrix multiplication of two square matrices
Amm and Bmm . These matrices are stored in two files: A.dat and B.dat. The files, in
case of 4 4 matrices, have this format:
3

1 2 3 4
4 6 7 8
9 10 11 12
13 14 15 16
that is elements of the the matrix are stored in the file as M rows and M columns. Each
element is separated by a space. Your program will be organized as follows:
a function index that accepts three integers representing row index, column index, and
size of the matrix. It returns the index of the element in the array.
a function load_matrix that accepts as parameters a file name and a reference tonumber of columns (rows) M. It return a pointer to a matrix (i.e. a dynamic array) where
the loaded values are stored.
a function product that accepts as parameters two matrices A, B, and the integer M
representing the number of columns (rows). It computes the product productMatrix =
A B and return a pointer to productMatrix.
a function display_matrix that accepts as parameter a matrix and the integer M
representing the number of columns (rows). It displays the matrix in a row by column
format.
a function matrix_multiplication that accepts as parameters two filenames. This
function will invoke the other functions to first load matrices from files, then compute
their product, and finally display the result.
Note: again you have to detect the size M of matrices at runtime and allocate memory
dynamically using new. You must do the appropriate checking to ensure that files exist,
matrices are square, and of the same size.
3. Load and Save Image Files Plain PGM images are saved in plain ascii format (http:
//netpbm.sourceforge.net/doc/pgm.html). This is a very simple image format. The
format consists of a three-line header field followed by the image data (i.e., pixel values). An
example of a small image is given below:
P2
24 7
7
0 0 0
0 3 3
0 3 0
0 3 3
0 3 0

0
3
0
3
0

0
3
0
0
0

0
0
0
0
0

0
0
0
0
0

0
7
7
7
7

0
7
0
7
0

0
7
0
7
0

0
7
0
0
0

0
0
0
0
0

0
0
0
0
0

0
1
1
1
1

0
1
0
1
0

0
1
0
1
0

0
1
0
0
0

0
0
0
0
0
4

0
0
0
0
0

0
5
5
5
5

0
5
0
5
0

0
5
0
5
0

0
5
5
5
0

0
0
0
0
0

0 3 0 0 0 0 0 7 7 7 7 0 0 1 1 1 1 0 0 5 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
where:
First line: Magic number P2 that identifies the type of image.
Second line: The width and height of the image.
Third line: The maximum pixel value in the image.
Each pixel in the raster has white space before and after it. There must be at least one
character of white space between any two pixels, but there is no maximum.
No line should be longer than 70 characters.
There is a newline character at the end of each of these lines.
Your program will be organized as follows:
load_image function accepts a filename, and three references for width, height, and
maximum pixel value. This function read a pgm image from a file and load it into
memory. It returns a pointer to memory containing pixel values.
save_image function accepts a filename, a matrix, width, height, and maximum pixel
value. This function save the image into a file with the correct format. It returns 0 if
everything ok, different from 0 otherwise.
load_and_save function accepts two filenames as parameters. It loads an image
imageToLoad by using load_image function and then save the image to a file named
imageToSave by using save_image function.
Note: your program must check that loading image and save image operations went fine.
You must also check that the image file is in the correct format.

Potrebbero piacerti anche