EECS 183 Labs

EECS 183 Lab 5: Exam 1 Practice

Lab due on your scheduled lab day (Feb 13-19)

Lab accepted for full credit until Tuesday, February 20, 2024, 11:59 pm Eastern

In this lab, you are completing a practice exam with your peers in preparation for Exam 1. For all labs in EECS 183, to receive a grade, every student must individually submit the lab assignment.

Starter Files

You can download the starter files using this link.

After downloading and unzipping, you will find the following files in the starter-files folder:

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

Lab Assignment

Tasks to Complete

  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 it to the autograder to check your solution.
    • 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.

FAILED: calculateFrequency(123, 2)
Expected return value: 1
Actual return value: 0
-------------------------------
EECS 183 Lab 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 the pin has less than five digits;
*           (2) returns false if the pin has more than five digits;
*           (3) returns false if the pin has a repeating digit;
*           (4) returns true if the 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 5 - Exam 1 Review
 *
 * Testing functions for your isValidPin.cpp implementation.
 * Holds the definition of the 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;
}

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.

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

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

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 5 - Exam 1 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 pressing enter
* 
*   Please enter a pin:
*   123<enter>
*   Invalid pin. Use exactly five non-repeating digits.
*   Please enter a pin:
*   11335<enter>
*   Invalid pin. Use exactly five non-repeating digits.
*   Please enter a pin:
*   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 an event-based loop to get a valid pin
	// you MUST use the isValidPin() function

	cout << endl << "Your pin has been registered. Thank you!" << endl;
	return;
}

How to Submit

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