Solved

Algorithm

Posted on 1998-06-08
3
354 Views
Last Modified: 2010-04-01
What is the best algorithm to check a number is in which particular range of:
0 to 9
10 to 99
100 to 999
1000 to 9999
10000 to 99999
...rate at exponential of 10

E.g X = 555 is in the range of 100 - 999

I use a if-else-then to do the checking but its looks terribly ugly.

Any advice?
0
Comment
Question by:engllf
3 Comments
 
LVL 23

Accepted Solution

by:
chensu earned 50 total points
ID: 1165459
Which is the best depends on the actual requirements. Here is an alternative. But you still need to check the value of n. You may use n as an index of an array.


#include <math.h>

.

if (X >= 0)
{
    unsigned int n = X == 0 ? 0 : log10((double)X);
    // 0 to 9, n = 0
    // 10 to 99, n = 1
    // 100 to 999, n = 2
    // 1000 to 9999, n = 3
    // ...
}
else
{
   ...
}

0
 
LVL 4

Expert Comment

by:sganta
ID: 1165460


      I have simple and beautiful algorithm and solution for your problem.

      ALGORITHM
      1. Read the number into "n";
      2. Store this number n as a string into test
          i.e., test = string(n);
      3. Get the length of the string into length
          length = string_length(n);
      4. Now
          Range_From = power(10,length-1)
          Range_To     = power(10,length)-1
      5. End of the algorithm

      /* Here the solution in C++ */

      #include <iostream.h>
      #include <string.h>
      #include <math.h>

      void main(void)
      {
         int n,len,ifrom,ito;
         char *test;

         cout << "Enter a number ";
         cin >>n;

         test = itoa(n); /* Converts integer to Ascii */
         len = strlen(test);
         ifrom = pow(10,len-1);
         ito = pow(10,len)-1;
         cout << "The number "<<n<<" is in the range from "<<ifrom<<" to "<<ito<<endl;
      }

      This  algorithm is tested and it works fine. If you are interested with my answer
      Please reject the above answer and accept my answer.
      Thank you.
      JESUS LOVES YOU - sganta





0
 
LVL 22

Expert Comment

by:nietod
ID: 1165461
engllf, just so you know, while sganta's solution is pretty short and very slick, it is not very efficient.  If you are concerned about speed, you'll want to use chensu's solution.  But if you aren't concerned about speed, sganta's solution is pretty clear and simple.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now