Hello

I'm a beginner at c++ I got these questions for homework. Would appreciate answers to these questions.

Consider the following code for these questions

                  const int NUMBER_OF_ITEMS = 5;
                  double nums [NUMBER_OF_ITEMS];

Question 1
What is the index of the first and last element in the array?

Question 2
Write a statement to store the value 5.6 in the 3rd location of the array, and another statement to output the 3rd location of the array to the console.

Question 3
Write a loop that will fill up the array by asking the user for input.

Question 4
Write down some code to output all the elements of the array that are greater than 22.

Question 5
What is the meaning of the word 'const', and give one good reason why it is good to declare a const variable for the size of an array?

Recommended Answers

All 2 Replies

Its not a good way to give away all the answers for a homework. You have to do your part. So lets see...

Q1. Indexing in C++ starts from 0, not from 1. So last element index is size-1. You do the rest of calculations.

Q2. What will be the index of 3rd position in an array, figure it out from the above answer. And each memory space in array is denoted by arrayname[index](eg. a[1], c[2], mine[30] etc etc). How do you insert a value to a variable?? int a=10;?? Right?? Substitute the array index instead of that "a".

Q3.

for(int i=0;i<size of the array; i++)
                {
                    \\insertion statement for each element;
                }

Q4.

for(int i=0;i<size of array;i++)
    {
        if(element>22)
           { 
             \\output that particular element
           }
    }

Q5.

Const means the value of that variable is fixed or constant. And cannot be changed during run time. Well abt one good reason??? Not sure, still here is my answer: size of an array depends upon the number of elements it have and the type of variable it stores. So in a program where the number of elements is known in advance, the size can be made a constant, making it immune to further expansions(Can be expaned by dynamic instialization otherwise, or while passing an reference of array to a UD Function). And restricting the amount of memory a array use up during the runtime...

And for the piece of code you gave, unless a value for the variable is defined as constant, you cannot put that variable in place of size of array during array declairation ie. size of an array cannot be defined during runtime (Some exceptions are there).

Question 5
What is the meaning of the word 'const', and give one good reason why it is good to declare a const variable for the size of an array?

The word "const" is short for constant, and really all this does is ask the compiler to keep an eye out for this variable to make sure that you don't change it somewhere down the line. So you can think of a const variable just the same as any other variable, except that the compiler is doing you the favor of keeping an eye on it for you to make sure it doesn't somehow get changed. But just a side note, a const variable, like const int x = 5; doesn't have an actual memory location where it resides. Rather, since the value is never going to change, the compiler just keeps the value of 5 "in it's head" so to speak, and just substitutes the value 5 whenever it sees the variable "x" used. However, there are a few exceptions to this, like if the variable is declared as "static" or if it is a global variable, then it will have an actual address in memory.

Generally, if you declare an array of a certain size, then you can't change the size after it has been contructed in memory. And the size of the array needs to be known, or kept around, like in a variable, so that when you write data into the array you don't go overboard and write beyond the end of the array, or else you will most likely cause your program to crash or mess up other data values in your program. So this variable that stores the array size certainly should be a const variable, since the size of the array never changes and your program needs and uses this variable to guide it when writing data into the array.

So by declaring the size of an array variable as const, the compiler will just keep an eye on it to be sure it never changes, or else will alert you if it does with an error message.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.