Can yoou return a range of values stored into a integer?

This is a discussion on Can yoou return a range of values stored into a integer? within the C++ Programming forums, part of the General Programming Boards category; Can someone give me an example of this too? Sorry, but i din't know if to open a new thread ...

  1. #1
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    Can you return a range of values stored into an array of integer?

    Can someone give me an example of this too? Sorry, but i din't know if to open a new thread or not since its a different kind of question?
    sorry for the confusion, i edited it.
    Last edited by correlcj; 10-27-2002 at 02:19 PM.
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  2. #2
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    What do you mean by:
    range of values stored into a integer
    I don't get it?

  3. #3
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    i don't either but this would return a range :

    Code:
    #include <iostream.h>
    
    int main()
    {
    for (int x = 1; x <=100; x++)
    {
    cout<<x<<endl;
    }
    
    return 0;
    }

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You can only store 1 value in an integer, though you could use bitshifting and masking to store more:
    Code:
    int SetValues(short First, short Last)
    {
       int Var;
       Var = First;
       Var |= (Last << 16);
       return Var;
    }
    
    short GetFirst(int Var)
    {
       return Var & 0x00FF;
    }
    
    short GetLast(int Var)
    {
        return Var & 0xFF00;
    }
    If you really want to return a range, you could use a structure:
    Code:
    typedef struct
    {
       int First;
       int Last;
    }RANGE;
    
    RANGE GetRangeFunction()
    {
       RANGE Range;
       Range.First = 12;
       Range.Last = 36;
       return Range;
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    > Can you return a range of values stored into an array of integer?
    An array of integers is a range of values. Just return a pointer to the array:
    Code:
    #include <iostream>
    
    int *f()
    {
      int *range = new int[5];
    
      for ( int i = 0; i < 5; ++i )
        range[i] = i;
    
      return range;
    }
    
    int main()
    {
      int *r = f();
    
      for ( int i = 0; i < 5; ++i )
        std::cout<< r[i] <<std::endl;
    
      delete[] r;
    
      std::cin.get();
    }
    -Prelude
    My best code is written with the delete key.

  6. #6
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    Wink Thanks everybody for the help!!!

    Thanks prelude, that really helped me understand.
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New string functions
    By Elysia in forum C Programming
    Replies: 11
    Last Post: 03-28-2009, 05:03 AM
  2. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-20-2009, 11:37 PM
  3. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  4. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 10:17 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21