Write a pseudocode for a 2 dimensional array for 30 numbers(
Write a pseudocode for a 2 dimensional array for 30 numbers(
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.
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.
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.
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.
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.
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.