1D Array Homework
1D Array Homework
2. A 1D array, character, with random characters is used to populate 3 separate 1D arrays upper, lower, and digit.
The following modules are
Module Outline description
Populate() It returns an array of size 30, with random characters (uppercase, lowercase or digits)
Transfer() Check each element of array , character
o When element contains an uppercase , it is copied to 1D array upper
o When element contains a lowercase , it is copied to 1D array lower
o When element contains a digit , it is copied to 1D array digit
It returns the number of elements in each array upper, lower and digit via
parameters.
Output() It takes two parameters, name of the array and the number of elements. It displays all
characters in the array
Main() -Call Populate() to fill the array character
-Call transfer() with 1D arrays character, upper, lower and digit
-call output() for each 1D array upper, lower and digit consecutively.
1D arrays ,Character, upper, lower and digit are not global. They are declared in the main module.
3. 10 numbers are input and validated (1-100) into 1D array, numinput. Each number is searched from another 1D
array, numbers, which is filled with 50 random digits(1-100). When a number , numinput, is found, a boolean value
is set to true in a 1D array , found. The modules are
Module Outline description
Fillarray() 50 random numbers from 1-100 are generated and stored in 1D array , numbers
Input() 10 numbers are input and validated in the range 1-100
Main() -Call fillarray() with 1D array, numbers.
-Call input() with 1D array, numinput.
-each value from 1D array, numinput, is searched from 1D array, numbers. when a
value is found, a boolean value is set to true in 1D array, found.
-Output all found values from numinput and then output all non-found values.
4. A global 1D array, alpha , is filled with 10 consecutive uppercase alphabets. The 1st alphabet is validated from A
to Q. A shift is applied on the 1D array, alpha, to move the characters on the left or on the right, the empty spaces
are to be filled with *. The modules are:
Module Outline description
Populate() -Input and validate an uppercase alphabet from A to Q (inclusive)
-1D array, alpha, is populated with 10 consecutive alphabets starting and including
the input character
Output() All characters from alpha are displayed on one row
Leftshift() -Each alphabet is moved to the left by number
-Each empty element in 1D array , alpha , is filled with *
Rightshift() -Each alphabet is moved to the right by number
-Each empty element in 1D array , alpha , is filled with *
Main() -Input and validate the number(1-10) of places, number
-Input and validate the type of shift, L for left and R for right.
-call output()
-Depending on the type of shift, call the appropriate module with number
-Output the shifted array
5 A global 1D array, alpha, contains 16 elements with random digits in odd elements and random uppercase
characters in even elements. The content of each pair of elements is swapped. 1D array, alpha, is to be displayed
before and after the swapping. The modules are
Module Outline description
Input() It fills the array , alpha, with random digits in odd elements and random uppercase
characters in even elements
Output() All characters from alpha are displayed on one row
Processing() The content of each pair of elements is swapped using a count-controlled loop.
6. The contents of two 1D arrays are to be transposed. Each element of 1st array, first, is transposed with each
element of the 2nd array, second.
First
1 2 3 4 5 6 …. 19 20
Second
1 2 3 4 5 6 …. 19 20
The modules are as follows:
Module Outline description
Toplevel() -Call input() with first, smallest ASCII value for a digit and largest ASCII value for
a digit
-Call input() with second, smallest ASCII value for an uppercase alphabet and largest
ASCII value for an uppercase alphabet.
-output both arrays
-call processing() with arrays first and second.
-output both arrays
Input() Using the smallest and largest ASCII values to populate the array with 20 random
characters.
Output() Display all characters on a single row
Processing() Using arrays first and second, each element from each array is swapped.
Note: No use of global arrays.
7. 1000 random numbers are generated in the range 1-50 and stored in 1D array, number. The number of occurrences
of each number 1-50 is stored in 1D array, counter. The most and least popular number(s) are to be displayed. The
modules are
Module Outline description
Counting() Traverse the array, number, and increment the array, counter, on each value from
number.
Toplevel() -Array, number, is populated with random numbers 1-50
-call counting() using arrays number and counter.
-call maxmin() using counter and a parameter.
-search and display ,the numbers 1-50, using the array, counter, for the maximum
and minimum values.
Maxmin() Search the maximum and minimum value from array, counter. It returns the
maximum value and the minimum value via a parameter.
Note: no global arrays are used.
8. The contents of two 1D arrays, first and second, are swapped as illustrated below:
First
1 2 3 4 5 6 …. 19 20
Second
1 2 3 4 5 6 …. 19 20
The modules are
Module Outline description
Input() Array, first, is populated with random digits(0-9) and array ,second, is populated
with random lowercase alphabets.
Output() The content of a 1D array is displayed on one row.
Toplevel() -Call input() using first and second
-display the content of 1D arrays first and second consecutively using output()
-Perform the swapping
- display the content of 1D arrays first and second consecutively using output()
Note: no use of global arrays
9. Three 1D arrays of size 15, upper, lower and digit, are populated with random corresponding characters. Each
array is sorted. The content of each 1D array is then copied into a single 1D array, together, in the order upper,
lower then digit.
The modules are
Module Outline description
Populate() Using the range of ASCII(lowest and highest) values to randomly populate the array
Sorting() Re-arrange the array in ascending order
Toplevel() -Call populate() using upper and the range for uppercase ASCII alphabets.
-Call populate() using lower and the range for lowercase ASCII alphabets.
-Call populate() using digit and the range for digit ASCII.
-call sorting() with upper , lower and digit consecutively.
-Copy the content of upper, lower and digit into 1D array together.
-display the content of 1D array, together.
10. A 1D array, numbers, is populated with numbers in ascending order from 1 to 30. The user inputs a number of
swaps. A swap is exchanging values in any two random elements, X and Y, in the array, numbers. The modules are