CSC 255 Objects and Algorithms HW 8

advertisement
Objects and Algorithms
CSC 255
HW 8
Student Name
Section
Instructor
Due Date
Part
Maximum Points
1
25 points
2
25 points
3
25 points
4
25 points
Total
100 points
Your Score
Textbook Reading Assignment
Review Chapter(s) 1 - 10 in your Applied Data Structures with C + + textbook.
Part 1 Exercises - Review Topics in Objects and Algorithms
( Algorithm : Cryptographic Rail Fence Cipher )
One type of encryption is the rail fence cipher. Here is an example of using this algorithm to
encrypt the plain text message " where is my car " .
w
e
h
e
r
s
i
y
m
a
c
r
The encrypted message is " w e e s ya hri mc r " .
The rail fence cipher involves writing the message such that alternate letters are written on separate
upper and lower lines.
Explain how you would use a Rail Fence cipher to encrypt the plaintext message:
" MEET ME AT MIDNIGHT " .
Explain how you would code a C ++ program that uses the Rail Fence cipher to encrypt the
above plaintext message.
Some starter code is given on the following page. You can modify the code to your liking.
© Copyright 2011 by P.E.P.
Page 1 of 4
CSC 255
Objects and Algorithms
Student Name
HW 8
Section
#include <iostream>
#include <string>
using namespace std;
void main() {
string myWords;
char myArray[100];
char myArray2[100];
string tempWords = "";
char temp;
cout << "enter your sentence or phrase\n";
cin >> myWords;
for (int index = 0; index < myWords.length(); index++)
{ // eliminate spaces
temp = myWords.substr(index, 1).at(0);
if (temp == ' ')
continue;
else
tempWords += temp;
}
for (int index = 0; index < tempWords.length(); index++)
{ // read individual characters of plain text into 2 arrays
if (index % 2 == 0)
myArray[index] =
tempWords.substr(index, 1).at(0);
else
myArray2[index] =
tempWords.substr(index, 1).at(0);
}
string sb;
string sb2;
for (int index = 0; index < tempWords.length(); index++) {
// append arrays into strings
sb += myArray[index];
sb2 += myArray2[index];
}
string temp1, temp2;
// display strings
string myOut;
myWords = ""; // reset
for (int index = 0; index < sb.length(); index++)
myOut += sb[index];
temp1 = myOut;
myOut = "";
for (int index = 0; index < sb2.length(); index++)
myOut += sb2[index];
temp2 = temp1 + myOut;
cout << temp2 << endl;
}
© Copyright 2011 by P.E.P.
Page 2 of 4
CSC 255
Objects and Algorithms
Student Name
HW 8
Section
Part 2 Exercises - Review Topics in Objects and Algorithms
( Algorithm : Two - Dimensional Matrix Inverse )
Square matrix inversion is used in solving systems of equations as well as in cryptographic
systems ( The Hill Cipher, as an example ) . One algorithm to invert a 2 row by 2 column square
matrix is given within the following source code. Test the program and modify it such that the
inverse matrix that is constructed is verified as being the actual inverse. To do this, perform
matrix multiplication between the original matrix and its inverse. The resulting product of a
matrix and its inverse always yields a unit matrix, filled with 1 's and 0's .
#include <iostream>
using namespace std;
void main() {
//program segment to construct the inverse of a 2 by 2 square matrix
//declare the 2 by 2 square matrix
double myMatrix[2][2] = {{1, 2}, {4, 3}};
//display the original matrix elements in matrix form
cout << "ORIGINAL MATRIX" << endl;
for (int row = 0; row < 2; row++)
{
for (int column = 0; column < 2; column++)
cout << "a[" << row << " , " << column << "] = "
<< myMatrix[row][column] << "\n";
}
//construct the matrix inverse
//interchange the forward diagonal elements
double temp = myMatrix[0][0];
myMatrix[0][0] = myMatrix[1][1];
myMatrix[1][1] = temp;
//change sign of off - diagonal elements
myMatrix[0][1] *= -1;
myMatrix[1][0] *= -1;
© Copyright 2011 by P.E.P.
Page 3 of 4
Objects and Algorithms
CSC 255
Student Name
//construct the matrix determinant
double determ = myMatrix[0][0] * myMatrix[1][1]
- myMatrix[1][0] * myMatrix[0][1];
HW 8
Section
//divide all elements my matrix determinant
for (int row = 0; row < 2; row++)
{
for (int column = 0; column < 2; column++)
myMatrix[row][column] /= determ;
}
//display inverse matrix
cout << "INVERSE MATRIX" << "\n";
for (int row = 0; row < 2; row++)
{
for (int column = 0; column < 2; column++)
cout << "a[" << row << " , " << column << "] = "
<< myMatrix[row][column] << "\n";
}
}
Part 2 Exercises - Review Topics in Objects and Algorithms
( Algorithm : Binary Trees )
Select an example of binary tree operations from your course textbook that you can code,
compile and execute. Submit your program code and screen snapshots for credit.
Part 2 Exercises - Review Topics in Objects and Algorithms
( Algorithm : Bin Packing with the First Fit Algorithm FF )
A bin - packing problem is the problem of determining the minimum number of containers of
capacity W into which objects of size w 1 , w 2 , . . . , w N ( with w i ≤ W ) can be packed.
One algorithm to bin pack is called the First - Fit Algorithm ( FF ) . This is a heuristic algorithm
for bin - packing in which the next weight to be packed is placed in the lowest - numbered bin
already opened into which it will fit. If it does not fit into any open bin, then a new bin is
opened.
Use the First - Fit bin - packing algorithm to determine the number of bins that are required to
pack the given weights into bins that can hold no more than a capacity of 6 .
4
2
5
© Copyright 2011 by P.E.P.
3
2
4
2
1
Page 4 of 4
Download