### Finding the largest and smallest digits in an array of integers

Here's how the output should look:
Select an option (1, 2, or 3): 2
How many integers? 3

Enter integer #1: 12795
Enter integer #2: -20784
Enter integer #3: -27904
The smallest digit: 0 Digit 0 can be found in integer number(s): 2, 3

The largest digit: 9 Digit 9 can be found in integer number(s): 1, 3
It's giving me 3 separate largest and smallest digits (one for each member of the array) how would I go about making the program compare the numbers amongst themselves to decide which over all is the largest or smallest and only reply with one set for the whole array? What am I missing so the digits will be compared properly ? Thank you

//smallest and largest digits

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546`` ``````#include using namespace std; int main (){ int numberofint; int* seriesArray; int value; cout << " How many integers? "; cin >> numberofint; seriesArray = new int [numberofint]; for (int i = 0; i < numberofint; i++) { cout << "\n Enter integer #" << i +1 << ": "; cin >> *(seriesArray +i); } for (int i = 0; i < numberofint; i++){ value = seriesArray[i]; if (value < 0) value = -value; int ldigit = -1 ; int sdigit = 10 ; int curdigit= 0; int powten = 10; ldigit = sdigit = value % powten; while (value){ if (value / powten == 0) break; curdigit = (value / powten) % 10; if (curdigit < sdigit); sdigit = curdigit; if (curdigit > ldigit) ldigit = curdigit; powten *= 10; } cout << "\nThe smallest digit is: " << sdigit << endl; cout << "The greatest digit is: " << ldigit << endl; } delete [] seriesArray; return 0; } ``````
that code looks complicated

the input is an integer but that doesn't mean that you must put the input into an integer data type

 ``12345678910`` `````` char sdigit = '~'; string input; cin >> input; for( int i = 0; i < input.size(); ++ i ) { if( sdigit != '-' && sdigit > input[i] ) sdigit = input[i]; } ``````

anyway, I am a bit confused...
what is the output ?
Last edited on
this is the output :
How many integers?
Enter integer #1: 231
Enter integer #2: 456
Enter integer #3: 987
The smallest digit is: 2
The greatest digit is: 3

The smallest digit is: 4
The greatest digit is: 6

The smallest digit is: 9
The greatest digit is: 9

Yours will not work rmxhaha

He is trying to:
1) Ask user for amount of integers
2) input n amount of integers
3) find the largest digit in all of the integers
4) output the largest digit and all the integers it can be found in
5) repeat 3 and 4 for smallest

ex

3 integers

1 , 23 , 321

largest digit = 3
largest digit is found in integer 2 and 3
smallest digit 1
smallest digit is found in integer 1 and 3

He would have to do something basically to break an integer down into digits like this:

 ``123456789101112131415161718`` ``````#include int main() { int input = 0; std::cout << "Please enter an integer: "; std::cin >> input; std::cout << "The digits for " << input << " are: "; while( input > 9 ) { std::cout << input % 10; input /= 10; } std::cout << input << std::endl; }``````

Then assign those digits to an array of some sort or a container then check the largest/smallest digits manually or using the std::min_element and std::max_element.
is assigning the digits to an array the only way to do it?
technically no but how else will you know what digits are in which one.

I would create an array that stores the largest and smallest digit in each number.

Then compare those digits to the largest/smallest overall so you know which numbers have the largest/smallest digit.

edit*
ex:

 ``12345678910111213141516171819202122232425262728293031`` ``````#include int main() { const int SIZE = 2; int largest = 0 , smallest = 9; //easy to check with those intial values int number[SIZE] = { 123 , 641 }; int *smallest_digits = new int[SIZE] , *largest_digits = new int[SIZE]; int temp = 0; for( int i = 0; i < SIZE; ++i ) { largest_digits[i] = 0; smallest_digits[i] = 9; while( number[i] > 0 ) { temp = number[i] % 10; if( temp > largest_digits[i] ) largest_digits[i] = temp; if( temp < smallest_digits[i] ) smallest_digits[i] = temp; if( temp > largest ) largest= temp; if( temp < smallest ) smallest = temp; number[i] /= 10; } } }``````

You can now compare like this

 ``123456`` `````` for( int i = 0; i < SIZE; ++i ) { if( largest_digits[i] == largest ) cout << i + 1 << ' '; }``````

Same for the smallest
Last edited on
Topic archived. No new replies allowed.