Question
Problem 3 Deliverables: problem3functions.h and problem3functions.cc Functions Test all of your code on a Linux lab computer. All source files submitted must compile and run
Problem 3 Deliverables: problem3functions.h and problem3functions.cc Functions
Test all of your code on a Linux lab computer. All source files submitted must compile and run on a Linux lab computer of the instructors choice. Submissions that do not compile on the Linux workstation will receive no compilation or execution/correctness points.
You should submit only the following individual files to the assignment: problem3functions.h, problem3functions.cc
Write a function named NumDigits that takes an integer parameter and returns the number of digits in the integer.
Example calls: NumDigits(1078) should return 4 NumDigits(-10) should return 2
Write a function named FindAndReplace that replaces digits within an integer variable.
The function will take an integer variable to search, a positive integer to find, and a positive integer to replace as arguments. The function should find all occurrences of the find integer within the integer variable and replace them with replace integer, moving from right to left.
Preconditions: the second and third arguments should be positive integers, and the second argument should contain at least as many digits as the third argument. If the preconditions are not met, the function should return false and leave the first argument unchanged.
Example calls: example 1:
x = 12325;
FindAndReplace(x, 2, 7); - the function should return true, and after the function call x should hold 17375
example 2: x = 11811;
FindAndReplace(x, 11, 6); - the function should return true, and after the function call x should hold 6806
example 3: x = 118111;
FindAndReplace(x, 11, 23); - the function should return true, and after the function call x should hold 238123
example 4: x = 10345;
FindAndReplace(x, 3, 10); - the function should return false because the second argument has fewer digits (1) than
the third argument (2), and after the function call x should hold 10345
example 5: x = 18;
FindAndReplace(x, -1, 1); - the function should return false because the second argument is negative, and after the
function call x should hold 18
Both function prototypes should be included in problem3.h
Both functions should be implemented in problem3.cc Initial Testing:
A makefile and some minimal unit tests have been included in problem3tests.zip. You are encouraged to create more rigorous tests. To run the test provided, create a directory containing only your problem3functions.h file, your problem3functions.cc file, and the files extracted from the attached problem3tests.zip. Then type
make testNumDigits make testFindAndReplace
Points: style: 1 point clean compilation: 1 point NumDigits passes instructors unit tests: 1 point FindAndReplace passes instructors unit tests: 2 points
makefile
# makefile for problem 3
#
# $@ target
# $< first prerequisite
# $^ all prerequisites
flags = -std=c++17 -Wall -I .
problem3functions.o : problem3functions.cc problem3functions.h
g++ $(flags) -c $<
testNumDigits : testNumDigits.cc problem3functions.o
g++ $(flags) $^ -o $@
./$@
testFindAndReplace : testFindAndReplace.cc problem3functions.o
g++ $(flags) $^ -o $@
./$@
clean :
rm problem3functions.o testNumDigits testFindAndReplace
testFindAndReplace.cc
#include
using std::cout;
using std::endl;
using std::cin;
#include"problem3functions.h"
int main() {
int x = 1231231;
FindAndReplace(x, 3, 7);
if ( x == 1271271 )
cout << "Passed first one digit replacement test" << endl;
else
cout << "Failed first one digit replacement test" << endl;
x = 1231231;
FindAndReplace(x, 1, 0);
if ( x == 230230 )
cout << "Passed second one digit replacement test" << endl;
else
cout << "Failed second one digit replacement test" << endl;
x = 1231231;
FindAndReplace(x, 1, 0);
if ( x == 230230 )
cout << "Passed second one digit replacement test" << endl;
else
cout << "Failed second one digit replacement test" << endl;
x = -1785;
FindAndReplace(x, 78, 96);
if ( x == -1965 )
cout << "Passed third two digit replacement test" << endl;
else
cout << "Failed third two digit replacement test" << endl;
x = 11121111;
FindAndReplace(x, 11, 3);
if ( x == 10320303 )
cout << "Passed second two digit replacement test" << endl;
else
cout << "Failed second two digit replacement test" << endl;
x = -401401;
FindAndReplace(x, 41, 7);
if ( x == -401401 )
cout << "Passed third two digit replacement test" << endl;
else
cout << "Failed third two digit replacement test" << endl;
return 0;
}
testNumDigits.cc
#include
using std::cout;
using std::endl;
using std::cin;
#include"problem3functions.h"
int main() {
if ( NumDigits(1200) == 4 )
cout << "Passed NumDigits(1200) test" << endl;
else
cout << "Failed NumDigits(1200) test" << endl;
if ( NumDigits(-31) == 2 )
cout << "Passed NumDigits(-3) test" << endl;
else
cout << "Failed NumDigits(-3) test" << endl;
if ( NumDigits(0) == 1 )
cout << "Passed NumDigits(0) test" << endl;
else
cout << "Failed NumDigits(0) test" << endl;
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started