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

Unique Number Array and Matrix Operations

The document outlines a practice lab with five programming tasks involving arrays and matrices. Tasks include storing unique numbers in an array, rotating an array, calculating diagonal sums of a square matrix, checking for matrix symmetry, and managing student scores with a passing criteria. Each task provides examples and expected outputs for clarity.

Uploaded by

Khadija R
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)
7 views2 pages

Unique Number Array and Matrix Operations

The document outlines a practice lab with five programming tasks involving arrays and matrices. Tasks include storing unique numbers in an array, rotating an array, calculating diagonal sums of a square matrix, checking for matrix symmetry, and managing student scores with a passing criteria. Each task provides examples and expected outputs for clarity.

Uploaded by

Khadija R
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

PRACTICE LAB 3

SEF21 (PF)
18-04-2022

Q1: Write a program that declares an array of size 10. Now ask user to enter positive numbers
and store only the unique numbers in the array. Stop taking input when the array fills. At the
end, display the array and also how many numbers are entered by user.
Example:
Enter number: 5

Enter number: 5

Enter number: 2

Enter number: 7

Enter number: 8

Enter number: 7

Enter number: 5

Enter number: 3

Enter number: 1

Enter number: 2

Enter number: 12

Enter number: 4

Enter number: 8

Enter number: 34

Enter number: 56

Array: { 5, 2, 7, 8, 3, 1, 12, 4, 34, 56}

Number of entries: 15
Q2: Make an array of size 10. Input the number of times array is to be left rotated. Display the
original array and rotated array.

Example:
Original Array: 3 4 6 8 2 7

D=3:

Rotated array: 8 2 7 3 4 6

Q3: Write user defined functions for square matrix to calculate

1. Left diagonal sum


2. Right diagonal sum

Example:
Matrix:

3 4 7 6

2 8 2 9

1 6 2 3

5 1 7 3

Left Diagonal Sum: 3+8+2+3=16

Right Diagonal Sum: 6+2+6+5=19

Q4: Write a program to check whether a matrix is symmetric or not.

Q5: Take an array of 30 integers named Score and a second array named Passing. Assume
elements array Score contain scores for 30 students.

1. Write a code segment that initializes all students in Passing to false (zero).
2. Write a code segment that sets the students in Passing to true (one) wherever the
parallel value of Score is greater than 60.
3. Write a code segment that prints the index of the of items in Passing that are true (one).

4. Write a code segment that displays the count of the students who passed.

Common questions

Powered by AI

To calculate the left and right diagonal sums of a square matrix, iterate over the elements, using a single loop where the index ranges from 0 to matrix size - 1. For the left diagonal, sum the elements where the row index equals the column index. For the right diagonal, sum the elements where the row index plus the column index equals the size of the matrix minus 1. This approach efficiently computes both sums with a single pass over the matrix elements .

To store only unique positive numbers in an array of size 10, start by initializing an array of this size and a counter to track the number of attempts. For each user input, check if the input is a unique positive number not already in the array. If it is, add the number to the array; if not, prompt for another input without increasing the unique count. Continue this process until the array is filled with 10 unique numbers, and ensure that every user attempt is counted as a 'try,' regardless of whether the number was successfully entered into the array. Display the filled array and the count of attempts when the array is full .

To left rotate an array of integers by a given number of positions, you can use an algorithm that first copies the elements to be rotated (from the start of the array) into a temporary array, then shifts the remaining elements in the array to the left, and finally inserts the elements from the temporary array to the end. The efficiency is largely influenced by the number of rotations relative to the array size. If the number of rotations is significantly large, equivalent to or exceeding the size of the array, it's optimal to calculate rotations modulus the array length, reducing redundant full array cycles .

Initializing all elements of an array to false serves as a base state indicating no action or event has occurred, which aids in subsequent parallel data operations by providing a uniform start. For example, if using such an array to track conditions met in parallel data, setting an element to true marks the condition met for that index. This clear distinction between default (false) and altered (true) states simplifies processing logic and ensures that only explicitly processed data is marked as such, reducing chances of erroneously considering unprocessed data .

A symmetric matrix is defined as one that is equal to its transpose, such that for every element A[i][j], A[i][j] must be equal to A[j][i]. To simplify the check for symmetry, iterate only over the upper triangle or the lower triangle of the matrix (including the diagonal) and compare the elements with their corresponding positions across the diagonal. This reduces redundant checks and improves efficiency. The presence of any non-matching elements immediately renders the matrix non-symmetric .

Frequent left rotations on a large array increase the time complexity rapidly as each rotation requires shifting all array elements, resulting in O(n) time complexity per rotation. To optimize, rather than performing multiple left single rotations, calculate the effective rotations needed (total rotations modulo array size), which reduces unnecessary complete shifts. Implementing an efficient algorithm like reversal or block swapping algorithm, where segments are shifted, flipped, and rotated in O(n) time, allows for efficient element repositioning without direct cyclic shifts .

To efficiently determine and display the students surpassing a passing score, initialize a 'passing' array to false. Traverse the 'scores' array, and for each score greater than the passing threshold (e.g., 60), set the corresponding index in the 'passing' array to true. Once traversal is complete, iterate over the 'passing' array and collect indices where the value is true. Print these indices to display the students who have surpassed the score. This modular approach ensures separation of state tracking and output logic, enhancing clarity and efficiency .

Modular arithmetic simplifies array rotation operations by reducing the number of positions to rotate through to a manageable size (specifically, the rotation count modulo the array length), which prevents unnecessary and repeated full iterations through the array. This approach avoids overflow errors often caused by large loop counts and provides a direct calculation from the intended rotations to the minimal effective rotations needed, ensuring both accuracy in index computation and resource efficiency during the rotation process .

Segmenting code into functions for calculating diagonal sums in a square matrix provides several benefits including enhanced readability and maintenance, modularity, and reuse. Each function encapsulates the logic to compute either the left or right diagonal sum, allowing for isolated modifications and tests. This separation of concerns means the main logic can incorporate these functions easily, improving code clarity. Additionally, the modular code can be reused for other matrix operations where diagonal sums are relevant without the need to extract or replicate logic, leading to efficient code reuse and reduced errors .

To ensure an array stores only unique elements, each input requires a storage check against all existing elements. For an array of size n, this requires n(n+1)/2 checks in the worst case, where each new element is compared against all previously stored elements. Efficient storage checks, such as using hash tables, can reduce repetitive linear checks by allowing constant time complexity checks, especially helpful as the array approaches capacity. Thus, leveraging data structures like sets or hash maps coupled with active counts of current entries optimizes addition and verification of uniqueness .

You might also like