SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,julia Lobur
Chapter:
Arrays
Exercise:
Self-test Exercises
Question:3 | ISBN:9780321531346 | Edition: 7

Question

Identify any errors in the following array declarations.

a. int x[4] = { 8, 7, 6, 4, 3 };

b. int x[] = { 8, 7, 6, 4 };

c. const int SIZE = 4;

d. int x[SIZE];

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

a)

Consider given array declaration:

          int x[4] = { 8, 7, 6, 4, 3 };

Error: The name of the array is 'x' and initialized the size is 4. That means, it accepts only 4 elements in the array. But, the given array stored 5 elements, those are (8, 7, 6, 4, 3 ) . So, it denotes an compiler errors.  So, the correct array declaration for initilized an array is declared any four elements.

For example: int x[4] = { 8, 7, 6, 4}; (or)   int x[4] = { 7, 6, 4, 3 }; ,etc...

 

b)

Consider given array declaration:

          int x[] = { 8, 7, 6, 4 };

Correct:  The reason is that, the initialized array size of 'x' is empty. It takes any number of elements in the array. So, given array declaration is correct.

 

c)

Consider given data:

const int SIZE = 4;

Correct: The constant values always represents in capital letters. The initialized varial name "SIZE" is an constant integer data type and size is 4. It accepts four elements in an array declaration.

 

d)

Consider given array declaration:

 int x[SIZE];

Correct:  The constant array name "SIZE" is stored in the variable 'x'.  If the array "SIZE" is defined the value is 4, then the array 'x' accepts only four elements.

0 0

Discussions

Post the discussion to improve the above solution.