Lab 4

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Lab 4.

Array Fundamentals of Programming CSC10012

Lab 4

Array
In this lab, we will learn about arrays, a fundamental data structure in C/C++ that allows us to
store multiple values of the same type in a single variable.

Instructions
1. Declaring and Initializing Arrays
An array is a collection of elements, all of the same type, stored in contiguous memory locations.
The syntax for declaring an array includes specifying the type, the array name, and the size.

• Declaration: Specifies the type and size of the array.

dataType arrayName[size];

For example:
1 int arr [5];

• Initialization: Arrays can be initialized at the time of declaration.


For example:
1 int arr [5] = {1 , 2 , 3 , 4 , 5}; // Arr has 5 elements : 1, 2, 3, 4, 5
2 int arr [] = {1 , 2 , 3 , 4 , 5}; // Arr has 5 elements : 1, 2, 3, 4, 5
3 int arr [5] = {1 , 2}; // Arr has 5 elements : 1, 2, 0, 0, 0
4 int arr [5] = {0}; // Arr has 5 elements : 0, 0, 0, 0, 0

2. Accessing and Modifying Array Elements


Array elements can be accessed and modified using indices, starting from 0 for the first element.

• Access an element by specifying the index in square brackets.


1 int x = arr [0]; // Access the first element (1)

• Modify an element by assigning a new value to a specific index.


1 arr [0] = 10; // Update the first element to 10

University of Science Faculty of Information Technology Page 1


Lab 4. Array Fundamentals of Programming CSC10012

3. Looping through Arrays


Loops can be used to iterate over arrays to perform operations on each element.

• Regular for-loop:
1 for ( int i = 0; i < 5; i ++)
2 {
3 cout << arr [ i ] << " " ;
4 }

• Range-based for-loop (C++11 and later):


1 for ( int value : arr )
2 {
3 cout << value << " " ;
4 }

4. Arrays as Function Parameters


Arrays can be passed to functions as parameters by specifying the array type and size (if known).
Arrays are passed by reference only (note that the symbol & is not used as a regular parameter),
meaning changes within the function affect the original array.
For example:

• Print Array Function:


1 void printArray ( int arr [] , int size )
2 {
3 for ( int i = 0; i < size ; i ++)
4 cout << arr [ i ] << " " ;
5

6 cout << endl ;


7 }

• Input Array Function:


1 void inputArray ( int arr [] , int & size )
2 {
3 cin >> size ;
4

5 for ( int i = 0; i < size ; i ++)


6 cin >> arr [ i ];
7 }

University of Science Faculty of Information Technology Page 2


Lab 4. Array Fundamentals of Programming CSC10012

5. Multidimensional Arrays
C/C++ allows Multidimensional Arrays, commonly used for 2D Arrays (matrices) or 3D Arrays.
Bellow are some examples for 2D Array:

• Declaration and Initialization:


1 int matrix [2][3] = {{1 , 2 , 3} , {4 , 5 , 6}};

Here, matrix is a 2x3 array, with 2 rows and 3 columns.

• Accessing Elements in a 2D Array:


1 int x = matrix [0][1];
2 // Access element in the first row , second column ( value : 2)

• Modifying Elements in a 2D Array:


1 matrix [1][2] = 10;
2 // Updates the element in the second row , third column to 10

• Loop through a 2D Array:


1 int rows , cols ;
2

3 cin >> rows >> cols ;


4

5 for ( int i = 0; i < rows ; i ++)


6 for ( int j = 0; j < rows ; j ++)
7 cin >> matrix [ i ][ j ];

• Passing Multidimensional Array: When passing a multidimensional array to a function,


the sizes of all dimensions must be specified (maybe except the first dimension).
1 void printMatrix ( int matrix [][3] , int rows )
2 {
3 for ( int i = 0; i < rows ; i ++)
4 {
5 for ( int j = 0; j < 3; j ++)
6 {
7 cout << matrix [ i ][ j ] << " " ;
8 }
9 cout << endl ;
10 }
11 }

University of Science Faculty of Information Technology Page 3


Lab 4. Array Fundamentals of Programming CSC10012

Exercises
Exercise 1. Find the smallest even and largest odd number
Write a program to find the smallest even and largest odd number in an array.

Input:

• The number of elements in array - n.

• The array of integers.

Output:

• The smallest even and the largest odd number.

• If there is no even number, then print No even number.

• If there is no odd number, then print No odd number.

Example:

Input Output
5 2
1 2 3 4 5 5

Exercise 2. Find the k th largest element


Write a program to find the k th largest element in an array.

Input:

• The number of elements in array - n; and the integer k.

• The array of integers.

Output:

• The k th largest element (print Not found if there is no valid number).

Example:

Input Output
5 2 4
1 5 3 4 2

University of Science Faculty of Information Technology Page 4


Lab 4. Array Fundamentals of Programming CSC10012

Exercise 3. Sort an array in ascending order


Write a program to sort an array in ascending order.

Input:

• The number of elements in array - n.

• The array of integers.

Output:

• The sorted array.

Example:

Input Output
5 1 2 3 4 5
3 1 2 5 4

Exercise 4. Delete number


Write a program to delete a number from an array.

Input:

• The number of elements in array - n; and the number k that need to be deleted.

• The array of integers.

Output:

• The array without the value k.

• Notes: If the array has no elements left after deletion, output Empty.

Example:

Input Output
11 4 1 2 3 5
4 4 1 4 2 4 3 4 5 4 4

University of Science Faculty of Information Technology Page 5


Lab 4. Array Fundamentals of Programming CSC10012

Exercise 5. Reverse an array


Write a program to reverse an array.

Input:

• The number of elements in array - n.

• The array of integers.

Output:

• The reversed array.

Example:

Input Output
5 5 4 3 2 1
1 2 3 4 5

Exercise 6. Check array


Write a program to check if an array is not increasing, or not decreasing, or just a regular array.

Input:

• The number of elements in array - n.

• The array of integers.

Output:

• Not increasing if the array is not an increasing array.

• Not decreasing if the array is not a decreasing array.

• Else print Regular.

Example:

Input Output
4 Not decreasing
1 2 3 4

University of Science Faculty of Information Technology Page 6


Lab 4. Array Fundamentals of Programming CSC10012

Exercise 7. Find the longest non-decreasing sub-array


Write a program to find the longest non-decreasing sub-array in an array.

Input:

• The number of elements in array - n.

• The array of integers.

Output:

• The longest non-decreasing sub-array.

• If there are many results with the same length, print any.

Example:

Input Output
6 3 4 8
5 3 4 8 6 7

Exercise 8. Find the maximum sum sub-array


Write a program to find the sub-array with the largest sum.

Input:

• The number of elements in array - n.

• The array of integers.

Output:

• The maximum sum sub-array.

• If there are many results, print any.

Example:

Input Output
9 4 -1 2 1
-2 1 -3 4 -1 2 1 -5 4

University of Science Faculty of Information Technology Page 7


Lab 4. Array Fundamentals of Programming CSC10012

Exercise 9. Histogram of an array


Write a program to generate a histogram representation of an array.

Input:

• The number of elements in array - n.

• The array of integers.

Output (Sorted in ascending order):

• The number: its number of occurrences.

Example:

Input Output
11 1: 5
3 2 1 1 1 2 1 1 3 2 2 2: 4
3: 2

Exercise 10. Merge two arrays


Write a program to merge two arrays that already sorted in ascending order to an ascending array.

Input:

• The number of elements in the first array - n, and the second array - m.

• The first array of integers.

• The second array of integers.

Output:

• The merged ascending array.

Example:

Input Output
2 2 1 2 3 4
1 3
2 4

University of Science Faculty of Information Technology Page 8


Lab 4. Array Fundamentals of Programming CSC10012

Exercise 11. Find smallest and largest prime in a matrix


Write a program to find the smallest and largest prime numbers in a matrix.

Input:

• The number of rows n and the number of columns m.

• The matrix where each row is on a line and each element is separated by a space.

Output:

• The smallest and largest prime in the matrix.

Example:

Input Output
2 2 2 5
2 4
5 6

Exercise 12. Sort a matrix in ascending order


Write a program to sort all elements of a matrix in ascending order.

Input:

• The number of rows n and the number of columns m.

• The matrix where each row is on a line and each element is separated by a space.

Output:

• The sorted matrix.

Example:

Input Output
2 2 1 2
3 1 3 4
4 2

University of Science Faculty of Information Technology Page 9


Lab 4. Array Fundamentals of Programming CSC10012

Exercise 13. Delete a row from a matrix


Write a program to delete a specific row from a matrix.

Input:

• The number of rows n, the number of columns m and the number k.

• The matrix where each row is on a line and each element is separated by a space.

Output:

• The matrix without the row k.

• Notes: If the matrix has no elements left after deletion, output Empty.

Example:

Input Output
2 2 2 1 2
1 2
3 4

Exercise 14. Delete a column from a matrix


Write a program to delete a specific column from a matrix.

Input:

• The number of rows n, the number of columns m and the number k.

• The matrix where each row is on a line and each element is separated by a space.

Output:

• The matrix without the column k.

• Notes: If the matrix has no elements left after deletion, output Empty.

Example:

Input Output
2 2 2 1
1 2 3
3 4

University of Science Faculty of Information Technology Page 10


Lab 4. Array Fundamentals of Programming CSC10012

Exercise 15. Rotate a matrix


Write a program to rotate a matrix 90 degrees clockwise.

Input:

• The number of rows n, the number of columns m.

• The matrix where each row is on a line and each element is separated by a space.

Output:

• The rotated matrix.

Example:

Input Output
2 2 3 1
1 2 4 2
3 4

The end.

University of Science Faculty of Information Technology Page 11

You might also like