Chapter 8: Problem 16
Determine whether the following array declarations are valid. If a declaration is valid, determine the size of the array. a. int list[] = {18, 13, 14, 16}; b. int x[10] = {1, 7, 5, 3, 2, 8}; c. double y[4] = {2.0, 5.0, 8.0, 11.0, 14.0}; d. double lengths[] = {8.2, 3.9, 6.4, 5.7, 7.3}; e. int list[7] = {12, 13, , 14, 16, , 8}; f. string names[8] = {"John","Lisa", "Chris", "Katie"};
Short Answer
Step by step solution
Analyze Declaration a
Analyze Declaration b
Analyze Declaration c
Analyze Declaration d
Analyze Declaration e
Analyze Declaration f
Unlock Step-by-Step Solutions & Ace Your Exams!
-
Full Textbook Solutions
Get detailed explanations and key concepts
-
Unlimited Al creation
Al flashcards, explanations, exams and more...
-
Ads-free access
To over 500 millions flashcards
-
Money-back guarantee
We refund you if you fail your exam.
Over 30 million students worldwide already upgrade their learning with Vaia!
Key Concepts
These are the key concepts you need to understand to accurately answer the question.
Array Initialization
- Implicit Size Deduction: If you use an initializer list without specifying a size, like `int list[] = {18, 13, 14, 16};`, C++ automatically deduces the size of the array based on the number of elements within the braces. This method requires that all elements are provided upfront.
- Explicit Size Declaration: You can specify the size of the array directly in the declaration. If fewer initializers are provided than the declared size, the remaining elements will automatically get a zero value in the case of numeric data types, or default values for others, like `int x[10] = {1, 7, 5, 3, 2, 8};`.
- Complete Initialization: All elements of the array can be explicitly initialized, ensuring no default values intervene, and each index has a direct corresponding initializer.
Array Size
When declaring an array, the size may be:
- Implicitly Determined: When you use `int list[] = {18, 13, 14, 16};`, the size is automatically determined by the number of initializers provided (here it’s 4).
- Explicitly Specified: Declaring an array with a size directly, for example, `int x[10] = ...`, means you have defined the array to hold 10 elements. If fewer values are provided, the rest default to zero.
Data Types in C++
When declaring an array, you assign it a data type which dictates the type of elements it can hold. For instance:
- Integers: Use `int` arrays, like `int list[] = {18, 13, 14, 16};`, best for storing whole numbers.
- Floating Points: Use `double` or `float` arrays for decimal-requiring values, such as `double lengths[] = {8.2, 3.9, 6.4, 5.7, 7.3};`.
- Strings: Arrays of `string` can hold text, e.g., `string names[8] = {
Syntax Errors in C++
- Improper Initializer Usage: For instance, as seen in the declaration `int list[7] = {12, 13, , 14, 16, , 8};`, C++ doesn't allow initializer lists with missing values, hence the error arises from the commas without numbers.
- Conflicting Sizes and Initializers: Providing more initializers than the specified size as in `double y[4] = {2.0, 5.0, 8.0, 11.0, 14.0};`, causes errors since the count of initializers exceeds the allocated array size.