Pseudocode Solutions for 1D Arrays

100% found this document useful (1 vote)
1K views6 pages

Uploaded by

Affan
  • Problem 1: Print Negative Elements
  • Problem 3: Find Maximum and Minimum Numbers
  • Problem 2: Count Even and Odd Elements
  • Problem 4: Find the Second Largest Element
  • Problem 6: Check Array Order
  • Problem 7: Search a Name in List
  • Problem 5: Reverse the Array
  • Problem 8: Find Shortest and Longest Name
  • Problem 9: Count Specific Characters

Computer Science (2210) Paper 2

Solved Problems on 1D Array

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

Common questions

Powered by AI

The search algorithm uses a WHILE loop to check each name against the target name, `NameToSearch`. If a match is found, it outputs the position, otherwise it continues until all elements are checked. If no match is found after checking all elements, it outputs a 'not found' message. This straightforward approach provides clear feedback based on the searching result .

The algorithm counts spaces in each name using a nested loop; a name with no spaces indicates a single-part name. It involves iterating over characters and incrementing a space counter for each space found. If a name has zero spaces, it increments the count of single-part names, which is then outputted .

The reversing algorithm swaps elements from the start with those from the end, moving towards the center. It leverages a temporary variable to facilitate swapping. This in-place approach works because it reduces required space, with each element being used as part of the swapping process until all are repositioned .

To find the sum, the algorithm declares and initializes a sum variable to 0. It then uses a loop to iterate through the array, adding each element's value to the sum variable. After completing the loop, the sum is printed .

The algorithm examines each name for spaces using nested loops. It counts spaces by iterating over each character of a name, incrementing a counter when a space is found. After counting, it checks if the count equals 2 (indicating the presence of all three parts) and outputs the name if true .

The algorithm initializes variables to track the shortest and longest lengths, and iterates through the list using a FOR loop. For each name, it checks the character length; updating the tracked minimum and maximum lengths and corresponding names when more extreme lengths are found. This ensures that by the end of the iteration, the shortest and longest names are accurately identified and printed .

The given pseudocode uses a nested loop to check each element against the others to determine uniqueness. This results in a time complexity of O(N^2), which can be inefficient for large arrays. Potential improvements could include using a hash table to track element occurrences, reducing time complexity to O(N).

The algorithm initializes an integer index and iterates over each element in the array. If an element is negative, it prints the element. Specifically, it uses a FOR loop from 1 to N (where N is the number of elements), checks each element with the condition `NumList[Index] < 0`, and prints the element if it meets this condition .

The algorithm uses integer counters for even and odd numbers. It iterates over the array with a FOR loop, checks each element using the modulus operation `MOD 2`; if the result is 0, the element is even and increments the even counter, otherwise it increments the odd counter. Finally, it outputs the counts of even and odd numbers .

The algorithm initializes two variables, Large and SecLarge, to the first element of the array. It iterates through the array from the second element onward. If an element is greater than Large, it assigns Large's value to SecLarge and the current element to Large. Otherwise, if the element is greater than SecLarge, this element is assigned to SecLarge. At the end, SecLarge holds the second largest number, which is then printed .

Page | 1  
 
Computer Science (2210) Paper 2 
Solved Problems on 1D Array 
 
Problem-1: 
A one-dimensional array, NumList[] c
Page | 2  
 
Problem-4: 
A one-dimensional array, NumList[] contains N integer numbers. Write an algorithm in the form of 
ps
Page | 3  
 
Problem-6: 
A one-dimensional array, NumList[] contains N unique integer numbers. Write an algorithm in the form
Page | 4  
 
Problem-8: 
A one-dimensional array, NumList[] contains N numbers. Write an algorithm in the form of pseudocode
Page | 5  
 
Problem-11: 
A one-dimensional array, NameList[] contains names of N students in a class. Write an algorithm in
Page | 6  
 
Problem-13: 
A one-dimensional array, NameList[] contains names of N students in a class. The names contain one,

You might also like