Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define A Range Of Numbers in C/C++

Tags:

c++

c

range

Is there a way that I could divide a range of numbers into certain sub ranges

i.e. If we have the range 1-10

The user inputs 1 3 , 4 7 ,7 10 and we define the range 1-3 as part of one range, 4-7 as another and so on. and if inputed the number 8 for example, we get output as range 3.

Was considering creating an array int arr[10] and as user inputs 1 3 for eg then arr[0]=1 and arr[2]=1 , then for 4 7 as arr[3]=2 and arr[6]=2 . But this misses numbers in between and if we looped from 0 to 2,and 3 to 6. On a larger scale with an array longer than 10 this would be inefficient.

Could someone help me?

like image 905
user3501236 Avatar asked Apr 05 '14 15:04

user3501236


People also ask

What is range of data types in C?

Range is the minimum to maximum value supported for that datatype. Integers in C are of 16-bit . Signed int will be -32768 to 32767 i.e. (-2^15) to (2^15 -1) Unsigned int: 0 to 65535 i.e. 0 to (2^16)

How do you define range in data type?

The size or range of the data that can be stored in an integer data type is determined by how many bytes are allocated for storage. Because a bit can hold 2 values, 0 or 1, you can calculate the number of possible values by calculating 2n where n is the number of bits.


2 Answers

Define a range type:

struct range
{
  int low;
  int high;
}

Then define an array of such:

struct range ranges[<some size>] = {0};

Define and implement functions finding the highest high and the lowest lowest low values:

int lowest(struct range * ranges, int * plowest);
int highest(struct range * ranges, int * phighest);

Enter all ranges and stores them in ranges.

Call highest() and lowest() on ranges and you have the outer interval:

struct range range_outer = {0};

/* code reading ranges. */

if (0 != lowest(ranges, &range_outer.lowest))
  fprintf(stderr, "lowest() failed\n");

if (0 != highest(ranges, &range_outer.highest))
  fprintf(stderr, "highest() failed\n");

To help lowest() and highest() it might make sense to sort the ranges, by member low for the former by member high for the latter.

int sort_by_low(struct ranges * ranges); 
int sort_by_high(struct ranges * ranges); 

Sorting arrays can easily be done by using the qsort() function.

like image 81
alk Avatar answered Sep 30 '22 14:09

alk


A full working implementation:

#include <iostream>
#include <vector>

using namespace std;

typedef struct
{
    int min; int max; int range;
} RANGE;

int InRange(int num, RANGE range)
{
    if (num >= range.min && num <= range.max)
        return 1;
    return 0;
}

int main()
{
    vector<RANGE> list; int count = 0, rangesCount = 0;
    puts("How many ranges?");
    cin >> rangesCount;
    puts("Input the range defenitions as a min-max-range pairs(Enter min, then enter      max, then enter the output range):");
for (int i = 0; i < rangesCount; i++)
{
    RANGE range;
    cin >> range.min;
    cin >> range.max;
    cin >> range.range;
    list.push_back(range);
}

while (1)
{
    puts("Now, enter a value, to know in what range it exist:");
    int num;
    cin >> num;
    for (int i = 0; i < rangesCount; i++)
    {
        RANGE r = list.at(i);
        int included = InRange(num, r);
        if (included)
        {
            cout << "The output range is " << r.range << "\n\n";
        }
    }
}
}

Many things can be improved, and you can tailor it to your needs.

like image 35
mrahhal Avatar answered Sep 30 '22 16:09

mrahhal