# EECS 183 Lab 6: Exam Practice

### Lab assignment Due Monday, March 1, 2021, 11:59 pm Eastern

In this lab, you are completing a practice exam with your peers in preparation for Exam 1.

# Prerequisite

Attend the discussion with your lab instructor at your assigned lab time. Check here for the lab section Zoom link

You will complete this lab in small groups of about 4 students. For all labs in EECS 183, to receive a grade, every student must individually submit the Lab assingment.

## Starter Files

The IDE setup tutorials for Visual Studio and XCode include a video about how to set up a project using the starter files. You can access the tutorials here:

• `calculateFrequency.cpp`, `isValidPin.cpp`, `lab6.cpp`, `registerPinc.cpp`: source code files you will use to submit your exam practice answers for grading.

IMPORTANT: For this lab, you must include all four files in a single project in Xcode or Visual Studio.

# Lab Assignment

• To complete this portion of the lab, you need to do the following steps:
1. Start a new project with your IDE using the starter files.
2. For each of the four practice exam questions below:
• Solve the practice exam question with your group.
• Transfer your solution to the starter code and submit to the autograder to check your solutiuon.
• `calculateFrequency()`
• `testisValidPin()`
• `isValidPin()`
• `registerPin()`

NOTE: The questions below appeared on a previous EECS 183 Exam 1.

## calculateFrequency()

write the implementation for the following RME and declaration.

```/**
* Requires: (1) num > 0
*           (2) num has no leading zeros
*           (3) 0 <= d <= 9
* Modifies: nothing
* Effects : returns the number of times digit d appears in num
*
* Note:     Your implementation should work for values of num
*           that contain any number of digits.
*           Here is a breakdown of the iterations needed for
*           calculateFrequency(133, 3), which should return 2
*           because the digit 3 appears in 133 twice.
*
*           1. 133 / 1 = 133 --> 133 % 10 = 3 ⇒ rightmost digit of 133 is 3
*           2. 133 / 10 = 13 --> 13 % 10 = 3 ⇒ next digit of 133 is 3
*           3. 133 / 100 = 1 --> 1 % 10 = 1 ⇒ next digit of 133 is 1
*           4. 133 / 1000 = 0 --> no additional digits in 133
**/
int calculateFrequency(int num, int d);
```

NOTE: We have written test cases for this function for you in `lab6.cpp`. You do not need to write test cases for this function.

• Add your definition to the file `calculateFrequency.cpp`, removing the `return 0;` statement after adding your solution.

• You can test your solution with your IDE. Run the program and select 1 at the menu.

• If your calculateFrequency fails any of our tests, a display message will be shown like the following…

```FAILED: calculateFrequency(123, 2)
Expected return value: 1
Actual return value: 0
```
• If your calculateFrequency passes all tests, the output from your program should look like the following…
```-------------------------------
EECS 183 Lab 6 Menu Options
-------------------------------
1 Execute testing for calculateFrequency()
2 Execute test cases for isValidPin()
3 Execute registerPin()
Any other number - Exit lab
Choice --> 1
Testing calculateFrequency()
Finished testing calculateFrequency()
```

## isValidPin()

You work for a software security company and are implementing a program to validate a user pin that should have exactly five non-repeating digits. Here is the RME and declaration for the function:

```/**
* Requires: pin has no leading zeros
* Modifies: nothing
* Effects : (1) returns false if pin has less than five digits;
*           (2) returns false if pin has more than five digits;
*           (3) returns false if pin has a repeating digit;
*           (4) returns true if pin contains exactly five
*               non-repeating digits;
**/
bool isValidPin(int pin);
```

Write test cases in the `testisValidPin()` function below so that the presence of bugs in isValidPin would be evident from the program’s output. You should account for all cases spelled out in the RME for isValidPin .

```/**
* test.cpp
*
* <#Name#>
* <#Uniqname#>
*
* EECS 183: Lab 6 - Exam Review
*
* Testing functions for your isValidPin.cpp implementation.
* Holds the definition of required testing function.
* We have stubbed the required function for you.
*
* NOTE: You WILL submit this file to the autograder
*/

#include <iostream>
#include <string>

using namespace std;

bool isValidPin(int pin);

void testisValidPin() {

// TODO: write your test cases for isValidPin here

return;
}
```
• Add your test cases to the file `test.cpp`.

• You can test your test cases with your IDE. Run the program and select 2 at the menu.

NOTE: Your test cases should show there are bugs in the function stub for `isValidPin()` provided for you in `isValidPin.cpp`. You will write that function next.

## isValidPin()

write the implementation for the `isValidPin()` function. Your implementation MUST use `calculateFrequency()` to determine whether a digit exists or repeats in pin . We include copy of the RME and declaration here for reference:

```/**
* Requires: pin has no leading zeros
* Modifies: nothing
* Effects : (1) returns false if pin has less than five digits;
*           (2) returns false if pin has more than five digits;
*           (3) returns false if pin has a repeating digit;
*           (4) returns true if pin contains exactly five
*               non-repeating digits;
**/
bool isValidPin(int pin);
```
• Add your definition to the file `isValidPin.cpp`.

• You can test your test cases with your IDE. Run the program and select 2 at the menu.

• Your test cases in test.cpp should tell you if there are any bugs in your implementation.

## registerPin()

You will now implement a program that will repeatedly prompt a user until they enter a valid pin. The function definition has been partially completed for you, add your code where the comment indicates. Assume isValidPin has been implemented correctly.

```/**
* registerPin.cpp
*
* <#Name#>
* <#Uniqname#>
*
* EECS 183: Lab 6 - Exam Review
*
* Holds the definition registerPin function.
* We have stubbed the required function for you.
*
* NOTE: You WILL submit this file to the autograder
*/

#include <iostream>
#include <string>

using namespace std;

bool isValidPin(int pin);

/**
* Requires: nothing
* Modifies: nothing
* Effects : repeatedly prompt a user until they enter a valid pin
*
* Sample run: user input is followed by <enter>
*             denoting the user presseing enter
*
*   123<enter>
*   Invalid pin. Use exactly five non-repeating digits.
*   11335<enter>
*   Invalid pin. Use exactly five non-repeating digits.
*   12345<enter>
*   Your pin has been registered. Thank you!
**/
void registerPin() {
const string PIN_PROMPT = "\nPlease enter a pin:";
const string INVALID_PROMPT = "\nInvalid pin. Use exactly five non-repeating digits.";

cout << PIN_PROMPT << endl;
int pin = 0;
cin >> pin;

// TODO: implement event-based loop to get valid pin
// you MUST use the isValidPin() function

cout << endl << "Your pin has been registered. Thank you!" << endl;
return;
}
```
• Modify the given definition to the file `registerPin.cpp`.

• You can test your test cases with your IDE. Run the program and select 3 at the menu.

## How to Submit

• When ready to submit to the autograder, visit https://autograder.io/web/project/967. You will submit your `arrays.cpp` file which should contain your solution for this lab as one program.

IMPORTANT: For all labs in EECS 183, to receive a grade, every student must individually submit the Lab Submission. Late submission for Labs will not be accepted for credit. For this lab, you will receive ten submissions per day with feedback.

• Once you receive a grade of 5 of 5 points from the autograder you will have received full credit for this lab.