Podcast
Questions and Answers
What is the general declaration of one-dimensional arrays in C++?
What is the general declaration of one-dimensional arrays in C++?
- dataType arrayName;
- dataType [arraySize];
- dataType arrayName[arraySize]; (correct)
- arrayName [dataType];
How can the elements of a one-dimensional array be initialized?
How can the elements of a one-dimensional array be initialized?
- By using both individual assignment and array initializer (correct)
- Only by using an array initializer
- Only by assigning values to each element individually
- It is not possible to initialize array elements
What is the purpose of the index in an array?
What is the purpose of the index in an array?
- To specify the name of the array
- To specify the data type of the array
- To access a specific element in the array (correct)
- To declare the size of the array
What is the output of the following code: int x [] = { 12, 3, 5, 0, 11, 7, 30, 100, 22 };
?
What is the output of the following code: int x [] = { 12, 3, 5, 0, 11, 7, 30, 100, 22 };
?
How can you access the first element of an array?
How can you access the first element of an array?
What is the result of the following code: cin >> mark[i-1];
where i
is 2 and the input value is 20?
What is the result of the following code: cin >> mark[i-1];
where i
is 2 and the input value is 20?
What is the output of the following code: cout > mark[i-1];
where i
is 2?
What is the output of the following code: cout > mark[i-1];
where i
is 2?
What is the purpose of the code int numbers = {7, 5, 6, 12, 35};
?
What is the purpose of the code int numbers = {7, 5, 6, 12, 35};
?
Study Notes
One-Dimensional Array
- A one-dimensional array is a single variable that stores multiple elements of the same data type.
- The general declaration of a one-dimensional array is:
dataType arrayName[arraySize];
- Each element in an array is called an element, and they are all of the same type, with only the values varying.
Initializing Array Elements
- Array elements can be initialized in several ways, including:
- Assigning a value to the first element of an array:
age = 18;
- Assigning a value to the last element of an array:
age = 19;
- Assigning values to all elements of an array:
age = { 18, 17, 18, 19, 20, 17, 18, 16, 19 };
- Using an initialization list:
int x[] = { 12, 3, 5, 0, 11, 7, 30, 100, 22 };
- Assigning a value to the first element of an array:
Accessing Array Elements
- Array elements can be accessed using the array name followed by brackets
[]
containing the array index. - The array index is a variable or constant that specifies the element to be accessed.
- Examples of accessing array elements include:
- Assigning the first element of an array to a variable:
x = num[0];
- Printing the second element of an array:
cout << num[1];
- Using a loop to access array elements:
for (int i = 0; i < 5; i++) { ... }
- Taking input from the user and inserting it into the ith index of an array:
cin >> mark[i-1];
- Assigning the first element of an array to a variable:
Example Program
- A C++ program to read a 1-D array and print it:
#include
int main() {
int numbers[] = {7, 5, 6, 12, 35};
cout << numbers[0]; // prints the first element of the array
return 0;
}
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
Learn about one-dimensional arrays, their declaration, and initialization in programming languages. Understand the elements and data types of arrays.