Podcast
Questions and Answers
What is the purpose of specifying the size
in an array declaration?
What is the purpose of specifying the size
in an array declaration?
To specify the number of elements the array can hold
What is the index of the first element in an array?
What is the index of the first element in an array?
0
Write the syntax to assign a value to the fifth element of an array named grades
.
Write the syntax to assign a value to the fifth element of an array named grades
.
grades[4] ← value
What is the purpose of the FOR
loop in array traversal?
What is the purpose of the FOR
loop in array traversal?
Signup and view all the answers
Write the syntax to read the value of the third element of an array named scores
and assign it to a variable total
.
Write the syntax to read the value of the third element of an array named scores
and assign it to a variable total
.
Signup and view all the answers
What is the advantage of using a WHILE
loop over a FOR
loop in array traversal?
What is the advantage of using a WHILE
loop over a FOR
loop in array traversal?
Signup and view all the answers
Write the syntax to declare an array named marks
that can hold 8 elements.
Write the syntax to declare an array named marks
that can hold 8 elements.
Signup and view all the answers
What is the purpose of array indexing?
What is the purpose of array indexing?
Signup and view all the answers
What will be the output of the following pseudocode: DIM marks[5]; marks[0] = 10; marks[1] = 20; marks[2] = 30; OUTPUT(marks[1]);
What will be the output of the following pseudocode: DIM marks[5]; marks[0] = 10; marks[1] = 20; marks[2] = 30; OUTPUT(marks[1]);
Signup and view all the answers
What will be the output of the following pseudocode: DIM scores[4]; FOR i = 0 TO 3; scores[i] = i * 2; NEXT i; OUTPUT(scores[2]);
What will be the output of the following pseudocode: DIM scores[4]; FOR i = 0 TO 3; scores[i] = i * 2; NEXT i; OUTPUT(scores[2]);
Signup and view all the answers
What will be the output of the following pseudocode: DIM grades[3]; grades[0] = 90; grades[1] = 80; grades[2] = 70; OUTPUT(grades[2]);
What will be the output of the following pseudocode: DIM grades[3]; grades[0] = 90; grades[1] = 80; grades[2] = 70; OUTPUT(grades[2]);
Signup and view all the answers
What will be the output of the following pseudocode: DIM marks[4]; FOR i = 0 TO 3; marks[i] = i + 1; NEXT i; OUTPUT(marks[3]);
What will be the output of the following pseudocode: DIM marks[4]; FOR i = 0 TO 3; marks[i] = i + 1; NEXT i; OUTPUT(marks[3]);
Signup and view all the answers
What will be the output of the following pseudocode: DIM scores[5]; scores[0] = 10; scores[1] = 20; scores[2] = 30; scores[3] = 40; scores[4] = 50; OUTPUT(scores[4]);
What will be the output of the following pseudocode: DIM scores[5]; scores[0] = 10; scores[1] = 20; scores[2] = 30; scores[3] = 40; scores[4] = 50; OUTPUT(scores[4]);
Signup and view all the answers
Study Notes
Array Declaration
- In pseudocode, a 1D array is declared using the following syntax:
array-name[ size ]
-
array-name
is the name given to the array -
size
is the number of elements the array can hold - Example:
scores[5]
declares an array namedscores
that can hold 5 elements
Array Indexing
- Array elements are accessed using an index, which is a numerical value that corresponds to a specific position in the array
- Indexing starts at 0, meaning the first element is at index 0, the second element is at index 1, and so on
- To access an element, use the following syntax:
array-name[ index ]
- Example:
scores[2]
accesses the third element in thescores
array (since indexing starts at 0)
Array Operations
-
Assignment: Assign a value to an array element using the following syntax:
array-name[ index ] ← value
- Example:
scores[2] ← 90
assigns the value 90 to the third element in thescores
array -
Reading: Read the value of an array element using the following syntax:
variable ← array-name[ index ]
- Example:
total ← scores[2]
reads the value of the third element in thescores
array and assigns it to the variabletotal
Array Traversal
- For Loop: Traverse an array using a for loop, which iterates over each element in the array
- Syntax:
FOR index FROM 0 TO size - 1 DO ... END FOR
- Example:
FOR i FROM 0 TO 4 DO
OUTPUT scores[i]
END FOR
This code outputs each element in the scores
array.
- While Loop: Traverse an array using a while loop, which iterates over each element in the array
- Syntax:
index ← 0; WHILE index < size DO ... index ← index + 1 END WHILE
- Example:
i ← 0
WHILE i < 5 DO
OUTPUT scores[i]
i ← i + 1
END WHILE
This code outputs each element in the scores
array.
Array Declaration
- A 1D array is declared using the syntax
array-name[ size ]
, wherearray-name
is the name of the array andsize
is the number of elements it can hold. - Example:
scores[ 5 ]
declares an array namedscores
that can hold 5 elements.
Array Indexing
- Array elements are accessed using an index, which is a numerical value corresponding to a specific position in the array.
- Indexing starts at 0, meaning the first element is at index 0, the second element is at index 1, and so on.
- The syntax for accessing an element is
array-name[ index ]
. - Example:
scores[ 2 ]
accesses the third element in thescores
array.
Array Operations
-
Assignment: A value is assigned to an array element using the syntax
array-name[ index ] ← value
. - Example:
scores[ 2 ] ← 90
assigns the value 90 to the third element in thescores
array. -
Reading: The value of an array element is read using the syntax
variable ← array-name[ index ]
. - Example:
total ← scores[ 2 ]
reads the value of the third element in thescores
array and assigns it to the variabletotal
.
Array Traversal
Using For Loop
- A for loop is used to traverse an array, iterating over each element in the array.
- The syntax for a for loop is
FOR index FROM 0 TO size - 1 DO...END FOR
. - Example:
FOR i FROM 0 TO 4 DO OUTPUT scores[ i ] END FOR
outputs each element in thescores
array.
Using While Loop
- A while loop is also used to traverse an array, iterating over each element in the array.
- The syntax for a while loop is
index ← 0; WHILE index < size DO...index ← index + 1 END WHILE
. - Example:
i ← 0; WHILE i < 5 DO OUTPUT scores[ i ]; i ← i + 1 END WHILE
outputs each element in thescores
array.
Array Declaration
- A 1D array is declared using the syntax
array-name[ size ]
, wherearray-name
is the name given to the array andsize
is the number of elements it can hold. - Example:
scores[ 5 ]
declares an array namedscores
that can hold 5 elements.
Array Indexing
- Array elements are accessed using an index, which is a numerical value that corresponds to a specific position in the array.
- Indexing starts at 0, meaning the first element is at index 0, the second element is at index 1, and so on.
- The syntax to access an element is
array-name[ index ]
. - Example:
scores[ 2 ]
accesses the third element in thescores
array.
Array Operations
-
Assignment: Assign a value to an array element using
array-name[ index ] ← value
. - Example:
scores[ 2 ] ← 90
assigns the value 90 to the third element in thescores
array. -
Reading: Read the value of an array element using
variable ← array-name[ index ]
. - Example:
total ← scores[ 2 ]
reads the value of the third element in thescores
array and assigns it to the variabletotal
.
Array Traversal
- For Loop: Traverse an array using a for loop, which iterates over each element in the array.
- The syntax is
FOR index FROM 0 TO size - 1 DO...END FOR
. - Example:
FOR i FROM 0 TO 4 DO
OUTPUT scores[i]
END FOR
This code outputs each element in the scores
array.
- While Loop: Traverse an array using a while loop, which iterates over each element in the array.
- The syntax is
index ← 0; WHILE index < size DO...index ← index + 1 END WHILE
. - Example:
i ← 0
WHILE i < 5 DO
OUTPUT scores[i]
i ← i + 1
END WHILE
This code outputs each element in the scores
array.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
Learn how to declare and access elements in a 1D array. Understand the syntax and indexing rules for arrays.