1d Array Question Examples Solved
1d Array Question Examples Solved
Problem-1:
A one-dimensional array, NumList[] contains N integer numbers. Write an algorithm in the form of
pseudocode to print all negative elements in this array.
Solution:
DECLARE Index: INTEGER
FOR Index ← 1 TO N
IF NumList[Index] < 0
THEN
PRINT NumList[Index]
ENDIF
NEXT Index
Problem-2:
A one-dimensional array, NumList[] contains N integer numbers. Write an algorithm in the form of
pseudocode to find and output the sum of all array elements.
Solution:
DECLARE Index, Sum: INTEGER
Sum ← 0
FOR Index ← 1 TO N
Sum ← Sum + NumList[Index]
NEXT Index
PRINT “Sum of array elements is:”, Sum
Problem-3:
A one-dimensional array, NumList[] contains N integer numbers. Write an algorithm in the form of
pseudocode to count and output the total number of negative elements in this array.
Solution:
DECLARE Index, NegCount: INTEGER
NegCount ← 0
FOR Index ← 1 TO N
IF NumList[Index] < 0
THEN
NegCount ← NegCount + 1
ENDIF
NEXT Index
PRINT “Number of negative elements in this array is:”, NegCount
Page | 1
Problem-4:
A one-dimensional array, NumList[] contains N integer numbers. Write an algorithm in the form of
pseudocode to count and output the total number of even and odd elements in this array.
Solution:
DECLARE Index, EvenCount, OddCount: INTEGER
EvenCount ← 0
OddCount ← 0
FOR Index ← 1 TO N
IF NumList[Index] MOD 2 = 0
THEN
EvenCount ← EvenCount + 1
ELSE
OddCount ← OddCount + 1
ENDIF
NEXT Index
PRINT “Number of even elements in this array is:”, EvenCount
PRINT “Number of odd elements in this array is:”, OddCount
Problem-5:
A one-dimensional array, NumList[] contains N integer numbers. Write an algorithm in the form of
pseudocode to output the maximum and the minimum element in this array.
Solution:
DECLARE Index, MaxNum, MinNum: INTEGER
MaxNum ← NumList[1]
MinNum ← NumList[1]
FOR Index ← 2 TO N
IF NumList[Index] > MaxNum
THEN
MaxNum ← NumList[Index]
ELSE
IF NumList[Index] < MinNum
THEN
MinNum ← NumList[Index]
ENDIF
ENDIF
NEXT Index
PRINT “The highest number in this array is:”, MaxNum
PRINT “The lowest number in this array is:”, MinNum
Page | 2
Problem-6:
A one-dimensional array, NumList[] contains N unique integer numbers. Write an algorithm in the form
of pseudocode to find and output second largest element in an array.
Solution:
DECLARE Index, Large, SecLarge: INTEGER
Large ← NumList[1]
SecLarge ← NumList[1]
FOR Index ← 2 TO N
IF NumList[Index] > Large
THEN
SecLarge ← Large
Large ← NumList[Index]
ELSE
IF NumList[Index] > SecLarge
THEN
SecLarge ← NumList[Index]
ENDIF
ENDIF
NEXT Index
PRINT “The second largest number in this array is:”, SecLarge
Problem-7:
A one-dimensional array, NumList[] contains N numbers. Write an algorithm in the form of pseudocode
to identify and print all the unique elements in this array. If no unique element found, the algorithm should
print an appropriate message.
Solution:
DECLARE Index, UniqueCount, chkIndex: INTEGER
DECLADE IsUnique: BOOLEAN
UniqueCount ← 0
FOR chkIndex ← 1 TO N
IsUnique ← TRUE
Index ← 1
WHILE Index <= N AND IsUnique = TRUE
IF chkIndex <> Index AND NumList[chkIndex] = NumList[Index]
THEN
IsUnique ← FALSE
ENDIF
Index ← Index + 1
ENDWHILE
IF IsUnique = TRUE
THEN
PRINT NumList[chkIndex]
UniqueCount ← UniqueCount + 1
ENDIF
NEXT chkIndex
IF UniqueCount = 0
THEN
PRINT “There is no unique element in the array.”
ENDIF
Page | 3
Problem-8:
A one-dimensional array, NumList[] contains N numbers. Write an algorithm in the form of pseudocode
to revert the array. For example, the array NumList contains the following values:
[33, 22, 11, 44, 55, 66]
After reverting, the contents of the array will be as follows:
[66, 55, 44, 11, 22, 33]
Solution:
DECLARE Index, Mid, Temp: INTEGER
Mid ← N DIV 2 // DIV is used for integer division
FOR Index ← 1 TO Mid
Temp ← NumList[Index]
NumList[Index] ← NumList[N ─ Index + 1]
NumList[N ─ Index + 1] ← Temp
NEXT Index
Problem-9:
A one-dimensional array, NumList[] contains N numbers that are sorted in ascending order. Write an
algorithm in the form of pseudocode to sort the elements of this array in descending order.
Solution:
DECLARE Index, Mid, Temp: INTEGER
Mid ← N DIV 2 // DIV is used for integer division
FOR Index ← 1 TO Mid
Temp ← NumList[Index]
NumList[Index] ← NumList[N ─ Index + 1]
NumList[N ─ Index + 1] ← Temp
NEXT Index
Problem-10:
A one-dimensional array, NameList[] contains names of N students in a class. Write an algorithm in the
form of pseudocode to input a name (using the identifier, NameToSearch) to search and output the location
of this name in the array if found, otherwise output an appropriate message.
Solution:
DECLARE Index, Location: INTEGER
DECLARE NameToSearch: STRING
OUTPUT “Enter the name to search”
INPUT NameToSearch
Location ← 0
Index ← 1
WHILE Index <= N AND Location = 0
IF NameList[Index] = NameToSearch
THEN
Location ← Index
ENDIF
Index ← Index + 1
ENDWHILE
IF Location = 0
THEN
OUTPUT “This name is not found in the list”
ELSE
OUTPUT “The name is found at location ”, Location
ENDIF
Page | 4
Problem-11:
A one-dimensional array, NameList[] contains names of N students in a class. Write an algorithm in the
form of pseudocode to identify and output the names with the shortest and the longest lengths.
Solution:
DECLARE Index, Short, Long, length: INTEGER
DECLARE ShortName, LongName: STRING
Short ← 0
Long ← 100
ShortName ← “”
LongName ← “”
FOR Index ← 1 TO N
length ← LENGTH(NameList[Index])
IF length > Long
THEN
Long ← length
LongName ← NameList[Index]
ENDIF
IF length < Short
THEN
Short ← length
ShortName ← NameList[Index]
ENDIF
NEXT Index
PRINT “The shortest name in this array is:”, ShortName
PRINT “The longest name in this array is:”, LongName
Problem-12:
A one-dimensional array, NameList[] contains names of N students in a class. Write an algorithm in the
form of pseudocode to identify and output the names that start with the letter ‘A’.
Solution:
DECLARE Index: INTEGER
DECLARE FirstChar: CHAR
FOR Index ← 1 TO N
FirstChar ← MID(NameList[Index], 1, 1)
IF FirstChar = ‘A’
THEN
OUTPUT NameList[Index]
ENDIF
NEXT Index
Page | 5
Problem-13:
A one-dimensional array, NameList[] contains names of N students in a class. The names contain one,
two or all of the three parts (First name, Middle name and Last name). Two parts of a name are separated
by a single space. Write an algorithm in the form of pseudocode to identify and output the names with all
the three parts.
Solution:
DECLARE Index, chrIndex, SpaceCount: INTEGER
DECLARE NextChar: CHAR
FOR Index ← 1 TO N
SpaceCount ← 0
FOR chrIndex ← 1 TO LENGTH(NameList[Index])
NextChar ← MID(NameList[Index], chrIndex, 1)
IF NextChar = ‘ ’
THEN
SpaceCount ← SpaceCount + 1
ENDIF
NEXT chrIndex
IF SpaceCount = 2
THEN
OUTPUT NameList[Index]
ENDIF
NEXT Index
Problem-14:
A one-dimensional array, NameList[] contains names of N students in a class. The names contain one,
two or all of the three parts (First name, Middle name and Last name). Two parts of a name are separated
by a single space. Write an algorithm in the form of pseudocode to count the names with only one part and
output the result of this counting.
Solution:
DECLARE Index, chrIndex, SpaceCount, NameCount: INTEGER
DECLARE NextChar: CHAR
NameCount ← 0
FOR Index ← 1 TO N
SpaceCount ← 0
FOR chrIndex ← 1 TO LENGTH(NameList[Index])
NextChar ← MID(NameList[Index], chrIndex, 1)
IF NextChar = ‘ ’
THEN
SpaceCount ← SpaceCount + 1
ENDIF
NEXT chrIndex
IF SpaceCount = 0
THEN
NameCount ← NameCount + 1
ENDIF
NEXT Index
Page | 6