Array Declaration and Indexing
13 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

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?

<p>To iterate over each element in the array</p> 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.

<p>total ← scores[2]</p> Signup and view all the answers

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

<p>You have more control over the loop iteration</p> Signup and view all the answers

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

<p>marks[8]</p> Signup and view all the answers

What is the purpose of array indexing?

<p>To access a specific element in the array</p> 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]);

<p>20</p> 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]);

<p>4</p> 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]);

<p>70</p> 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]);

<p>4</p> 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]);

<p>50</p> 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 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 &lt; size DO ... index ← index + 1 END WHILE
  • Example:
i ← 0
WHILE i &lt; 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 &lt; size DO...index ← index + 1 END WHILE.
  • Example: i ← 0; WHILE i &lt; 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 &lt; size DO...index ← index + 1 END WHILE.
  • Example:
i ← 0
WHILE i &lt; 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.

Quiz Team

Description

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

More Like This

Array Indexing
10 questions

Array Indexing

GreatestFreesia avatar
GreatestFreesia
NumPy Array Indexing and Slicing
6 questions
Programming Week 10 Flashcards
5 questions
Use Quizgecko on...
Browser
Browser