Question: Problem 3 Deliverable: problem 3 functions.h and problem 3 functions.cpp Functions - Write a function named NumDigits that takes an integer parameter and returns the
Problem
Deliverable: problemfunctions.h and problemfunctions.cpp
Functions
Write a function named NumDigits that takes an integer parameter and returns the
number of digits in the integer.
Example calls:
NumDigits should return
NumDigits should return
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:
x ;
FindAndReplacex; the function should return true, and after
the function call x should hold
x ;
FindAndReplacex; the function should return true, and after the
function call x should hold
x ; FindAndReplacex; the function should return true,
and after the function call x should hold
x ;
FindAndReplacex; the function should return false because the
second argument has fewer digits than the third argument and
after the function call x should hold
x ;
FindAndReplacex; the function should return false because the
second argument is negative, and after the function call x should hold
Specifications:
Both function prototypes should be included in problemh
Both functions should be implemented in problemcpp
A makefile and some minimal unit tests have been included in problemtests.zip. You are
encouraged to create more rigorous tests. To run the test provided, create a directory containing
only your problemfunctions.h file, your problemfunctions.cpp file, and the files extracted from the attached problemtests.zip. Then type
make testNumDigits
make testFindAndReplace
makefile:
flags stdcWall I
problemfunctions.o : problemfunctions.cpp problemfunctions.h
g $flagsc $
testNumDigits : testNumDigits.cpp problemfunctions.o
g $flags $o $@
$@
testFindAndReplace : testFindAndReplace.cpp problemfunctions.o
g $flags $o $@
$@
clean :
rm problemfunctions.o testNumDigits testFindAndReplace
using std::cout;
using std::endl;
using std::cin;
#include"problemfunctions.h
int main
int x ;
FindAndReplacex;
if x
cout "Passed first one digit replacement test" endl;
else
cout "Failed first one digit replacement test" endl;
x ;
FindAndReplacex;
if x
cout "Passed second one digit replacement test" endl;
else
cout "Failed second one digit replacement test" endl;
x ;
FindAndReplacex;
if x
cout "Passed second one digit replacement test" endl;
else
cout "Failed second one digit replacement test" endl;
x ;
FindAndReplacex;
if x
cout "Passed third two digit replacement test" endl;
else
cout "Failed third two digit replacement test" endl;
x ;
FindAndReplacex;
if x
cout "Passed second two digit replacement test" endl;
else
cout "Failed second two digit replacement test" endl;
x ;
FindAndReplacex;
if x
cout "Passed third two digit replacement test" endl;
else
cout "Failed third two digit replacement test" endl;
return ;
Finclude
using std::cout;
using std::endl;
using std::cin;
#include"problemfunctions.h
int main
if NumDigits
cout "Passed NumDigits test" endl;
else
cout "Failed NumDigits test" endl;
if NumDigits
cout "Passed NumDigits test" endl;
else
cout "Failed NumDigits test" endl;
if NumDigits
cout "Passed NumDigits test" endl;
else
cout "Failed NumDigits test" endl;
return ;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
