473,407 Members | 2,315 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Constants in Arrays

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

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. enum ANIMALS { MAMMAL, DOG, CAT}; 
  6. const int NumAnimalTypes = 3; //Whats the purpose of this?
  7.  
  8. int main()
  9. {
  10.     int theArray[NumAnimalTypes]; //Why not put 3 here instead of NumAnimalTypes? Does it have something to do with constant?
  11.     int choice, i;
  12.  
  13.     for(i=0; i<NumAnimalTypes; i++)
  14.     {
  15.              cout << "(0)Mammal (1)dog (2)cat: ";
  16.              cin >> choice;
  17.              switch(choice)
  18.              {
  19.                            case DOG: cout << "I am a dog" << endl;
  20.                            break;
  21.                            case CAT: cout << "I am a cat" << endl;
  22.                            break;
  23.                            case MAMMAL: cout << "I am a Mammal" << endl;
  24.                            break;
  25.                            default: cout << "I don't know what I am" << endl;
  26.                            break;
  27.              }
  28.              theArray[i] = choice; //assign the choices the user entered to the array's counter i.
  29.     }
  30.     //list the choices the user entered
  31.     for (i=0; i<NumAnimalTypes; i++)
  32.     cout << theArray[i] << endl;        
  33.  
  34.  
  35.     system("pause");
  36.     return 0;
  37. }
  38.  
Aug 11 '10 #1

✓ answered by donbock

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.

4 1302
donbock
2,426 Expert 2GB
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.
Aug 11 '10 #2
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?
Aug 11 '10 #3
donbock
2,426 Expert 2GB
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.
Aug 12 '10 #4
Thank You donbock, that was very clear.
Aug 12 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Christian Hackl | last post by:
I honestly wasn't able to find an answer for this design question using Google and Google Groups, so I apologize if it is asked too frequently :) Anyway: Let's say I have a multidimensional array...
2
by: Suddn | last post by:
Here is a real beginner type question: I wish to use constants in place of numbers when dimensioning string arrays. However I get an error when doing so. (Linux/KDevelop) The following gives...
29
by: Joe | last post by:
This is a very basic question, but why can't I do the following ? class schedule { private: static const string mScheduleFile = "schedule.txt"; .... }
79
by: Me | last post by:
Just a question/observation out of frustration. I read in depth the book by Peter Van Der Linden entitled "Expert C Programming" (Deep C Secrets). In particular the chapters entitled: 4: The...
6
by: kobu.selva | last post by:
I was recently part of a little debate on the issue of whether constants and string literals are considered "data objects" in C. I'm more confused now than before. I was always under the...
53
by: SK | last post by:
Hi I appreciate all of the feedback I have recieved. I am enjoying working on my program and I hope that it can be appreciated. I have my program compiling now and I am continuing to work out...
2
by: JJ Feminella | last post by:
This statement is legal C#: public const string day = "Sunday"; but this statement is not: public const string days = new string { "Sun", "Mon", "Tue" }; The C# Programmer's Reference...
11
by: Richard Meister | last post by:
Hi, I'd like to define several constants and make sure that all of them are smaller than a given other constant. I thought this could be done by a simple macro. Something like this: #define...
21
by: utab | last post by:
Hi there, Is there a way to convert a double value to a string. I know that there is fcvt() but I think this function is not a part of the standard library. I want sth from the standard if...
1
by: chiefychf | last post by:
I'm working on a school project and I am having a few issues... The program calls for three arrays a,b,c that have to be sorted, then compared to even or odd and stored in arrays d & e, then merge...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.