Difference: Lab8 (1 vs. 20)

Revision 202018-04-17 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Lab 8 - Operator Overloading

Changed:
<
<

Due: APril 17 11:55pm

>
>

Due: April 19 11:55pm

 Moodle Link

add_fracs_inq_2.png
Your goal is to create a class for storing and manipulating fractions. We wish to write code to add, subtract, multiply, and divide fractions. In addition we wish to overload the traditional binary arithemitic operators so that we can create code like below:

Revision 192018-04-15 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Lab 8 - Operator Overloading

Changed:
<
<

Due: 11:55pm

>
>

Due: APril 17 11:55pm

 
Changed:
<
<
Moodle Link

add_fracs_inq_2.png
Your goal is to create a class for storing and manipulating fractions. We wish to write code to add, subtract, multiply, and divide fractions. In addition we wish to overload the traditional binary arithemitic operators so that we can create code like below:
>
>
Moodle Link

add_fracs_inq_2.png
Your goal is to create a class for storing and manipulating fractions. We wish to write code to add, subtract, multiply, and divide fractions. In addition we wish to overload the traditional binary arithemitic operators so that we can create code like below:
  %CODE{"c++"}% int main() {

Revision 182017-12-05 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Lab 8 - Operator Overloading

Due: 11:55pm

Changed:
<
<
Moodle Link

add_fracs_inq_2.png
Your goal is to create a class for storing and manipulating fractions. We wish to write code to add, subtract, multiply, and divide fractions. In addition we wish to overload the traditional binary arithemitic operators so that we can create code like below:
>
>
Moodle Link

add_fracs_inq_2.png
Your goal is to create a class for storing and manipulating fractions. We wish to write code to add, subtract, multiply, and divide fractions. In addition we wish to overload the traditional binary arithemitic operators so that we can create code like below:
  %CODE{"c++"}% int main() {

Revision 172017-11-07 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Lab 8 - Operator Overloading

Line: 74 to 74
  // Constructors and Destructor Fraction(void); Fraction(int n, int d);
Deleted:
<
<
~Fraction(void);
  // Test functions string to_string();
Line: 132 to 131
  } }
Deleted:
<
<
Fraction::~Fraction(void) { }
  // Returns the greatest common divisor of two integers int Fraction::gcd(int a, int b)const{
Line: 183 to 179
  * DIVISION (/) OPERATOR * result = (num1 * den2) / (den1 * num2) *
Deleted:
<
<
* INSERTION (<<) operator * Output fraction in form n/d
  */

%ENDCODE%

Revision 162017-10-18 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Lab 8 - Operator Overloading

Line: 108 to 108
  using namespace std;
Changed:
<
<
int main() { int n,d; cout << "Fraction test program." << endl; cout << "Enter numerator and denominator of first fraction:"; cin >> n >> d; Fraction a(n,d); cout << "Enter numerator and denominator of second fraction:"; cin >> n >> d; Fraction b(n,d); Fraction c = add(a,b); //Will be changed to use + operator //Fraction c = a+b;
>
>
// Default constructor Fraction::Fraction(void) { numerator = 0; denominator = 1; }
 
Changed:
<
<
// Display Result cout << a.to_string() << " + " << b.to_string() << " = " << c.to_string() << endl; return 0;
>
>
// Constructor Fraction::Fraction(int n, int d) { if (d == 0){ denominator = 1; numerator = 0; }else if (d < 0){ denominator = d * -1; if (n > 0){ numerator = n * -1; } }else{ numerator = n; denominator = d; } }

Fraction::~Fraction(void) {

 }
Added:
>
>
// Returns the greatest common divisor of two integers int Fraction::gcd(int a, int b)const{ while (b = 0){ int temp = b; b = a % b; a = temp; } return a; }

// Simplifies the calling fraction void Fraction::simplify(){ int factor = gcd(numerator, denominator); numerator = numerator / factor; denominator = denominator / factor; }

//test functions Fraction add(Fraction a, Fraction b) { int num = a.numerator * b.denominator + b.numerator * a.denominator; int den = a.denominator * b.denominator; Fraction result(num,den); result.simplify(); return result; }

string Fraction::to_string() { string result = std::to_string(numerator) + "/" + std::to_string(denominator); return result; }

// Overloaded Operators /* * In each case: (num1/den1) op (num2/den2) * * PLUS (+) OPERATOR * result = (num1 * den2 + num2 * den1) / (den1 * den2) * * MINUS (-) OPERATOR * result = (num1 * den2 - num2 * den1) / (den1 * den2) * * MULIPLICATION (*) OPERATOR * result = (num1 * num2) / (den1 * den2) * * DIVISION (/) OPERATOR * result = (num1 * den2) / (den1 * num2) * * INSERTION (<<) operator * Output fraction in form n/d */

 %ENDCODE%
Changed:
<
<

factions.cpp

>
>

main.cpp

  %CODE{"c++"}% #include
Line: 156 to 217
 %ENDCODE%

Runs to turn in:

Added:
>
>
 
Fraction 1 Fraction 2
1/2 3/5
52/43 63/21
3542/431 45785/3241
8/3 123/43

Grading Table

Changed:
<
<
RequirementGrading CommentsPointsScore
Easy to use user interface 10
C++ code includes comments, with project information at top, pre and post conditions for each functions and other cmments as needed. 10
The C++ code has good formatting, indentation, and organization. 10
Good variable and function names, appropriate use of constants rather then literal numbers. 10
Classes: Correctly defined and well organized 20
Runs: Run examples from trials with correct output 40
Total 100

Solutions

>
>
RequirementGrading CommentsPointsScore
Easy to use user interface 10
C++ code includes comments, with project information at top, pre and post conditions for each functions and other cmments as needed. 10
The C++ code has good formatting, indentation, and organization. 10
Good variable and function names, appropriate use of constants rather then literal numbers. 10
Classes: Correctly defined and well organized 20
Runs: Run examples from trials with correct output 40
Total 100
Solutions
 
META FILEATTACHMENT attachment="xl-2016-encryption-1.jpg" attr="" comment="" date="1475261034" name="xl-2016-encryption-1.jpg" path="xl-2016-encryption-1.jpg" size="103604" user="JimSkon" version="1"
META FILEATTACHMENT attachment="add_fracs_inq_2.png" attr="" comment="" date="1507471517" name="add_fracs_inq_2.png" path="add_fracs_inq_2.png" size="17268" user="JimSkon" version="1"

Revision 152017-10-08 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Lab 8 - Operator Overloading

Line: 11 to 10
 int main() { int n,d; cout << "Fraction test program." << endl;
Added:
>
>
char again; do {
  cout << "Enter numerator and denominator of first fraction:"; cin >> n >> d; Fraction a(n,d);
Line: 29 to 28
  cout << a.to_string() << " * " << b.to_string() << " = " << prod.to_string() << endl; cout << a.to_string() << " / " << b.to_string() << " = " << quotient.to_string() << endl;
Added:
>
>
cout << endl << "Go again? (Y or y to continue):"; cin >> again; } while (again = 'Y' || again = 'y');
  return 0; }
Changed:
<
<
%ENDCODE% =
>
>
%ENDCODE%
Fraction test program.
Enter numerator and denominator of first fraction:1 2
Enter numerator and denominator of second fraction:3 4
1/2 + 3/4 = 5/4
1/2 - 3/4 = 1/-4
1/2 * 3/4 = 3/8
1/2 / 3/4 = 2/3

Go again? (Y or y to continue):y
Enter numerator and denominator of first fraction:1 10
Enter numerator and denominator of second fraction:1 20
1/10 + 1/20 = 3/20
1/10 - 1/20 = 1/20
1/10 * 1/20 = 1/200
1/10 / 1/20 = 2/1

Go again? (Y or y to continue):y
Enter numerator and denominator of first fraction:23 57
Enter numerator and denominator of second fraction:101 31
23/57 + 101/31 = 6470/1767
23/57 - 101/31 = 5044/-1767
23/57 * 101/31 = 2323/1767
23/57 / 101/31 = 713/5757

Go again? (Y or y to continue):n
  Consider the following code for representing fractions.

Fraction.h

Line: 128 to 155
 } %ENDCODE%
Changed:
<
<
  • add_fracs_inq_2.png:
>
>
Runs to turn in:
Fraction 1 Fraction 2
1/2 3/5
52/43 63/21
3542/431 45785/3241
8/3 123/43

Grading Table

RequirementGrading CommentsPointsScore
Easy to use user interface 10
C++ code includes comments, with project information at top, pre and post conditions for each functions and other cmments as needed. 10
The C++ code has good formatting, indentation, and organization. 10
Good variable and function names, appropriate use of constants rather then literal numbers. 10
Classes: Correctly defined and well organized 20
Runs: Run examples from trials with correct output 40
Total 100

Solutions

 
META FILEATTACHMENT attachment="xl-2016-encryption-1.jpg" attr="" comment="" date="1475261034" name="xl-2016-encryption-1.jpg" path="xl-2016-encryption-1.jpg" size="103604" user="JimSkon" version="1"
META FILEATTACHMENT attachment="add_fracs_inq_2.png" attr="" comment="" date="1507471517" name="add_fracs_inq_2.png" path="add_fracs_inq_2.png" size="17268" user="JimSkon" version="1"

Revision 142017-10-08 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Lab 8 - Operator Overloading

Due: 11:55pm

Changed:
<
<
Moodle Link


Consider the following code for representing fractions.
>
>
Moodle Link

add_fracs_inq_2.png
Your goal is to create a class for storing and manipulating fractions. We wish to write code to add, subtract, multiply, and divide fractions. In addition we wish to overload the traditional binary arithemitic operators so that we can create code like below:
 
Changed:
<
<
Fraction.h
>
>
<-- SyntaxHighlightingPlugin -->
int main() {
    int n,d;
    cout << "Fraction test program." << endl;
    cout << "Enter numerator and denominator of first fraction:";
    cin >> n >> d;
    Fraction a(n,d);
    cout << "Enter numerator and denominator of second fraction:";
    cin >> n >> d;
    Fraction b(n,d);    
    Fraction sum = a+b;
    Fraction diff = a-b;
    Fraction prod = a*b;
    Fraction quotient  = a/b;

    
    // Display Results
    cout << a.to_string() << " + " << b.to_string() << " = " << sum.to_string() << endl;
    cout << a.to_string() << " - " << b.to_string() << " = " << diff.to_string() << endl;
    cout << a.to_string() << " * " << b.to_string() << " = " << prod.to_string() << endl;
    cout << a.to_string() << " / " << b.to_string() << " = " << quotient.to_string() << endl;

    return 0;
}
<-- end SyntaxHighlightingPlugin -->
=

Consider the following code for representing fractions.

Fraction.h

  %CODE{"c++"}% #include
Line: 20 to 49
  Fraction(int n, int d); ~Fraction(void);

Added:
>
>
// Test functions string to_string();

  // Overloaded operators // +, -, *, and /
Line: 37 to 70
  int gcd(int a, int b)const;

// Friend functions

Changed:
<
<
// ...
>
>
friend Fraction add(Fraction a, Fraction b);
 }; %ENDCODE%
Changed:
<
<
Fraction.cpp
>
>

Fraction.cpp

  %CODE{"c++"}% #include
Line: 49 to 81
  using namespace std;
Changed:
<
<
// Default constructor Fraction::Fraction(void) { numerator = 0; denominator = 1;
>
>
int main() { int n,d; cout << "Fraction test program." << endl; cout << "Enter numerator and denominator of first fraction:"; cin >> n >> d; Fraction a(n,d); cout << "Enter numerator and denominator of second fraction:"; cin >> n >> d; Fraction b(n,d); Fraction c = add(a,b); //Will be changed to use + operator //Fraction c = a+b;

// Display Result cout << a.to_string() << " + " << b.to_string() << " = " << c.to_string() << endl; return 0;

 }
Changed:
<
<
// Constructor Fraction::Fraction(int n, int d) { if (d == 0){ denominator = 1; numerator = 0; }else if (d < 0){ denominator = d * -1; if (n > 0){ numerator = n * -1; } }else{ numerator = n; denominator = d; } }
>
>
%ENDCODE%
 
Changed:
<
<
Fraction::~Fraction(void) { }
>
>

factions.cpp

%CODE{"c++"}% #include #include "Fraction.h"
 
Changed:
<
<
// Returns the greatest common divisor of two integers int Fraction::gcd(int a, int b)const{ while (b = 0){ int temp = b; b = a % b; a = temp; } return a; }
>
>
using namespace std;
 
Changed:
<
<
// Simplifies the calling fraction void Fraction::simplify(){ int factor = gcd(numerator, denominator); numerator = numerator / factor; denominator = denominator / factor;
>
>
int main() { int n,d; cout << "Fraction test program." << endl; cout << "Enter numerator and denominator of first fraction:"; cin >> n >> d; Fraction a(n,d); cout << "Enter numerator and denominator of second fraction:"; cin >> n >> d; Fraction b(n,d); Fraction c = add(a,b); //Will be changed to use + operator //Fraction c = a+b;

// Display Result cout << a.to_string() << " + " << b.to_string() << " = " << c.to_string() << endl; return 0;

 }
Deleted:
<
<
// Overloaded Operators /* * In each case: (num1/den1) op (num2/den2) * * PLUS OPERATOR * result = (num1 * den2 + num2 * den1) / (den1 * den2) * * MINUS OPERATOR * result = (num1 * den2 - num2 * den1) / (den1 * den2) * * MULIPLICATION OPERATOR * result = (num1 * num2) / (den1 * den2) * * DIVISION OPERATOR * result = (num1 * den2) / (den1 * num2) */
 %ENDCODE%
Added:
>
>
  • add_fracs_inq_2.png:
 
META FILEATTACHMENT attachment="xl-2016-encryption-1.jpg" attr="" comment="" date="1475261034" name="xl-2016-encryption-1.jpg" path="xl-2016-encryption-1.jpg" size="103604" user="JimSkon" version="1"
Added:
>
>
META FILEATTACHMENT attachment="add_fracs_inq_2.png" attr="" comment="" date="1507471517" name="add_fracs_inq_2.png" path="add_fracs_inq_2.png" size="17268" user="JimSkon" version="1"

Revision 132017-10-03 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"
Changed:
<
<

Lab 8 - Encryption and Vectors

Due: April 13, 11:55pm

>
>

Lab 8 - Operator Overloading

Due: 11:55pm

 
Changed:
<
<
Moodle Link

xl-2016-encryption-1.jpg

Instructions

  • There are two different problems for this assignment:
    1. Decrypt a secret message
    2. Sort with Vectors
  • Turn in the code (a cpp file or ideone.com link), and the run outputs as requested below.

Grading

Grading Table

Requirement Grading Comments Points Score
Encryption: Easy to use user interface   5  
Encryption: C++ code includes comments, with project information at top, pre and post conditions for each functions and other comments as needed.   5  
Encryption: The C++ code has good formatting, indentation, and organization.   5  
Encryption: Good variable and function names, appropriate use of constants rather then literal numbers.   5  
Encryption: Functions: Logic divided up into cohesive functions with a single purpose   15  
Encryption: Runs: Run examples from trials with correct output   25  
Sorting with Vectors: Complete and proper use of vector template   10  
Sorting with Vectors: Good formatted and commented code   10  
Sorting with Vectors: Complete and correct test runs   20  
Total   100  

Problem 1 - Decrypt a secret message

Your country is at war and your enemies are using a secret code to communicate with each other. You have managed to intercept a message that reads as follows:

:mmZ\dxZmx]Zpgy

The message is obviously encrypted using the enemy’s secret code. You have just learned that their encryption method is based upon the ASCII code. Appendix 3 shows the ASCII character set. Individual characters in a string are encoded using this system. For example, the letter “A” is encoded using the number 65 and “B” is encoded using the number 66.

Your enemy’s secret code takes each letter of the message and encrypts it as follows:

If (OriginalChar + Key > 126) then
    EncryptedChar = 32 + ((OriginalChar + Key) – 127)
Else
    EncryptedChar = (OriginalChar + Key)

For example, if the enemy uses Key = 10 then the message “Hey” would be encrypted as:

Character ASCII code
H 72
e 101
y 121

  • Encrypted H = (72 + 10) = 82 = R in ASCII
  • Encrypted e = (101 + 10) = 111 = o in ASCII
  • Encrypted y = 32 + ((121 + 10) – 127) = 36 = $ in ASCII
Consequently, “Hey” would be transmitted as “Ro$.”
>
>
Moodle Link


Consider the following code for representing fractions.
 
Changed:
<
<
Write a program that decrypts the intercepted message above. You will need to write a function that, giving a character, reverses the encryption operation.

The problem is that you only know that the key used to do the encryption is a number between 1 and 100. Your program will need to try all possible keys to decode the message. When you try the valid key, the message will make sense. For all other keys, the message will appear as gibberish.

You should use the string class for this lab. Two functions you might create are:

>
>
Fraction.h
  %CODE{"c++"}%
Deleted:
<
<
// Use key to decrypt encryptedChar, returning the character. char decryptChar(char encryptedChar, int key);

// Use key to decrypt encryptedStr, returning the decrypted string. // This function will use decryptChar above string decryptString(string encryptedStr, int key); %ENDCODE%

Below is an example of some string processing: %CODE{"c++"}% #include

 #include
Deleted:
<
<
 using namespace std;
Changed:
<
<
string reverse(string s) { int len = s.length(); int last = len-1; string r(s);
>
>
class Fraction { public: // Constructors and Destructor Fraction(void); Fraction(int n, int d); ~Fraction(void);
 
Changed:
<
<
for (int i = 0; i < len; i++) { r[last - i] = s[i]; } return r; }
>
>
// Overloaded operators // +, -, *, and /
 
Deleted:
<
<
int main() { string str; cout << "Enter a string: "; getline(cin, str); cout << reverse(str) << endl;
 
Changed:
<
<
return 0; }
>
>
private: int numerator; //the "top" of the fraction int denominator; //the "bottom" of the fraction
 
Added:
>
>
// PRE: // POST: Fraction is simplified, e.g. 3/9, simplifies to 1/3 void simplify();
 
Changed:
<
<
%ENDCODE%
>
>
// PRE: // POST: Returns the greatest common divisor of a and b int gcd(int a, int b)const;
 
Changed:
<
<

Problem 2 - Sort with Vectors

>
>
// Friend functions // ... }; %ENDCODE%
 
Changed:
<
<
Recall the sort program we studied earlier:

Sort Array

>
>
Fraction.cpp
  %CODE{"c++"}% #include
Added:
>
>
#include "Fraction.h"
 using namespace std;
Deleted:
<
<
void displayValues(int scores[], int scoreCount) { for (int i = 0 ; i < scoreCount ; i++) { cout << scores[i] << " "; } cout << endl; }
 
Changed:
<
<
void inputValues(int scores[], int aSize, int& scoreCount) { int i=0; do { cin >> scores[i++]; } while (scores[i-1] > 0 && i < aSize); if (scores[i-1] < 0) { i--; } scoreCount = i; } void swap_values(int& v1, int& v2)
>
>
// Default constructor Fraction::Fraction(void)
 {
Changed:
<
<
int temp; temp = v1; v1 = v2; v2 = temp;
>
>
numerator = 0; denominator = 1;
 }

Changed:
<
<
int index_of_smallest(const int a[], int start_index, int number_used)
>
>
// Constructor Fraction::Fraction(int n, int d)
 {
Changed:
<
<
int min = a[start_index]; int index_of_min = start_index; for (int index = start_index + 1; index < number_used; index++) if (a[index] < min) { min = a[index]; index_of_min = index; //min is the smallest of a[start_index] through a[index]
>
>
if (d == 0){ denominator = 1; numerator = 0; }else if (d < 0){ denominator = d * -1; if (n > 0){ numerator = n * -1; } }else{ numerator = n; denominator = d;
  }
Deleted:
<
<
return index_of_min;
 }
Changed:
<
<
void sort(int a[], int number_used)
>
>
Fraction::~Fraction(void)
 {
Deleted:
<
<
int index_of_next_smallest; for (int index = 0; index < number_used - 1; index++) {//Place the correct value in a[index]:

index_of_next_smallest = index_of_smallest(a, index, number_used);

swap_values(a[index], a[index_of_next_smallest]); //a[0] <= a[1] <=...<= a[index] are the smallest of the original array //elements. The rest of the elements are in the remaining positions. }

 }
Changed:
<
<
const int MAX = 100; int main() { int theNums[MAX]; int ncount; cout << "Input up to " << MAX << " Values to sort, -1 to end: "; inputValues(theNums, MAX, ncount); displayValues(theNums, ncount); sort(theNums, ncount); displayValues(theNums, ncount);

return 0;

>
>
// Returns the greatest common divisor of two integers int Fraction::gcd(int a, int b)const{ while (b = 0){ int temp = b; b = a % b; a = temp;
 }
Changed:
<
<
%ENDCODE%

Rewrite this program to use only vectors (not c arrays). Below is a sample program that uses vectors.

Test Run data

  1. 34,65,23,54,93,36,13,78,34,91,23,34,55,73
  2. 6,5,6,2,7,2,5,4,7,4,5,3,2,3

Using a Vector

%CODE{"c++"}% //DISPLAY 8.9 Using a Vector #include #include using namespace std;

int main( ) { vector v; cout << "Enter a list of positive numbers.\n" << "Place a negative number at the end.\n";

int next; cin >> next; while (next > 0) { v.push_back(next); cout << next << " added. "; cout << "v.size( ) = " << v.size( ) << endl; cin >> next;

>
>
return a;
  }
Changed:
<
<
cout << "You entered:\n"; for (unsigned int i = 0; i < v.size( ); i++) cout << v[i] << " "; cout << endl;

return 0;

>
>
// Simplifies the calling fraction void Fraction::simplify(){ int factor = gcd(numerator, denominator); numerator = numerator / factor; denominator = denominator / factor;
 }
Changed:
<
<
%ENDCODE%
>
>
// Overloaded Operators /* * In each case: (num1/den1) op (num2/den2) * * PLUS OPERATOR * result = (num1 * den2 + num2 * den1) / (den1 * den2) * * MINUS OPERATOR * result = (num1 * den2 - num2 * den1) / (den1 * den2) * * MULIPLICATION OPERATOR * result = (num1 * num2) / (den1 * den2) * * DIVISION OPERATOR * result = (num1 * den2) / (den1 * num2) */
 
Changed:
<
<
Solution
>
>
%ENDCODE%
 
META FILEATTACHMENT attachment="xl-2016-encryption-1.jpg" attr="" comment="" date="1475261034" name="xl-2016-encryption-1.jpg" path="xl-2016-encryption-1.jpg" size="103604" user="JimSkon" version="1"

Revision 122017-04-12 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Lab 8 - Encryption and Vectors

Due: April 13, 11:55pm

Changed:
<
<
Moodle Link

xl-2016-encryption-1.jpg
>
>
Moodle Link

xl-2016-encryption-1.jpg
 

Instructions

Added:
>
>
  • There are two different problems for this assignment:
    1. Decrypt a secret message
    2. Sort with Vectors
 
  • Turn in the code (a cpp file or ideone.com link), and the run outputs as requested below.

Grading

Grading Table

Line: 22 to 24
 
Sorting with Vectors: Complete and correct test runs   20  
Total   100  
Changed:
<
<

Problem 1

>
>

Problem 1 - Decrypt a secret message

  Your country is at war and your enemies are using a secret code to communicate with each other. You have managed to intercept a message that reads as follows:
Line: 95 to 98
  %ENDCODE%
Changed:
<
<

Problem 2 - Using Vectors

>
>

Problem 2 - Sort with Vectors

  Recall the sort program we studied earlier:

Sort Array

Revision 112017-01-17 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Lab 8 - Encryption and Vectors

Changed:
<
<

Due: Nov 10, 11:55pm

>
>

Due: April 13, 11:55pm

Moodle Link

 

xl-2016-encryption-1.jpg

Instructions

  • Turn in the code (a cpp file or ideone.com link), and the run outputs as requested below.

Revision 102016-11-08 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Lab 8 - Encryption and Vectors

Line: 52 to 52
  The problem is that you only know that the key used to do the encryption is a number between 1 and 100. Your program will need to try all possible keys to decode the message. When you try the valid key, the message will make sense. For all other keys, the message will appear as gibberish.
Changed:
<
<
You should use the string class for this lab. Below is an example of some string processing:
>
>
You should use the string class for this lab. Two functions you might create are:
<-- SyntaxHighlightingPlugin -->
// Use key to decrypt encryptedChar, returning the character.
char decryptChar(char encryptedChar, int key);

// Use key to decrypt encryptedStr, returning the decrypted string.
// This function will use decryptChar above
string decryptString(string encryptedStr, int key);
 
<-- end SyntaxHighlightingPlugin -->

Below is an example of some string processing:

  %CODE{"c++"}% #include
Line: 198 to 209
  return 0; }
Changed:
<
<
%ENDCODE%
>
>
%ENDCODE%
 
Changed:
<
<
Solution
>
>
Solution
 
META FILEATTACHMENT attachment="xl-2016-encryption-1.jpg" attr="" comment="" date="1475261034" name="xl-2016-encryption-1.jpg" path="xl-2016-encryption-1.jpg" size="103604" user="JimSkon" version="1"

Revision 92016-11-06 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Lab 8 - Encryption and Vectors

Line: 198 to 198
  return 0; }
Changed:
<
<
%ENDCODE%
>
>
%ENDCODE%

Solution

 
META FILEATTACHMENT attachment="xl-2016-encryption-1.jpg" attr="" comment="" date="1475261034" name="xl-2016-encryption-1.jpg" path="xl-2016-encryption-1.jpg" size="103604" user="JimSkon" version="1"

Revision 82016-11-03 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"
Changed:
<
<

Lab 8 - Encryption

>
>

Lab 8 - Encryption and Vectors

 

Due: Nov 10, 11:55pm


xl-2016-encryption-1.jpg

Instructions

  • Turn in the code (a cpp file or ideone.com link), and the run outputs as requested below.
Deleted:
<
<
  • Remember to format the code as described and the book and text, and to include comments including complete commetns at the beginning of the program.
 

Grading

Added:
>
>

Grading Table

Requirement Grading Comments Points Score
Encryption: Easy to use user interface   5  
Encryption: C++ code includes comments, with project information at top, pre and post conditions for each functions and other comments as needed.   5  
Encryption: The C++ code has good formatting, indentation, and organization.   5  
Encryption: Good variable and function names, appropriate use of constants rather then literal numbers.   5  
Encryption: Functions: Logic divided up into cohesive functions with a single purpose   15  
Encryption: Runs: Run examples from trials with correct output   25  
Sorting with Vectors: Complete and proper use of vector template   10  
Sorting with Vectors: Good formatted and commented code   10  
Sorting with Vectors: Complete and correct test runs   20  
Total   100  
 
Deleted:
<
<
Feature %
Program correctness and completeness with respect to defination 70%
Code Format (Indenting, variable names) 10%
Code Comments 10%
Turning in the run with your solution to the encoding.. 10%
 

Problem 1

Changed:
<
<
Your country is at war and your enemies are using a secret code to com- municate with each other. You have managed to intercept a message that reads as follows:
>
>
Your country is at war and your enemies are using a secret code to communicate with each other. You have managed to intercept a message that reads as follows:
  :mmZ\dxZmx]Zpgy
Line: 42 to 48
 
  • Encrypted y = 32 + ((121 + 10) – 127) = 36 = $ in ASCII
Consequently, “Hey” would be transmitted as “Ro$.”
Changed:
<
<
Write a program that decrypts the intercepted message. You only know that the key used is a number between 1 and 100. Your program should try to decode the message using all possible keys between 1 and 100. When you try the valid key, the message will make sense. For all other keys, the message will appear as gibberish.
>
>
Write a program that decrypts the intercepted message above. You will need to write a function that, giving a character, reverses the encryption operation.
 
Changed:
<
<

Problem 2

>
>
The problem is that you only know that the key used to do the encryption is a number between 1 and 100. Your program will need to try all possible keys to decode the message. When you try the valid key, the message will make sense. For all other keys, the message will appear as gibberish.
 
Changed:
<
<
Write a sorting function that is similar to Display 7.12 in Chapter 7 except that it has an argument for a vector of ints rather than an array. This function will not need a parameter like number_used as in Display 7.12, since a vector can determine the number used with the member function size(). This sort function will have only this one parameter, which will be of a vector type. Use the selection sort algorithm (which was used in Display 7.12).
>
>
You should use the string class for this lab. Below is an example of some string processing:
<-- SyntaxHighlightingPlugin -->
#include <iostream>
#include <string>

using namespace std;

string reverse(string s) {
    int len = s.length();
    int last = len-1;
    string r(s);

    for (int i = 0; i < len; i++) {
        r[last - i] = s[i];
    }
    return r;
}

int main() {
    string str;
    cout << "Enter a string: ";
    getline(cin, str);
    cout << reverse(str) << endl;

    return 0;
}


 
<-- end SyntaxHighlightingPlugin -->

Problem 2 - Using Vectors

Recall the sort program we studied earlier:

Sort Array

%CODE{"c++"}% #include using namespace std; void displayValues(int scores[], int scoreCount) { for (int i = 0 ; i < scoreCount ; i++) { cout << scores[i] << " "; } cout << endl; }

void inputValues(int scores[], int aSize, int& scoreCount) { int i=0; do { cin >> scores[i++]; } while (scores[i-1] > 0 && i < aSize); if (scores[i-1] < 0) { i--; } scoreCount = i; } void swap_values(int& v1, int& v2) { int temp; temp = v1; v1 = v2; v2 = temp; }

int index_of_smallest(const int a[], int start_index, int number_used) { int min = a[start_index]; int index_of_min = start_index; for (int index = start_index + 1; index < number_used; index++) if (a[index] < min) { min = a[index]; index_of_min = index; //min is the smallest of a[start_index] through a[index] }

return index_of_min; }

void sort(int a[], int number_used) { int index_of_next_smallest; for (int index = 0; index < number_used - 1; index++) {//Place the correct value in a[index]:

index_of_next_smallest = index_of_smallest(a, index, number_used);

swap_values(a[index], a[index_of_next_smallest]); //a[0] <= a[1] <=...<= a[index] are the smallest of the original array //elements. The rest of the elements are in the remaining positions. } }

const int MAX = 100; int main() { int theNums[MAX]; int ncount; cout << "Input up to " << MAX << " Values to sort, -1 to end: "; inputValues(theNums, MAX, ncount); displayValues(theNums, ncount); sort(theNums, ncount); displayValues(theNums, ncount);

 
Changed:
<
<
Sort the following lists and turn in:
>
>
return 0; }
 
Added:
>
>
%ENDCODE%

Rewrite this program to use only vectors (not c arrays). Below is a sample program that uses vectors.

Test Run data

 
  1. 34,65,23,54,93,36,13,78,34,91,23,34,55,73
  2. 6,5,6,2,7,2,5,4,7,4,5,3,2,3
Changed:
<
<
  • xl-2016-encryption-1.jpg:
>
>

Using a Vector

<-- SyntaxHighlightingPlugin -->
//DISPLAY 8.9 Using a Vector
#include <iostream>
#include <vector>
using namespace std;

int main( )
{
    vector<int> v;
    cout << "Enter a list of positive numbers.\n"
         << "Place a negative number at the end.\n";

    int next;
    cin >> next;
    while (next > 0)
    {
        v.push_back(next);
        cout << next << " added. ";
        cout << "v.size( ) = " << v.size( ) << endl;
        cin >> next;
    }

    cout << "You entered:\n";
    for (unsigned int i = 0; i < v.size( ); i++)
        cout << v[i] << " ";
    cout << endl;

    return 0;
}

 
<-- end SyntaxHighlightingPlugin -->
 
META FILEATTACHMENT attachment="xl-2016-encryption-1.jpg" attr="" comment="" date="1475261034" name="xl-2016-encryption-1.jpg" path="xl-2016-encryption-1.jpg" size="103604" user="JimSkon" version="1"

Revision 72016-09-30 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"
Changed:
<
<

Lab 8

>
>

Lab 8 - Encryption

 

Due: Nov 10, 11:55pm

Added:
>
>

xl-2016-encryption-1.jpg
 

Instructions

  • Turn in the code (a cpp file or ideone.com link), and the run outputs as requested below.
  • Remember to format the code as described and the book and text, and to include comments including complete commetns at the beginning of the program.
Line: 35 to 37
 
e 101
y 121
Changed:
<
<
  • Encrypted H = (72 + 10) = 82 = R in ASCII
  • Encrypted e = (101 + 10) = 111 = o in ASCII
  • Encrypted y = 32 + ((121 + 10) – 127) = 36 = $ in ASCII
>
>
  • Encrypted H = (72 + 10) = 82 = R in ASCII
  • Encrypted e = (101 + 10) = 111 = o in ASCII
  • Encrypted y = 32 + ((121 + 10) – 127) = 36 = $ in ASCII
 Consequently, “Hey” would be transmitted as “Ro$.”

Write a program that decrypts the intercepted message. You only know that the key used is a number between 1 and 100. Your program should try to decode the message using all possible keys between 1 and 100. When you try the valid key, the message will make sense. For all other keys, the message will appear as gibberish.

Line: 48 to 50
  Sort the following lists and turn in:
Deleted:
<
<
  1. 34,65,23,54,93,36,13,78,34,91,23,34,55,73
  2. 6,5,6,2,7,2,5,4,7,4,5,3,2,3
 \ No newline at end of file
Added:
>
>
  1. 34,65,23,54,93,36,13,78,34,91,23,34,55,73
  2. 6,5,6,2,7,2,5,4,7,4,5,3,2,3
  • xl-2016-encryption-1.jpg:

META FILEATTACHMENT attachment="xl-2016-encryption-1.jpg" attr="" comment="" date="1475261034" name="xl-2016-encryption-1.jpg" path="xl-2016-encryption-1.jpg" size="103604" user="JimSkon" version="1"

Revision 62016-08-24 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Lab 8

Changed:
<
<

Due: Nov 13, 11:55pm

>
>

Due: Nov 10, 11:55pm

 

Instructions

  • Turn in the code (a cpp file or ideone.com link), and the run outputs as requested below.
  • Remember to format the code as described and the book and text, and to include comments including complete commetns at the beginning of the program.
Line: 18 to 18
  :mmZ\dxZmx]Zpgy
Changed:
<
<
The message is obviously encrypted using the enemy’s secret code. You have just learned that their encryption method is based upon the ASCII code. Appendix 3 shows the ASCII character set. Individual characters in a string are encoded using this system. For example, the letter “A” is encoded using the number 65 and “B” is encoded using the number 66.
>
>
The message is obviously encrypted using the enemy’s secret code. You have just learned that their encryption method is based upon the ASCII code. Appendix 3 shows the ASCII character set. Individual characters in a string are encoded using this system. For example, the letter “A” is encoded using the number 65 and “B” is encoded using the number 66.

Your enemy’s secret code takes each letter of the message and encrypts it as follows:

 
Deleted:
<
<
Your enemy’s secret code takes each letter of the message and encrypts it as follows:
 
If (OriginalChar + Key > 126) then

Changed:
<
<
EncryptedChar = 32 + ((OriginalChar + Key) – 127)
>
>
EncryptedChar = 32 + ((OriginalChar + Key) – 127)
 Else EncryptedChar = (OriginalChar + Key)
Changed:
<
<
For example, if the enemy uses Key = 10 then the message “Hey” would be encrypted as:
>
>
For example, if the enemy uses Key = 10 then the message “Hey” would be encrypted as:
 
Character ASCII code
H 72
Line: 36 to 37
 
  • Encrypted H = (72 + 10) = 82 = R in ASCII
  • Encrypted e = (101 + 10) = 111 = o in ASCII
Changed:
<
<
  • Encrypted y = 32 + ((121 + 10) – 127) = 36 = $ in ASCII
Consequently, “Hey” would be transmitted as “Ro$.”
>
>
  • Encrypted y = 32 + ((121 + 10) – 127) = 36 = $ in ASCII
Consequently, “Hey” would be transmitted as “Ro$.”
  Write a program that decrypts the intercepted message. You only know that the key used is a number between 1 and 100. Your program should try to decode the message using all possible keys between 1 and 100. When you try the valid key, the message will make sense. For all other keys, the message will appear as gibberish.

Revision 52015-11-10 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Lab 8

Due: Nov 13, 11:55pm

Line: 43 to 43
 

Problem 2

Changed:
<
<
Write a sorting function that is similar to Display 7.12 in Chapter 7 except that it has an argument for a vector of ints rather than an array. This function will not need a parameter like number_used as in Display 7.12, since a vector can determine the number used with the member function size(). This sort function will have only this one parameter, which will be of a vec- tor type. Use the selection sort algorithm (which was used in Display 7.12).
>
>
Write a sorting function that is similar to Display 7.12 in Chapter 7 except that it has an argument for a vector of ints rather than an array. This function will not need a parameter like number_used as in Display 7.12, since a vector can determine the number used with the member function size(). This sort function will have only this one parameter, which will be of a vector type. Use the selection sort algorithm (which was used in Display 7.12).
  Sort the following lists and turn in:

Revision 42015-11-10 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Lab 8

Changed:
<
<

Due: Nov 12, 11:55pm

>
>

Due: Nov 13, 11:55pm

 

Instructions

  • Turn in the code (a cpp file or ideone.com link), and the run outputs as requested below.
  • Remember to format the code as described and the book and text, and to include comments including complete commetns at the beginning of the program.

Revision 32015-11-01 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Lab 8

Due: Nov 12, 11:55pm

Line: 41 to 40
 Consequently, “Hey” would be transmitted as “Ro$.”

Write a program that decrypts the intercepted message. You only know that the key used is a number between 1 and 100. Your program should try to decode the message using all possible keys between 1 and 100. When you try the valid key, the message will make sense. For all other keys, the message will appear as gibberish.

Added:
>
>

Problem 2

Write a sorting function that is similar to Display 7.12 in Chapter 7 except that it has an argument for a vector of ints rather than an array. This function will not need a parameter like number_used as in Display 7.12, since a vector can determine the number used with the member function size(). This sort function will have only this one parameter, which will be of a vec- tor type. Use the selection sort algorithm (which was used in Display 7.12).

Sort the following lists and turn in:

  1. 34,65,23,54,93,36,13,78,34,91,23,34,55,73
  2. 6,5,6,2,7,2,5,4,7,4,5,3,2,3

Revision 22015-11-01 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"
Changed:
<
<

Lab 7

>
>

Lab 8

 

Due: Nov 12, 11:55pm

Instructions

Line: 12 to 12
 
Program correctness and completeness with respect to defination 70%
Code Format (Indenting, variable names) 10%
Code Comments 10%
Changed:
<
<
Turning in complete run of every function at least twice with difference values. 10%
>
>
Turning in the run with your solution to the encoding.. 10%
 

Problem 1

Your country is at war and your enemies are using a secret code to com- municate with each other. You have managed to intercept a message that reads as follows:

Line: 41 to 41
 Consequently, “Hey” would be transmitted as “Ro$.”

Write a program that decrypts the intercepted message. You only know that the key used is a number between 1 and 100. Your program should try to decode the message using all possible keys between 1 and 100. When you try the valid key, the message will make sense. For all other keys, the message will appear as gibberish.

Deleted:
<
<

Problem 2

Write a sorting function that is similar to Display 7.12 in Chapter 7 except that it has an argument for a vector of ints rather than an array. This function will not need a parameter like number_used as in Display 7.12, since a vector can determine the number used with the member function size(). This sort function will have only this one parameter, which will be of a vec- tor type. Use the selection sort algorithm (which was used in Display 7.12).

Revision 12015-10-29 - JimSkon

Line: 1 to 1
Added:
>
>
META TOPICPARENT name="WebHome"

Lab 7

Due: Nov 12, 11:55pm

Instructions

  • Turn in the code (a cpp file or ideone.com link), and the run outputs as requested below.
  • Remember to format the code as described and the book and text, and to include comments including complete commetns at the beginning of the program.

Grading

Feature %
Program correctness and completeness with respect to defination 70%
Code Format (Indenting, variable names) 10%
Code Comments 10%
Turning in complete run of every function at least twice with difference values. 10%

Problem 1

Your country is at war and your enemies are using a secret code to com- municate with each other. You have managed to intercept a message that reads as follows:

:mmZ\dxZmx]Zpgy

The message is obviously encrypted using the enemy’s secret code. You have just learned that their encryption method is based upon the ASCII code. Appendix 3 shows the ASCII character set. Individual characters in a string are encoded using this system. For example, the letter “A” is encoded using the number 65 and “B” is encoded using the number 66.

Your enemy’s secret code takes each letter of the message and encrypts it as follows:

If (OriginalChar + Key > 126) then
    EncryptedChar = 32 + ((OriginalChar + Key) – 127)
Else
    EncryptedChar = (OriginalChar + Key)

For example, if the enemy uses Key = 10 then the message “Hey” would be encrypted as:

Character ASCII code
H 72
e 101
y 121

  • Encrypted H = (72 + 10) = 82 = R in ASCII
  • Encrypted e = (101 + 10) = 111 = o in ASCII
  • Encrypted y = 32 + ((121 + 10) – 127) = 36 = $ in ASCII
Consequently, “Hey” would be transmitted as “Ro$.”

Write a program that decrypts the intercepted message. You only know that the key used is a number between 1 and 100. Your program should try to decode the message using all possible keys between 1 and 100. When you try the valid key, the message will make sense. For all other keys, the message will appear as gibberish.

Problem 2

Write a sorting function that is similar to Display 7.12 in Chapter 7 except that it has an argument for a vector of ints rather than an array. This function will not need a parameter like number_used as in Display 7.12, since a vector can determine the number used with the member function size(). This sort function will have only this one parameter, which will be of a vec- tor type. Use the selection sort algorithm (which was used in Display 7.12).
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2019 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback