0% found this document useful (0 votes)
9 views2 pages

EE121 Recitation 5 Fall2024

Uploaded by

seifsoumes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

EE121 Recitation 5 Fall2024

Uploaded by

seifsoumes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

L01 / EE121 Algorithmic/ FALL 2024 / R.

NAMANE

Recitation#5: Array Data Structure

Objectives:
Purpose of this recitation is to familiarize students with the use of elementary data structure Arrays.

Exercise 1: Sum of two 1D arrays


Let A1, A2, and A3 three 1D arrays with ten integer elements each. Write an algorithm that asks the user first to fill in
the two arrays A1 and A2. Thereafter, A1 and A2 are summed and the results are stored in A3. Elements of A3 are
finally printed out.

Exercise 2: Sum of all elements in a 1D array


Write an algorithm that accepts a 1D array of integers A[20] as input, and then finds and displays the sum of all its
elements.

Exercise 3: Dot product of two vectors


The dot product is one way of multiplying two vectors. The resultant of the dot product of vectors is a scalar quantity.
The dot product of two vectors A=[a1 , a2 , … , an] and B=[b1 , b2 , … , bn] is defined as:
𝐴𝐴. 𝐵𝐵 = ∑𝑛𝑛𝑖𝑖=1 𝑎𝑎𝑖𝑖 × 𝑏𝑏𝑖𝑖 =𝑎𝑎1 × 𝑏𝑏1 + 𝑎𝑎2 × 𝑏𝑏2 + ⋯ + 𝑎𝑎𝑛𝑛 × 𝑏𝑏𝑛𝑛
Where Σ denotes summation and n is the dimension of the vector space. For instance, in three-dimensional space, the
dot product of vectors A[1, 3, −5] and B[4, −2, −1] is: A.B = (1×4) + (3× −2) + (−5 × −1) = 4−6+5=3.
Write an algorithm that calculates the dot product of two vectors A and B of dimension 5.

Exercise 4: Average value of all elements in a 1D array


Write an algorithm that accepts real array A[50] as input, and then it performs the following:
1. Ask the user to fill the array with random real data.
2. Find the average of all elements in the array.
3. Search the number of elements above the average.
4. Search the number of elements below the average.

Exercise 5: Smallest and largest value in a 1D array


Write an algorithm that accepts a 1D array of integers A[25] as input, and then finds the smallest and the largest
elements of the array. The algorithm then swaps the smallest and the largest elements. In addition, elements of the
array are displayed before and after the swapping.

Exercise 6: Rotating elements of a 1D array


1. Write an algorithm that accepts a 1D array of integers A[5] as input, and then rotate left elements of A by one
position. That is if elements of A were: 103 12 45 7 23 then after rotation, elements of A become
12 45 7 23 103
2. Repeat question 1 to rotate right elements of A.

Exercise 7: An algorithm to inverse elements of a 1D array


Write an algorithm to inverse the elements of a 1D array.
Example: A 5 1 8 2 2 3 its inverse is: 3 2 2 8 1 5

Exercise 8: Sorting elements in a 1D array


Write an algorithm that sorts the ten integer elements of a given 1D array from the smallest to the largest one (in
increasing order). The sorted array will then be printed out.
There are several techniques for sorting a 1D array among which "Selection Sort algorithm". Selection sort is a simple
sorting algorithm. The main steps of that algorithm may be stated as follows:
1. Examine each element in the array to find the smallest.
2. Swap the element found in step 1 with the first element in the array.
3. Repeat steps 1 and 2, each time ignoring the smallest elements already swapped.
4. Stop when only one element is left in the array.

Exercise 9: An algorithm to check if a 1D array is sorted or not.


Write an algorithm that accepts a 1D array of integers A[20] as input, and then checks if that array is sorted in
increasing order or not.
L01 / EE121 Algorithmic/ FALL 2024 / R. NAMANE

Exercise 10: Inserting an element in a 1D array


Let A be a 1D array of size 60 and which is used to stores N integer elements (N<60). Write an algorithm that inserts a
value VAL given by the user at the kth position (specified by the user too) into the array A.
In the following is an example of the output of the algorithm:
Enter the number of elements you want to store in the array:
6
Enter your 6 elements:
12
23
34
45
56
67
Enter the position where you want to insert a new element:
3
Enter the value you want to insert into that position:
48
Final array after inserting the value is:
12
23
48
34
45
56
67

Exercise 11: Compute sums of diagonals of a matrix


Write an algorithm that calculates the sum of the values of the main diagonal of a square real matrix of size 5 x 5.
Given a 2D square matrix, write an algorithm that finds the sum of elements in Principal and Secondary diagonals. For
example, consider the following 4X4 input matrix:
A00 A01 A02 A03
A10 A11 A12 A13
A20 A21 A22 A23
A30 A31 A32 A33
The primary diagonal is formed by the elements A00, A11, A22, A33. The secondary diagonal is formed by the
elements A03, A12, A21, A30.
Example:
Input:
1 2 3 4
4 3 2 1
7 8 9 6
6 5 4 3
Output:
Principal Diagonal: 16
Secondary Diagonal: 20

Exercise 12: Grade management algorithm


Consider a 2D array representing the grades of 50 students in 4 different courses. The array must hold an extra column
that is used to store the average grade of each student in the four courses. An extra row must also be included in the
array in order to store the average grade of each course for all students.
Write the algorithm that asks the user first to fill the array with students’ grades, and then calculates all averages
(students’ and courses’ averages). The final results (students’ grades and all averages) should be printed out by your
algorithm.

You might also like