0% found this document useful (0 votes)
8 views

CSE220 Lab 01 - Arrays (Fall 2022)

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)
8 views

CSE220 Lab 01 - Arrays (Fall 2022)

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/ 4

Instructions for students:

1. Complete the following methods on Arrays,


2. You may use any language to complete the tasks.
3. All your methods must be written in one single .java or .py file. DO NOT CREATE
separate files for each task.
4. You need to submit one single file containing all the methods/functions. You will
get 1 week to complete your lab.
5. The submission format MUST be maintained. You need to copy paste all your
codes in ONE SINGLE .txt file and upload that. If format is not maintained, whole
lab submission will be canceled.
6. If you are using JAVA, you must include the main method as well which should
test your other methods and print the outputs according to the tasks.
7. If you are using PYTHON, make sure your code has the methods invoked and
proper printing statements according to the tasks.
8. The google form link for this lab will be provided in BUX under LAB 1-Arrays
subsection under the CSE220 lab tab 2 days before the deadline.

Linear Arrays
NOTE: Each method carries 5 marks

1. Shift Left k Cells


Consider an array named source. Write a method/function named shiftLeft( source, k)
that shifts all the elements of the source array to the left by 'k' positions. You must
execute the method by passing an array and number of cells to be shifted. After calling
the method, print the array to show whether the elements have been shifted properly.
Example:
source=[10,20,30,40,50,60]
shiftLeft(source,3)
After calling shiftLeft(source,3), printing the array should give the output as:
[ 40, 50, 60, 0, 0, 0 ]
2. Rotate Left k cells
Consider an array named source. Write a method/function named rotateLeft( source, k)
that rotates all the elements of the source array to the left by 'k' positions. You must
execute the method by passing an array and number of cells to be shifted. After calling
the method, print the array to show whether the elements have been shifted properly.
Example:
source=[10,20,30,40,50,60]
rotateLeft(source,3)
After calling rotateLeft(source,3), printing the array should give the output as:
[ 40, 50, 60, 10, 20, 30]

3. Shift Right k Cells


Consider an array named source. Write a method/function named shifRight( source, k)
that shifts all the elements of the source array to the right by 'k' positions. You must
execute the method by passing an array and number of cells to be shifted. After calling
the method, print the array to show whether the elements have been shifted properly.
Example:
source=[10,20,30,40,50,60]
shiftRight(source,3)
After calling shiftRight(source,3), printing the array should give the output as:
[ 0,0,0,10,20,30 ]
4. Rotate Right k cells
Consider an array named source. Write a method/function named rotateRight( source,
k) that rotates all the elements of the source array to the right by 'k' positions. You must
execute the method by passing an array and number of cells to be shifted. After calling
the method, print the array to show whether the elements have been shifted properly.
Example:
source=[10,20,30,40,50,60]
rotateRight(source,3)
After calling rotateRight(source,3), printing the array should give the output as:
[ 40, 50, 60, 10, 20, 30]

5. Remove an element from an array


Consider an array named source. Write a method/function named remove( source,
size, idx) that removes the element in index idx of the source array. You must execute
the method by passing an array, its size and the idx( that is the index of the element to
be removed). After calling the method, print the array to show whether the element of
that particular index has been removed properly.
Example:
source=[10,20,30,40,50,0,0]
remove(source,5,2)
After calling remove(source,5,2) , printing the array should give the output as:
[ 10,20,40,50,0,0,0]
6. Remove all occurrences of a particular element from an array
Consider an array named source. Write a method/function named removeAll( source,
size, element) that removes all the occurrences of the given element in the source
array. You must execute the method by passing an array, its size and the element to be
removed. After calling the method, print the array to show whether all the occurrences
of the element have been removed properly.
Example:
source=[10,2,30,2,50,2,2,0,0]
removeAll(source,7,2)
After calling removeAll(source,7,2), all the occurrences of 2 must be removed. Printing
the array afterwards should give the output as:
[ 10,30,50,0,0,0,0,0,0]

7. Splitting an Array

Suppose the elements of an array A containing positive integers, denote the weights in
kilograms. And we have a beam balance. We want to put the weights on both pans of
the balance in such a way that for some index 0 < i < A.length - 1, all values starting
from A[0], A[1], upto A[ i - 1], should be on the left pan. And all values starting from A[ i ]
upto A[ A.length - 1] should be on the right pan and the left and right pan should be
balanced. If such an i exists, return true . Else, return false.

Input: [1, 1, 1, 2, 1] Output : true

Explanation: (summation of [1, 1, 1] = summation of [2,1])

Input: [2, 1, 1, 2, 1] Output: false

Input: [10, 3, 1, 2, 10] Output: true

Explanation: (summation of [10, 3] = summation of [1,2,10]))

8. Array series

Write a method that takes an integer value n as a parameter. Inside the method, you
should create an array of length n squared (n*n) and fill the array with the following
pattern. Return the array at the end and print it.

If,

n=2: { 0,1, 2,1 } (spaces have been added to show two distinct groups).

n=3 : { 0, 0, 1, 0, 2, 1, 3, 2, 1 } ((spaces have been added to show three distinct

groups).
n=4 : {0, 0, 0, 1, 0, 0, 2, 1, 0, 3, 2, 1, 4, 3, 2, 1} (spaces have been added to show
four distinct groups).

9. Max Bunch Count

A "bunch" in an array is a consecutive chain of two or more adjacent elements of the


same value. Write a method that returns the number of elements in the largest bunch
found in the given array.

Input: [1, 2, 2, 3, 4, 4, 4] Output: 3

Explanation: There are two bunches here {2,2} and {4,4,4}. The largest bunch is {4,4,4}
containing 3 elements so 3 is returned.

Input: [1,1,2, 2, 1, 1,1,1] Output:4

Explanation: There are three bunches here {1,1} and {2,2} and {1,1,1,1}. The largest
bunch is {1,1,1,1} containing 4 elements so 4 is returned.

10. Repetition

Write a method that takes in an array as a parameter and counts the repetition of each
element. That is, if an element has appeared in the array more than once, then its
‘repetition’ is its number of occurrences. The method returns true if there are at least
two elements with the same number of ‘repetition’. Otherwise, return false.

Input: {4,5,6,6,4,3,6,4} Output: True

Explanation: Two numbers repeat in this array: 4 and 6. 4 has a repetition of 3, 6 has a
repetition of 3. Since two numbers have the same repetition output is True.

Input: {3,4,6,3,4,7,4,6,8,6,6} Output: False

Explanation: Three numbers repeat in this array:3,4 and 6 .3 has a repetition of 2, 4 has
a repetition of 3, 6 has a repetition of 4. Since no two numbers have the same repetition
output is False.

You might also like