void AnalyzingIntegerArray( void ) {
int iValue;
int i;
double ary[ ARRAY_SIZE ]; // 100, defined on top
double small;
for ( i = 0; i < ARRAY_SIZE; i++ ) {
ary[ i ] = DBL_MAX;
}
cout << "How many integers? ";
cin >> iValue;
for (i = 0; i < iValue; i++ ) {
cout << "Enter integer #" << i + 1 << ":";
cin >> ary[ i ];
}
small = DBL_MAX;
for( i = 0; i < iValue; i++ ) {
if ( small > ary [ i ] ) {
small = ary [ i ];
}
}
cout << "\n The smallest integer : " << small << endl;
return;
}
What i'm trying to do is for the program to ask me to insert multiple numbers, then after inserting the numbers it would look for the smallest digit in those numbers. For example;
I put in
2 digits:
1234
2345
Then it would extract the 1 because its the smallest digit.
Read up on it, and look at the pseudo code. You should be able to create a sorting algorithm yourself with some time. Then, instead of sort, just use your own sorting algorithm.
As for num.length(), that simply is the length of the array num (as strings are arrays of characters).
Strings are great for picking apart pieces as they are an array, and even if numbers or odd characters, you always have the ASCII table for reference.
quicksort is a bad decision for this problem. The data is fixed in the range [0-9] you could sorted it in linear time (ie: count sort).
But why sort, if you just need the minimum and maximum elements?
void AnalyzingIntegerArray( void ) {
int iValue;
int i;
double ary[ ARRAY_SIZE ]; // 100
double small;
for ( i = 0; i < ARRAY_SIZE; i++ ) {
ary[ i ] = DBL_MAX;
}
cout << "How many integers? ";
cin >> iValue;
for (i = 0; i < iValue; i++ ) {
cout << "Enter integer #" << i + 1 << ":";
cin >> ary[ i ];
}
small = DBL_MAX;
for( i = 0; i < iValue; i++ ) {
if ( small > ary [ i ] ) {
small = ary [ i ];
}
}
smallestDigit( small );
return;
}
void smallestDigit (int arg) {
int smallest;
int smallest1;
if (arg < 0) {
arg = arg * -1;
}
smallest = arg % 10;
while (arg > 0) {
arg = arg / 10;
if (arg != 0 ) {
smallest1 = arg % 10;
}
if (smallest > smallest1 ){
smallest = smallest1;
}
}
cout << "\nThe Smallest digit: " << smallest<< endl;
return;
}
Alright, in the end I come up with this.
This comes out by asking us how many integers would we like to use
I put 2
Then I put the two numbers
9123
456
Then it looks for the smallest digit which is 456.
Then it extracts the smallest number 4.
I'm trying to make it extract the smallest digit between those two numbers.
Which would be 1. Since one is the smallest digit. but it seems not to be working right.