• C

integer interval variable

I want to create a integer variable v in such a way that v olny take values between two integer constants.
Is there any way way of doing it in
      a) ansi c?
      b) c++?
thanks.
Juan Carlos Ruiz Gomez.
juancAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mlaiosaCommented:
You could make a class and overload some operators (C++):

class Cint
 {
  private:
   int max,min,n;
  public:
   Cint(int a, int b) { max=a, min=b; };
   ~Cint();

   operator int() { return n; };
   int operator ==( int i ) { return n==i; };
   int operator =( int i ) { if (i <= max && i >= min) n=i, return n };
 }

this is a start.  You may need to overload some more operators, if you need more help, just say so.
 }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
juancAuthor Commented:
Using the class you proppossed above, what should be the syntax to get the following effect?:
(the interval limits)
const int min1=mn1,min2=mn2,...,minN=mnN;
const int max1=mx1,max2=mx2,...,maxN=mxN;
                              (mnI,mxI : some integer constants)

(type definition)
range1 = min1..max1;
range2 = min2..max2;
.
rangeN = minN..maxN;

(variable declaration)
range1 a1,b1,c1;
range2 a2,b2,c2;
.
rangeN aN,bN,cN;

 
thanks,
Juan Carlos Ruiz Gomez
0
mlaiosaCommented:
#include <stdio.h>

//the code from above here

void main ()
 {
  Cint i(1,10);   //i will now have a limited range of 1..10
  i=7;
  printf("\ni == ", i);
 }


also, if you want somethign to have a range from 0..2^x-1, you could say:
unsigned int i:x;
0
Identify and Prevent Potential Cyber-threats

Become the white hat who helps safeguard our interconnected world. Transform your career future by earning your MS in Cybersecurity. WGU’s MSCSIA degree program was designed in collaboration with national intelligence organizations and IT industry leaders.

mlaiosaCommented:
#include <stdio.h>

//the code from above here

void main ()
 {
  Cint i(1,10);   //i will now have a limited range of 1..10
  i=7;
  printf("\ni == ", i);
 }


also, if you want somethign to have a range from 0..2^x-1, you could say:
unsigned int i:x;
0
juancAuthor Commented:
when i compile the following code:

#include <stdio.h>
class Cint
{
     private:
           int max,min,n;
     public:
           Cint(int a, int b) { max=a, min=b; };
      ~Cint();

      operator int() { return n; };
      int operator ==( int i ) { return n==i; };
      int operator =( int i ) { if (i <= max && i >= min) n=i, return n };
    }
}
void main(void)
{
      const int mn=2,mx=7;
      Cint iv(mn,mx);
      iv = 4;
      printf("\ni ==",iv);
}      




I got the following message:

rango.c++: In method `int Cint::operator =(int)':
rango.c++:12: parse error before `return'
rango.c++:12: confused by earlier errors, bailing out


0
mlaiosaCommented:
Ok, for the first time, I actually tried this code.  Fixed the problems and made it a little better.

with a Cint, you can use the ==, =, +=, -=, *= and /= operators.  It sould work for everything else, but if you need more help, that's no problem.

#include <stdio.h>

class Cint
 {
  private:
   int max,min,n;
  public:
   Cint(int a, int b) { max=b, min=a; };
   ~Cint() {};


   unsigned fix();

   operator int&() { return n; };
   int operator ==( int i ) { return n==i; };
   int operator =( int i );
   int operator +=( int i );
   int operator -=( int i );
   int operator *=( int i );
   int operator /=( int i );
 };

unsigned Cint::fix()
 {
  if (n > max)
   n=max;
  else if (n < min)
   n=min;
 }

int Cint::operator =( int i )
 {
  if (i <= max && i >= min)
   n=i;
  return n;
 }

int Cint::operator +=( int i )
 {
  n += i;
  fix();
 }

int Cint::operator -=( int i )
 {
  n -= i;
  fix();
 }

int Cint::operator *=( int i )
 {
  n *= i;
  fix();
 }

int Cint::operator /=( int i )
 {
  n /= i;
  fix();
 }

void main(void)
 {
  const int mn=2,mx=7;
  Cint iv(mn,mx);
  iv = 4;
  printf("\ni == %d\n",(int)iv);  //if you are sending this to printf
                                          //you need to typecast it to an int,
                                          //but not in most cases.
 }

0
juancAuthor Commented:
I modified a little main() fuction declaring two Cint variables (iv and iva) and initialized them with 1 and 8. When i ran the program i got iv == 134519032 iva==0. I always got the first (second) value for the first (second) variable I declared.

I was wondering what do I have to do for "typedef'ing" types of Cint variables, (for example tmm a type of Cint variable with values from m to n) and the way of constructing arrays of those type of variables. This could be my following question :)

thanks
J.C. Ruiz.

0
mlaiosaCommented:
remember that if you send them to printf, you need to typecast them as (int), boefore they have values assigned, the possibilites are random.

What compiler do you use?

ok, here is an arrary of Cint, with a range of 1..5

Cint array[10](1,5)
I thnk that's it, either that or:
Cint array(1,5)[10]

Im not sure what you mean by 'typedef-ing'.  
0
juancAuthor Commented:
What i mean with "typedef-ing" in the above context is to use the "typedef" keyword for defining a new type of (Cint) variables. Then when I do that I could declare some (for example, array) variables of this type. By the way neither
      Cint array[10](1,5)
nor
      Cint array(1,5)[10]
works under
      gcc version egcs-2.90.29 980515 (egcs-1.0.3 release)
for declaring arrays (i got this version by typing: g++ -v)
(My pc has Linux 2.0.34 (Slackware 3.5))

0
mlaiosaCommented:
I've talked with some coluges, and that "typedef-ing" doesnt seem to be practicle.  Here is how you can define the array:

Cint * iv = new Cint[10](2,7);  //create an array of Cint's with bounds 2 and 7

I use gcc 2.7.2.1      I use win98 and the DJGPP port of GCC
0
juancAuthor Commented:
i get the messages
 c++ inter.c++ -o i
inter.c++: In function `int main(...)':
inter.c++:70: no matching function for call to `Cint::Cint (int)'
inter.c++:21: candidates are: Cint::Cint(const Cint &)
inter.c++:8:                 Cint::Cint(int, int)

when i compile (c++ inter.c++ -o i) the following code:
     #include <stdio.h>

     class Cint
      {
       private:
        int max,min,n;
       public:
        Cint(int a, int b) { max=b, min=a; };
        ~Cint() {};


        unsigned fix();

        operator int&() { return n; };
        int operator ==( int i ) { return n==i; };
        int operator =( int i );
        int operator +=( int i );
        int operator -=( int i );
        int operator *=( int i );
        int operator /=( int i );
      };

     unsigned Cint::fix()
      {
       if (n > max)
        n=max;
       else if (n < min)
        n=min;
      }

     int Cint::operator =( int i )
      {
       if (i <= max && i >= min)
        n=i;
       return n;
      }

     int Cint::operator +=( int i )
      {
       n += i;
       fix();
      }

     int Cint::operator -=( int i )
      {
       n -= i;
       fix();
      }

     int Cint::operator *=( int i )
      {
       n *= i;
       fix();
      }

     int Cint::operator /=( int i )
      {
       n /= i;
       fix();
      }

     void main(void)
      {
       const int mn=2,mx=7,t=10;
         Cint *iv = new Cint[t](mn,mx);
      }

0
juancAuthor Commented:
i get the messages:

inter.c++: In function `int main(...)':
inter.c++:70: no matching function for call to `Cint::Cint (int)'
inter.c++:21: candidates are: Cint::Cint(const Cint &)
inter.c++:8:                 Cint::Cint(int, int)

when i compile (c++ inter.c++ -o i) the following code:

     #include <stdio.h>

     class Cint
      {
       private:
        int max,min,n;
       public:
        Cint(int a, int b) { max=b, min=a; };
        ~Cint() {};


        unsigned fix();

        operator int&() { return n; };
        int operator ==( int i ) { return n==i; };
        int operator =( int i );
        int operator +=( int i );
        int operator -=( int i );
        int operator *=( int i );
        int operator /=( int i );
      };

     unsigned Cint::fix()
      {
       if (n > max)
        n=max;
       else if (n < min)
        n=min;
      }

     int Cint::operator =( int i )
      {
       if (i <= max && i >= min)
        n=i;
       return n;
      }

     int Cint::operator +=( int i )
      {
       n += i;
       fix();
      }

     int Cint::operator -=( int i )
      {
       n -= i;
       fix();
      }

     int Cint::operator *=( int i )
      {
       n *= i;
       fix();
      }

     int Cint::operator /=( int i )
      {
       n /= i;
       fix();
      }

     void main(void)
      {
       const int mn=2,mx=7,t=10;
         Cint *iv = new Cint[t](mn,mx);
      }

0
mlaiosaCommented:
You indicate that you are not using GCC to compile.   Compile with g++ file.cc (or gpp file.cc depending on you port)
0
juancAuthor Commented:
The results do not change when i compile the code with c++ or g++ or gcc and using any of the extensions .c++, .cpp, .cxx, .C or .cc for the source file

0
mlaiosaCommented:
    class Cint
             {
              private:
               int max,min,n;
              public:
               Cint(int a, int b) { max=b, min=a; };
               ~Cint() {};

                .......

Try changing that to:

     class Cint
             {
              private:
               int max,min,n;
              public:
               Cint(int a, int b) { max=b, min=a; };
               Cint() { max = 32000, min = -32000; };    //this is the new line
               ~Cint() {};


0
streamer102198Commented:
Have you considered using the enum-type ?
Isn't this what you really want, and in a very easy way?

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.