0% found this document useful (0 votes)
14 views

Write a pseudocode for a 2 dimensional array for 30 numbers(

The document provides pseudocode examples for declaring, initializing, and manipulating both 1D and 2D arrays. It includes explanations for accessing, modifying, iterating through elements, finding sums, and identifying the largest element in an array. Additionally, it demonstrates how to declare and initialize a 2D array with specific dimensions.

Uploaded by

Kepha Ogada
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Write a pseudocode for a 2 dimensional array for 30 numbers(

The document provides pseudocode examples for declaring, initializing, and manipulating both 1D and 2D arrays. It includes explanations for accessing, modifying, iterating through elements, finding sums, and identifying the largest element in an array. Additionally, it demonstrates how to declare and initialize a 2D array with specific dimensions.

Uploaded by

Kepha Ogada
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

Write a pseudocode for a 2 dimensional array for 30 numbers(3 lists of 10)

Here are some common array-related questions and answers in


pseudocode, along with explanations to help you understand the concepts:

Question 1: How do you declare and initialize a 1D array in pseudocode?

Answer:
Code
DECLARE myArray AS ARRAY OF INTEGER [5] // Declares an array named myArray to hold 5
integers
myArray[0] <- 10
myArray[1] <- 20
myArray[2] <- 30
myArray[3] <- 40
myArray[4] <- 50

Explanation:
• DECLARE myArray AS ARRAY OF INTEGER [5] declares an array
named myArray that can store integers. The [5] specifies that the
array has 5 elements (indexed from 0 to 4).
• myArray[0] <- 10 assigns the value 10 to the first element of the array
(index 0).
• The following lines similarly assign values to other elements.

Question 2: How do you access and modify elements in an array?

Answer:
Code
// Accessing an element
value <- myArray[2] // Assigns the value at index 2 to the variable 'value'

// Modifying an element
myArray[1] <- 25 // Changes the value at index 1 to 25

Explanation:
• value <- myArray[2] retrieves the value stored at index 2 of the array
myArray and assigns it to the variable value.
• myArray[1] <- 25 changes the value stored at index 1 of the array
myArray to 25.

Question 3: How do you iterate through all elements of an array?

Answer:
Code
FOR i FROM 0 TO 4 DO
// Code to process each element
OUTPUT myArray[i]
ENDFOR

Explanation:
• The FOR loop iterates through the array elements from index 0 to 4.
• Inside the loop, myArray[i] accesses the current element, and
OUTPUT myArray[i] displays its value.

Question 4: How do you find the sum of all elements in an array?

Answer:
Code
total <- 0
FOR i FROM 0 TO 4 DO
total <- total + myArray[i]
ENDFOR
OUTPUT total

Explanation:
• The total variable is initialized to 0.
• The loop iterates through all elements of the array.
• In each iteration, the current element myArray[i] is added to the total.
• Finally, the total value is displayed.

Question 5: How do you find the largest element in an array?

Answer:
Code
largest <- myArray[0] // Initialize largest with the first element
FOR i FROM 1 TO 4 DO
IF myArray[i] > largest THEN
largest <- myArray[i]
ENDIF
ENDFOR
OUTPUT largest

Explanation:
• largest is initialized with the first element of the array.
• The loop iterates through the remaining elements of the array.
• Inside the loop, the current element myArray[i] is compared with
largest.
• If myArray[i] is greater than largest, largest is updated with the value
of myArray[i].
• Finally, the largest value is displayed.

Question 6: How do you declare and initialize a 2D array in pseudocode?

Answer:
Code
DECLARE myMatrix AS ARRAY OF INTEGER [3, 2] // Declares a 3x2 matrix (2D array)
myMatrix[0, 0] <- 1
myMatrix[0, 1] <- 2
myMatrix[1, 0] <- 3
myMatrix[1, 1] <- 4
myMatrix[2, 0] <- 5
myMatrix[2, 1] <- 6

Explanation:
• DECLARE myMatrix AS ARRAY OF INTEGER [3, 2] declares a 2D
array named myMatrix that can store integers. The [3, 2] specifies
that the array has 3 rows and 2 columns.
• myMatrix[0, 0] <- 1 assigns the value 1 to the element at row 0,
column 0.

You might also like