Thread: Sorting out cause of wrong response from class members

  1. #1
    Registered User
    Join Date
    Apr 2006
    Location
    Taxachusetts
    Posts
    10

    Sorting out cause of wrong response from class members

    I've fallen and I can't get up. I don't want my homework done for me, but this particular piece of my solution to a homework problem is killing me. I need some other eyes to help me see the light on what I'm missing here.

    A class named myClass has private data consisting of several sorted int arrays, IE
    Code:
    int arrayOne[10]={1,3,5,7,9,11,13,15,17,19};
    int arrayTwo[10]={2,4,6,8,10,12,14,16,18,20};
    That class has a public member that will generate a pseudo-'random' number when it is called, and then check to see if that number is in the 'odd' array, like this :
    Code:
    bool myClass::tryOdd()
    { 
         int random = rand()%20;
         bool response;
         int arrayLength = 10; // number of elements in the odd array
         if ( arraySearch( odd, arrayLength, random ) )
         {
            response=true;
         }
         else
         {
            response=false;
         }
         return(response);        
    }
    The myClass::arraySearch() member is implementedlike this:
    Code:
    bool myClass:arraySearch( int name[], int length, int match )
    {
        bool response;
        int start = 0;
        int index;
        int* result = find( name+start, name+length, match );
        if ( result == name+length )
        {
            response = false;
        }
        else
        {
            response = true;
        }
        return(response);            
    }
    My problem here with this design is that no matter what I do, I keep getting incorrect response. I even removed the rand() call and set random = 5, which is obviously in arrayOne as an odd number, but the client still receives a bool false return. What's more annoying is that when I take the arraySearch algorithm and just create a little stub program with it, and a single array so I'm isolating it, it works fine:
    Code:
    #include<iostream>
    #include<algorithm>
    
    using namespace std;
    
    const int name[]={1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35};
    
    int main()
    {
        
        
        bool response;
        int match = 5;
        int start = 0;
        int length = 18;
        int index;
        int* result = find( name+start, name+length, match );
        if ( result == name+length )
        {
            cout << "False" << endl;
        }
        else
        {
            cout << "Match " << *result << endl;
        }  
       
        return(0);   
    }
    Is this a scope problem, or? What am I missing here that I can't even FORCE an odd number into 'random' and get a 'true' response back at the client?

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
    bool myClass:arraySearch( int name[], int length, int match )
    There's a scope problem Are you giving us real code? 'Cause you have stuff like

    Code:
    int arrayOne[10]={1,3,5,7,9,11,13,15,17,19};
    And then

    Code:
    arraySearch( odd, arrayLength, random )
    Because I can't see anything really wrong with your code. Only that this:

    Code:
         if ( arraySearch( odd, arrayLength, random ) )
         {
            response=true;
         }
         else
         {
            response=false;
         }
         return(response);
    Could be shortened to this:

    Code:
        return arraySearch( odd, arrayLength, random );

  3. #3
    Registered User
    Join Date
    Apr 2006
    Location
    Taxachusetts
    Posts
    10
    Huh, so there is a scope issue...Thanks!

    This is real code. The example there shows me attempting to pass the name of one of the arrays, named 'odd', in the private data. I apologize, I should have used that name as an example array.

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Well, not really, I mean, this:

    Code:
    bool myClass:arraySearch
    Shouldn't compile, should be the (::) scope resolution operator

    Code:
    bool myClass::arraySearch

  5. #5
    Registered User
    Join Date
    Apr 2006
    Location
    Taxachusetts
    Posts
    10
    Quote Originally Posted by Tonto
    Well, not really, I mean, this:

    Code:
    bool myClass:arraySearch
    Shouldn't compile, should be the (: scope resolution operator

    Code:
    bool myClass::arraySearch
    You know what, that was my bad when I transposed it into the cprogramming site post window. I didn't even catch that when I posted until you caught it. In fact, I had to read your clarification to catch it the second time.

    I switched over to using MS VC++ just to use the step through debugger, and discovered that when a new instance of the class is created, the default constructor isn't initializing the arrays properly; the arrays show the correct number of elements, but the elements are all set to 0. Frustratingly, I'm not able to post much more without feeling like I'm violating my professor's agreement that we only post small amount of code for which we had direct question about. So, rather than post more, I'll just ask a question, based on the following esoterica that I can't find in my textbook:

    If I have a class that contains only several arrays as private data, and all member methods of the class are only observers, is it OK to have only a single, no-arguments default constructer that initializes the arrays? My textbook always models both the no argument default constructor as well as the constructor that allows manipulating/setting the private data...But, in my class the values of the private data is const and all methods are only observers, so I omitted the arguments constructor...Could this be why everything seems to run "fine" except for the fact that the arrays are not actually being initialized with the correct values? Is there some requirement for both constructors at runtime that, since I only have one, is leading to the problem I'm having?

  6. #6
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Are the arrays actual const data or you just don't let anything modify the data?
    Woop?

  7. #7
    Registered User
    Join Date
    Apr 2006
    Location
    Taxachusetts
    Posts
    10
    Quote Originally Posted by prog-bman
    Are the arrays actual const data or you just don't let anything modify the data?
    I've tried both declaring them as const, and not declaring them as const. But I won't allow anything to modify the data, either. That is why I omitted the parameterized constructor, thinking that it was of no value other than to tamper with the private data when I don't want this allowed.

    I'm sorry to be so cryptic but I don't think I can post more code then I already sent my professor and he OK'd me posting.

  8. #8
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Well. Let's say you have a class like this
    Code:
    #include <iostream>
    #include <cstdlib>
    
    class arrayClass
    {
        public:
            arrayClass()
            {
                //This will set array with 10 "random" numbers
                for(int i = 0; i < 10; i++)
                {
                    array[i] = rand() % 10;
                }
            }
            const int getArrayValue(const int value)
            {
                //return the specified value(unchecked) 
                return array[value];
            }
        private:
            int array[10];
    };
    Your array is in fact un modifiable because well there is nothing to modify it.

    Is this your question or am I completly off base?
    Woop?

  9. #9
    Registered User
    Join Date
    Apr 2006
    Location
    Taxachusetts
    Posts
    10
    Quote Originally Posted by prog-bman
    Well. Let's say you have a class like this
    Code:
    #include <iostream>
    #include <cstdlib>
    
    class arrayClass
    {
        public:
            arrayClass()
            {
                //This will set array with 10 "random" numbers
                for(int i = 0; i < 10; i++)
                {
                    array[i] = rand() % 10;
                }
            }
            const int getArrayValue(const int value)
            {
                //return the specified value(unchecked) 
                return array[value];
            }
        private:
            int array[10];
    };
    Your array is in fact un modifiable because well there is nothing to modify it.

    Is this your question or am I completly off base?
    No, you are exactly on target. AND, I'm looking at your constructor and think you may have just showed me the error of my way here (I'll have kindly ask you to verify)...

    My default constructor is doing an aggregate assignment of the array values, but maybe that's not legal(?). I wonderd about this before, but because I read it's OK to declare the array and initialiaze it this way, I did the same thing in the constructor rather than initialize one array element at a time. If I use your method as my example, I am actually doing this with the constructor:

    Code:
            arrayClass()
            {
                  array[10] = {1,2,3,4,5,6,7,8,9,10};
             }
    Could my whole problem be that the aggregate attempt on the array isn't allowed at runtime?!?

  10. #10
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    No, that isn't legal. You can only do that on the initial statment which won't work in a class, so you have to do it "my" way.
    Woop?

  11. #11
    Registered User
    Join Date
    Apr 2006
    Location
    Taxachusetts
    Posts
    10
    Quote Originally Posted by prog-bman
    No, that isn't legal. You can only do that on the initial statment which won't work in a class, so you have to do it "my" way.
    I love you man.

    Your class method example and your two short sentence response just unravelled a LOT of recent late nights 'trying' things on this program.

  12. #12
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    It happened and happens to the best of us. Good luck and keep going.
    Woop?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 07-26-2008, 08:44 AM
  2. Read-only class members
    By kidburla in forum C++ Programming
    Replies: 4
    Last Post: 10-14-2006, 12:52 PM
  3. Need help to build network class
    By weeb0 in forum C++ Programming
    Replies: 0
    Last Post: 02-01-2006, 11:33 AM
  4. are static class members the right choice?
    By drrngrvy in forum C++ Programming
    Replies: 6
    Last Post: 10-12-2005, 09:41 AM
  5. Replies: 8
    Last Post: 10-02-2005, 12:27 AM