Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, hope all is well. C++ Can you help me get rid of all the if/else if statements. I can only use these if their

Hello, hope all is well. C++

Can you help me get rid of all the if/else if statements. I can only use these if their within the framework of if/else if/else. Nevertheless I wish you could help me rid of all these flaws..

//#include "stdafx.h"

#include

using namespace std;

bool extractEvenDigitCount(int val, int &minEvenVal, int &count)

{

int currDigit;

bool newSmallEvenIntFound = false; /*If new small even integer is found, then it will be true */

do

{

currDigit = val % 10; /*Extract the digit from the number */

val /= 10;

if (currDigit < 0) currDigit = -currDigit; /* Invert the sign if found -ve digit */

if (currDigit % 2) continue; /* If not even digit continue with next digit */

/* If current digit is even and its value is less than current smallEvenValue, then overwrite the smallEvenValue with the current digit. Or for the first number in array smallEvenValue will be -1, so overwrite the current digit as the first small even digit. */

if ((currDigit % 2 == 0) && ((currDigit < minEvenVal) || (minEvenVal == -1)))

{

minEvenVal = currDigit;

count = 1; /* Start count from 1 */

newSmallEvenIntFound = true; /*Make new small even digit found flag true, so that the counters for the previous small digit can be made zero by calling resetCounter */

}

else if (currDigit == minEvenVal)

count++; /*If current digit matches with the small even digit then increment the counter */

} while (val); /*Continue until val becomes zero */

return newSmallEvenIntFound;

}

void resetCounters(int count[], int range)

{/* Reset the count for the previous counters, when another small even integer is found */

for (int i = 0; i < range; i++)

count[i] = 0;

}

void extractSmallestEvenDigitInfoYourName(int arr[], int size)

{

int *count = new int[size];

resetCounters(count, size);

int minEvenVal = -1;

for (int i = 0; i < size; i++)

{

/*If a new small digit is found, then reset previous small digit counters to zero */

if (true == extractEvenDigitCount(arr[i], minEvenVal, count[i]))

resetCounters(count, i);

}

int totalCount = 0;

for (int i = 0; i

if (totalCount == 0)

cout << "There are no even digits." << endl;

else

{

cout << "The smallest even digit is " << minEvenVal << " and is seen " << totalCount << " time(s) in the following values:" << endl;

for (int i = 0; i

{

if (count[i] != 0) /* print numbers where the digit is present */

cout << arr[i] << endl;

}

}

}

int main()

{

int choice, size, *arr;

while (1)

{

cout << "MENU" << endl;

cout << "1. Calling extractSmallestEvenDigitInfoYourName()" << endl;

cout << "2. Quit" << endl;

cout << "Enter an integer for option + ENTER: ";

cin >> choice;

if (choice == 2)

//exit(0);

return 1;

if (choice != 1)

{

cout << "Wrong option!" << endl << endl;

continue;

}

cout << "How many integers? ";

cin >> size;

arr = new int[size];

for (int i = 0; i< size; i++)

{

cout << "Enter integer #" << i + 1 << ":";

cin >> arr[i];

}

cout << "The original array :" << endl;

for (int i = 0; i< size; i++)

{

cout << arr[i] << endl;

}

cout << endl << "Calling extractDigitExistenceInfoYourName()" << endl;

cout << endl << "After the function completed and returned the info -" << endl << endl;

extractSmallestEvenDigitInfoYourName(arr, size);

delete[] arr;

}

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Visual C# And Databases

Authors: Philip Conrod, Lou Tylee

16th Edition

1951077083, 978-1951077082

More Books

Students also viewed these Databases questions

Question

Define language, and recognize its properties.

Answered: 1 week ago