Constants in Arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Deanm007
    New Member
    • Apr 2010
    • 37

    Constants in Arrays

    Hi, please see questions below. And thanks in advance.

    Code:
    #include<iostream>
    
    using namespace std;
    
    enum ANIMALS { MAMMAL, DOG, CAT}; 
    const int NumAnimalTypes = 3; //Whats the purpose of this?
    
    int main()
    {
        int theArray[NumAnimalTypes]; //Why not put 3 here instead of NumAnimalTypes? Does it have something to do with constant?
        int choice, i;
        
        for(i=0; i<NumAnimalTypes; i++)
        {
                 cout << "(0)Mammal (1)dog (2)cat: ";
                 cin >> choice;
                 switch(choice)
                 {
                               case DOG: cout << "I am a dog" << endl;
                               break;
                               case CAT: cout << "I am a cat" << endl;
                               break;
                               case MAMMAL: cout << "I am a Mammal" << endl;
                               break;
                               default: cout << "I don't know what I am" << endl;
                               break;
                 }
                 theArray[i] = choice; //assign the choices the user entered to the array's counter i.
        }
        //list the choices the user entered
        for (i=0; i<NumAnimalTypes; i++)
        cout << theArray[i] << endl;        
                               
        
        system("pause");
        return 0;
    }
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    The purpose of NumAnimalTypes is to hold the number of kinds of animals supported by your program. The enumeration lists all of the kinds of animals supported. The definition of NumAnimalTypes immediately follows the enumeration to increase the likelihood that any changes to add or remove kinds of animals will be done consistently to both lines.

    NumAnimalTypes is defined as a const int to prevent your program from accidentally changing its value.

    This program is a bit of a maintenance nightmare. Consider what it takes to add a new kind of animal:
    1. Change line 5 to add the new animal to the enumeration.
    2. Change line 6 from "3" to "4".
    3. Change line 15 to add the new choice to the annunciator string.
    4. Insert a new case and break between lines 24 and 25.


    Can you think of any ways to reduce the footprint of a change of this sort?

    By the way, why does the size of theArray match the number of kinds of animals?

    By the way, your kinds of animals are not mutually exclusive: both dogs and cats are also mammals.

    Comment

    • Deanm007
      New Member
      • Apr 2010
      • 37

      #3
      Thanks a lot. This is a watered down version of a more complicated code. Basically, I needed to know why the constant and you answered my question mostly. However, one thing I still need to grasp: So if i put in the number 3 into theArray, the program could accidently change that value?
      Last edited by Deanm007; Aug 11 '10, 08:12 PM. Reason: still a little doubt

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        No, the program can't alter the literal constant "3"; but if you omit const from the definition of NumAnimalTypes then a typo in your program could change the value of that variable.

        Comment

        • Deanm007
          New Member
          • Apr 2010
          • 37

          #5
          Thank You donbock, that was very clear.

          Comment

          Working...