finding smallest number among random amount of digits

so I have a tricky task here again, I have to find smallest number within input, that can be like whatever amount of digits, its easier, if you can determine amount of numbers, but i have to be able to find smallest integer amongst any input number. for example 7592748758 -->2

1
2
3
4
5
6
7
8
#include <iostream>
using namespace std;
int main()

 {
          int smallest, a;
          cin>>a; //can be number with any amount of digits
  Put the code you need help with here.
you are going to implement 2 variables num1andnum2 for example. then you will be creating an if statement that checks num 1 against num 2. which ever one is smaller, can be moved to your variable smallest. the next thing you will do is create a loop that will prompt for user input, store it in one of your previous variables num2 for example, and compare num2 and smallest to determine which is smaller. I hope this helps!
Last edited on
closed account (Evq21hU5)
If I understand you correctly, there can be any amount of unlimited-length numbers...

So, firstly, you should be able to read these numbers (the best solution for me is putting numbers into strings)
1
2
3
4
std::string a;
while (std::cin >> a) {
    // ...
}


And next, and define function to compare them:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*  -1: a > b
     0: a == b
     1: a < b */
int compare_str(std::string a, std::string b)
{
    // you can check also sign here
    if (a == b) return 0;
    if (a.size() < b.size())
        return 1;
    else if (a.size() == b.size()) {
        /* checking from the most significant digit  to the least significant */
        for (int i = 0; i < a.size(); ++i) {
            if (a[i] < b[i]) return 1;
            else if (a[i] > b[i]) return -1;
        }
    } else {
        return -1;
    }
}


At the end, good idea is setting 'smallest' variable to the first read number (string-number).
If your not using input from a user, and are using a random number genetator, you can just change
the next thing you will do is create a loop that will prompt for user input, store it in one of your previous variables num2 for example, and compare num2 and smallest to determine which is smaller. I hope this helps!
to
the next thing you will do is create a loop that will accept a random number from a random number generator, store it in one of your previous variables num2 for example, and compare num2 and smallest to determine which is smaller. I hope this helps!
Last edited on
Ok i got this so far, that I can break number into digits but now how can i compare digits to each other to find smallest? or how can I move them to array now? then would be easier to find min number.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;
int main()

 {
          int  a, digit, min=0, max=9;
          cout<<"ievadi skaitli"<<endl;
          cin>>a; 
          
        do{ 
            digit=a%10; a=a/10; 
          
        }while (a!=0);
        
          
          system ("pause");
          return 0;
          }
Last edited on
Ok here s is the program, figured it out myself, just one last thing, how do I make output print the result just once? now it does as many times asdo while loop works.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;
int main()

 {
          int  a, digit, min=0, max=9;
          cout<<"ievadi skaitli"<<endl;
          cin>>a; 
          
        do{ 
            digit=a%10; a=a/10;   
        }while (a!=0);
        
        for (int i=min; i<=max; i++)
        {  
            if (digit<i?i:digit) cout<<"-->"<<digit<<endl; 
}
          system ("pause");
          return 0;
          }
Last edited on
Topic archived. No new replies allowed.