Array Declaration and Indexing

UpscaleFlashback2648 avatar
UpscaleFlashback2648
·
·
Download

Start Quiz

Study Flashcards

13 Questions

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?

0

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?

To iterate over each element in the array

Write the syntax to read the value of the third element of an array named scores and assign it to a variable total.

total ← scores[2]

What is the advantage of using a WHILE loop over a FOR loop in array traversal?

You have more control over the loop iteration

Write the syntax to declare an array named marks that can hold 8 elements.

marks[8]

What is the purpose of array indexing?

To access a specific element in the array

What will be the output of the following pseudocode: DIM marks[5]; marks[0] = 10; marks[1] = 20; marks[2] = 30; OUTPUT(marks[1]);

20

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]);

4

What will be the output of the following pseudocode: DIM grades[3]; grades[0] = 90; grades[1] = 80; grades[2] = 70; OUTPUT(grades[2]);

70

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]);

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]);

50

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 named scores 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 the scores 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 the scores 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 the scores array and assigns it to the variable total

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 ], where array-name is the name of the array and size is the number of elements it can hold.
  • Example: scores[ 5 ] declares an array named scores 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 the scores 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 the scores 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 the scores array and assigns it to the variable total.

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 the scores 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 the scores array.

Array Declaration

  • A 1D array is declared using the syntax array-name[ size ], where array-name is the name given to the array and size is the number of elements it can hold.
  • Example: scores[ 5 ] declares an array named scores 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 the scores 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 the scores 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 the scores array and assigns it to the variable total.

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.

Learn how to declare and access elements in a 1D array. Understand the syntax and indexing rules for arrays.

Make Your Own Quizzes and Flashcards

Convert your notes into interactive study material.

Get started for free

More Quizzes Like This

Mastering Arrays in Java
14 questions
Arrays Records Lists & Tuples
5 questions
Array Indexing
10 questions

Array Indexing

GreatestFreesia avatar
GreatestFreesia
Use Quizgecko on...
Browser
Browser