A program i am writing uses a class that represents a fraction. I have a class member function that takes two integers (numerator and denominator) from the class member variables and compares them to find out if there are any uncommon digits.
(numerator 123 and denominator 110035 would have 3 uncommon digits being 2 and 0 and 5 because those digits are unique to their int).
My member function must output an array that contains the data of how many uncommon digits there are, which digits specifically, and how many times that digit appears in its respective int (numerator or denominator).
I have an array within the member function called uncommonDigitArray that is formatted like this:
Index 0 contains an int representing how many uncommon digits there are
Indices 1 - 10 represent the digits 0 - 9 and they contain ints representing how many times that digit appears in the numerator.
Indices 11 - 20 represent the digits 0 - 9 and they contain ints representing how many times that digit appears in the denominator.
I need to pass that array to another function and then iterate through it to output the information correctly.
This is the member function that creates the array:
int Fraction::extractUncommonDigit() {
int numDigitCounterArray[10] = { 0 };
int denomDigitCounterArray[10] = { 0 };
int digitCounterArray[20] = { 0 };
int uncommonDigitArray[21] = { 0 };
int tempDigit;
int numInt{ num };
int denomInt{ denom };
int numLength{ 0 };
int denomLength{ 0 };
int uncommonDigitCounter = 0;
do {
tempDigit = numInt % 10;
digitCounterArray[tempDigit] += 1;
numInt /= 10;
numLength++;
} while (numInt != 0);
do {
tempDigit = denomInt % 10;
digitCounterArray[tempDigit + 10] += 1;
denomInt /= 10;
denomLength++;
} while (denomInt != 0);
for (int i = 0; i < 10; i++) {
if ((digitCounterArray[i] == 0 && digitCounterArray[i + 10] > 0) ||
(digitCounterArray[i] > 0 && digitCounterArray[i + 10] == 0)) {
uncommonDigitCounter++;
}
}
uncommonDigitArray[0] = uncommonDigitCounter;
for (int i = 0; i < 10; i++) {
if (digitCounterArray[i] > 0 && digitCounterArray[i + 10] == 0) {
uncommonDigitArray[i+1] = digitCounterArray[i];
}
if (digitCounterArray[i] == 0 && digitCounterArray[i + 10] > 0) {
uncommonDigitArray[i + 11] = digitCounterArray[i + 10];
}
}
cout << "\nuncommonDigitsArray" << endl;
for (int i = 0; i < 21; i++) {
cout << uncommonDigitArray[i];
}
cout << "\n\ndigitCounterArray\n";
for (int i = 0; i < 20; i++) {
cout << digitCounterArray[i];;
}
return (*uncommonDigitArray);
}
This is where I want to take that array and iterate through it. Case 3 is where I call the member function to extract the uncommon digits:
void runMenu3(void) {
int option;
int n;
int d;
Fraction* frPtr{ nullptr };
int *uncommonDigitArray = new int[21];
do {
cout <<
"*****************************************\n"
"* Menu - HW #1 *\n"
"* 1. Creating/Updating Fraction *\n"
"* 2. Displaying the Fraction *\n"
"* 3. Displaying Uncommon Digit(s) *\n"
"* 4. Quit *\n"
"*****************************************\n"
"Enter your option (1 through 4): ";
cin >> option;
switch (option) {
case 1:
if (frPtr == nullptr) {
cout << "\nEnter an int for num: ";
cin >> n;
cout << "\nEnter an int for denom: ";
cin >> d;
frPtr = new FractionGeorgeMS(n, d);
}
else if (frPtr != nullptr) {
runHWMenu4(frPtr);
}
break;
case 2:
frPtr->print();
break;
case 3:
frPtr->print();
*uncommonDigitArray = frPtr->extractUncommonDigit();
cout << "\n\nuncommonDigitArray after copying" << endl;
for (int i = 0; i < 21; i++) {
cout << uncommonDigitArray[i] << endl;
}
cout << "Using the results from extractUncommonDigit() -\n"
<< " The uncommon digit array has " <<
uncommonDigitArray[0] << " uncommon digit(s) of\n"
<< " from num --\n";
for (int i = 0; i < 10; i++) {
if (uncommonDigitArray[i] > 0) {
cout << i << " (occured " << uncommonDigitArray[i + 1]
<< " time(s))" << endl;
}
}
for (int i = 10; i < 20; i++) {
if (uncommonDigitArray[i] > 0) {
cout << i << " (occured " << uncommonDigitArray[i + 11]
<< " time(s))" << endl;
}
}
break;
}
} while (option != 4);
}
Here is my output:
*****************************************
* Menu - HW #1 *
* 1. Creating/Updating Fraction *
* 2. Displaying the Fraction *
* 3. Displaying Uncommon Digit(s) *
* 4. Quit *
*****************************************
Enter your option (1 through 4): 1
Enter an int for num: 123
Enter an int for denom: 110035
123 / 110035
A call to Fraction(int, int) was made to build a Fraction!
num : 123
denom : 110035
*****************************************
* Menu - HW #1 *
* 1. Creating/Updating Fraction *
* 2. Displaying the Fraction *
* 3. Displaying Uncommon Digit(s) *
* 4. Quit *
*****************************************
Enter your option (1 through 4): 3
The current Fraction object has
num : 123
denom : 110035
uncommonDigitsArray
300100000002000010000
digitCounterArray
01110000002201010000
uncommonDigitArray after copying
3
-842150451
-842150451
-842150451
-842150451
-842150451
-842150451
-842150451
-842150451
-842150451
-842150451
-842150451
-842150451
-842150451
-842150451
-842150451
-842150451
-842150451
-842150451
-842150451
-842150451
Using the results from extractUncommonDigit() -
The uncommon digit array has 3 uncommon digit(s) of
from num --
0 (occured -842150451 time(s))
*****************************************
* Menu - HW #1 *
* 1. Creating/Updating Fraction *
* 2. Displaying the Fraction *
* 3. Displaying Uncommon Digit(s) *
* 4. Quit *
*****************************************
Enter your option (1 through 4):
As you can see when I iterate through the array within the member function, I get good results. When I iterate through the array after I return a copy of it,Ii get nothing.
Any advice on what I am doing wrong would be greatly appreciated.