C++ Beginner with Arrays Advise

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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.
This is a block of a function, that I'm working with at the moment~
Anyone? D: This is really frustrating me >.<
Using cin >> some_int; will read in an entire integer value.

You could just read in characters and after making sure that they are within the '0'-'9' range, compare them.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <algorithm>
#include <string>

int main()
{
    using namespace std;

    string num;

    cout << "Please input a number\n> ";
    cin >> num;

    cout << "First: " << num[0];
    cout << "\nLast: " << num[num.length()-1];

    sort(num.begin(), num.end());

    cout << "\nLowest: " << num[0];
    cout << "\nHighest: " << num[num.length()-1];

    return 0;
}


That's what I did, although it's probably not the best solution.

EDIT: Oh, and of course this only takes one number. It should be easy for you to modify it to take multiple numbers though if you chose to use it.
Last edited on
I'm not allowed to use any other headers beside iostream and string >.<
I haven't learned num.length, is there a way with just
iostream and string?
Yep, you'll just have to make a sorting algorithm yourself. I recommend quicksort: http://en.wikipedia.org/wiki/Quicksort

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?
Can someone spoonfeed me? (:
Do I like need a double loop?

I can't even use % because its a double D:
But why sort, if you just need the minimum and maximum elements?


I suppose. It was really just an example of a direct port of my code, in which sort actually makes it fairly easy.

Just loop through each element to look for the maximum and minimum numbers.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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.
Why not get the smallest int from each, then compare those to find out which one is bigger?
Topic archived. No new replies allowed.