W05-Req-Array 1D & 2D
W05-Req-Array 1D & 2D
Lab 05
1D & 2D Array
CS10001 – Introduction to Programming L05 – 1D & 2D Array
1 Review
- 1D arrays provide a way to store multiple elements of the same type in a contiguous block of memory.
+ Arrays are fixed in size, and elements are accessed via indices starting at 0.
+ They are ideal for tasks where a collection of related data needs to be processed, such as lists of
numbers, strings, or objects…
- 2D arrays are used to store data in a table or grid format, where each element can be accessed using
two indices (row and column).
+ They are particularly useful for representing matrices, tables, or any other data structure that requires
a grid-like organization.
+ You can declare, initialize, and manipulate these arrays using nested loops and standard 1D array
operations.
2
CS10001 – Introduction to Programming L05 – 1D & 2D Array
2 Requirements
2.1 In-class assignment
2.1.1. P01
Input an array of integers with N elements, find the largest even number and the smallest odd number.
Input Data:
Includes 2 lines.
Line 1: An integer, the number of elements in the array.
Line 2: N integers, separated by a space.
Output Data:
A single line, consisting of the largest even number and the smallest odd number, separated by a
space
Example:
Input Output
5 83
26385
2.1.2. P02
Input a sequence of N positive integers with values ranging from 1 to 100, calculate the average value
of the even numbers in the sequence.
Input Data:
Includes 2 lines.
Line 1: An integer, the number of elements in the array.
Line 2: N integers, separated by a space.
Output Data:
The average value of the even numbers, rounded to 2 decimal places.
Example:
Input Output
5 5.33
26385
3
CS10001 – Introduction to Programming L05 – 1D & 2D Array
2.1.3. P05
Input an array of N integers, with 1 ≤ N ≤ 100, sort the even numbers in the array in ascending order.
Input Data:
Includes 2 lines.
Line 1: An integer, the number of elements in the array.
Line 2: N integers, separated by a space.
Output Data:
Includes 2 lines.
Line 1: An integer, the number of elements in the array.
Line 2: The array with the even numbers sorted in ascending order.
Example:
Input Output
5 5
26147 24167
2.1.4. P315
Write a function to find the largest value in a matrix of real numbers.
2.1.5. P320
Calculate the sum of positive numbers in a matrix of real numbers.
2.2 Homework
2.2.1. P04
Input an array of N integers, with 1 ≤ N ≤ 100, and print all the prime numbers in the array.
Input Data:
Includes 2 lines.
Line 1: An integer, the number of elements in the array.
Line 2: N integers, separated by a space.
Output Data:
Includes 2 lines.
4
CS10001 – Introduction to Programming L05 – 1D & 2D Array
2.2.2. P08
Input a one-dimensional array of n positive integers. Remove k consecutive elements from the array
starting at a given position i.
Input Data:
Line 1: Enter an integer 0 < n ≤ 10⁹ (size of the array), an integer 0 ≤ i ≤ n-1 (position i), and k ≥ 0
(number of elements to remove).
Line 2: Enter n positive integers into the array.
Output Data:
Print the array after removal.
Example:
Input Output
732 12 67 45 0 2
12 67 45 87 678 0 2
721 12 67 87 678 0 2
12 67 45 87 678 0 2
10 3 4 9 18 76 34 23 21
9 18 76 87 54 65 67 34 23 21
2.2.3. P12
Input a one-dimensional array of n positive integers. Find the second largest number in the array.
Input Data:
Line 1: Enter an integer 0 < n ≤ 10⁹ (size of the array).
Line 2: Enter n positive integers into the array.
Output Data:
Print the second largest number.
Example:
Input Output
8 87
5 12 5 45 87 678 5
5
CS10001 – Introduction to Programming L05 – 1D & 2D Array
2.2.4. P26
Given two arrays a and b with m and n elements respectively, where the elements in each array are
distinct, find the common values that appear in both arrays. Extension: Assume there may be duplicate
elements.
Input Data:
Line 1: Enter integers 0 < m, n ≤ 10⁹ (the number of elements in the arrays).
Line 2: Enter array a, and array b on separate lines.
Conditions: The elements in each array are distinct from each other.
Output Data:
Find and print the values that appear in both arrays. Extension: Assume there may be duplicate
elements.
Example:
Input Output
76 5 12 67 45
3 5 12 67 80 45 678 5 23 12 45 56 67
76 5 12 12 67 67 45
3 5 12 67 67 45 678 5 23 12 45 12 67
2.2.5. P322
Calculate the sum of the values in each row of a matrix of real numbers.
2.2.6. P347
Count the number of "Saddle Points" in the matrix. An element is called a Saddle Point if it is the largest
in its row and the smallest in its column.
2.2.7. P398
Rotate the values on the border of the matrix clockwise.
2.2.8. P423
Sort the values in the matrix in ascending order in a spiral pattern (spiral matrix).
2.2.9. P457
Check if the matrix is symmetric with respect to the main diagonal
2.2.10. P471
Calculate the product of two matrices.
6
CS10001 – Introduction to Programming L05 – 1D & 2D Array
3 Guidelines
Create your project with appropriate .h/.cpp files.
Write at least 1 function for each problem.
Write a main() function, which a menu that allows user to choose a problem to be executed If that user
chooses 0, exit the program
// Test case 1
// Input:
// Output:
…
// Test case 3
// Input:
// Output:
int main(){
//your code here
return 0;
}