Assignment 8
CS 135: Computer Science I
Spring 2008
- Learn and demonstrate declaration, initialization, and
manipulations of 2D arrays in C/C++.
- Learn and demonstrate simple string manipulation in C/C++.
Design, code, and test a C/C++ program that reads two 2D arrays from a
file and prints the two arrays that were read, their product, and
their sum to another file. There are constraints on the filenames to
be used and on the format that the arrays follow in the files.
- Constraints on filenaming. The input filename (the filename from
which you read the two arrays) and the output filename (the filename
to which you write all results) are generated by your program from
the same root. To generate the input filename you concatenate
"In.txt" to the root. Similarly, to generate the output filename you
concatenate "Out.txt" to the root. Here is a simple example. If your
root is "test" then your input filename will be "testIn.txt" and
your output filename will be "testOut.txt".
- This root will be read in from standard input. You will
then generate the input and output filenames in your program and
then read from and write to the filenames that were generated.
- Constraints on array format. Assume that the maximum size of the
arrays is
. Here is a sample file holding two 2D
arrays. The first is a
array, while the second is a
array. Input files always hold two 2D arrays.
5 5
3 9 8 7 0
3 5 3 6 1
5 9 0 9 5
9 0 0 8 0
5 7 8 2 1
5 4
1 8 1 7
9 6 1 5
0 8 0 3
8 7 8 4
9 1 6 9
The first two numbers in the file specify the number or rows and the
number of columns respectively. The next set of five numbers
( 3 9 8 7 0 ) are the values of the 2D array's first
row. The next five, the second row and so on. The second array is
seperated from the first by a blank line and follows the same format.
So, it is an array with
rows and
columns, and the first row contains
( 1 8 1 7 ) while the last (
) row contains
( 9 1 6 9 ).
- When you write 2D arrays to your output file, you must use the
same format.
- Adding two 2D arrays is simple. Both arrays must have the same
dimensions and for the two 2D arrays
and
, their array denoted by their sum,
, is an element by element sum as shown below:
for all
and
.
- Multiplying two arrays is a bit more complicated. First, the
number of colums in the first array must be equal to the number of
rows in the second array. Second, to understand how to multiply 2D
arrays, consider the following example.
The result comes from the following computation:
So the first row of the first array and the first column
of the second array combine to generate the element in the first
row, first column of the product array. The first row of the
first array and second column of the second array combine to
generate the element in the first row, second column of the
product array. And so on. In general you need a triple loop, and the
computation within the innermost loop is
Here is a sample run of the program on the data in the file below.
2 2
1 2
6 7
2 2
1 4
7 6
Clearly, this contains two
2D arrays. Assume that this has
already been placed in the file named "testIn.txt"
sushil@toasty:~/classes/135/assignments/2d/code$ ./ops
Enter array data filename root:
test
Input Filename: testIn.txt Output Filename: testOut.txt
The program computes the product and sum, writes them to "testOut.txt"
and the file "testOut.txt" contains:
2 2
1 2
6 7
2 2
1 4
7 6
2 2
15 16
55 66
2 2
2 6
13 13
Here's another sample:
sushil@toasty:~/classes/135/assignments/2d/code$ ./ops
Enter array data filename root:
a5
Input Filename: a5In.txt Output Filename: a5Out.txt
Cannot add these arrays:
Showing that there was a problem in adding these arrays. Here's "a5In.txt"
5 5
3 9 8 7 0
3 5 3 6 1
5 9 0 9 5
9 0 0 8 0
5 7 8 2 1
5 4
1 8 1 7
9 6 1 5
0 8 0 3
8 7 8 4
9 1 6 9
Clearly, the number of columns in both arrays is different and so
these arrays cannot be added. However, these arrays can be multiplied
and the program does compute the product and writes this to the
output file, "a5Out.txt" shown below.
5 5
3 9 8 7 0
3 5 3 6 1
5 9 0 9 5
9 0 0 8 0
5 7 8 2 1
5 4
1 8 1 7
9 6 1 5
0 8 0 3
8 7 8 4
9 1 6 9
5 4
140 191 68 118
105 121 62 88
203 162 116 161
73 128 73 95
93 161 34 111
Make sure your program handles such error cases.
Turn in a Folder (Binder) containing:
- Cover sheet with
- Assignment Number
- Section Number
- Your name
- Your email
- Your TA's name
- Source code (.cpp) file (s) on a CD/USB stick with your name and section number written on your CD/USB stick.
- Hardcopy of your source code (printout)
- A printout of sample runs of your program on test cases showing all error cases that your program handles.
Ask an instructor or TA if you have questions.
Assignment 8
This document was generated using the
LaTeX2HTML translator Version 2002-2-1 (1.71)
Copyright © 1993, 1994, 1995, 1996,
Nikos Drakos,
Computer Based Learning Unit, University of Leeds.
Copyright © 1997, 1998, 1999,
Ross Moore,
Mathematics Department, Macquarie University, Sydney.
The command line arguments were:
latex2html -split 1 asN
The translation was initiated by Sushil Louis on 2008-04-16
Sushil Louis
2008-04-16