Practice questions on Array

Level 1

1.
Take 10 integer inputs from user and store them in an array and print them on screen.
```i = 10
a = []
while i>0
puts "Enter number"
num = gets.chomp.to_i
a.push(num)
i = i-1
end
puts "#{a}"
```

2.
Take 10 integer inputs from user and store them in an array. Again ask user to give a number. Now, tell user whether that number is present in array or not.
( Iterate over array using while loop ).
```i = 10
a = []
while i>0
puts "Enter number"
num = gets.chomp.to_i
a.push(num)
i = i-1
end
puts "Enter a number"
n = gets.chomp.to_i
i = 9
count = 0
while i>=0
if n == a[i]
puts "Yes"
count = count + 1
end
i = i-1
end
if count == 0
puts "No"
end
```

3.
Take 20 integer inputs from user and print the following:
number of positive numbers
number of negative numbers
number of odd numbers
number of even numbers
number of 0s.
```i = 20
a = []
while i>0
puts "Enter number"
num = gets.chomp.to_i
a.push(num)
i = i-1
end
odd = 0
even = 0
zero = 0
positive = 0
negative = 0
i = 19
while i>=0
if a[i] == 0
zero = zero+1
elsif a[i]>0
positive = positive + 1
if a[i]%2 == 0
even = even + 1
else
odd = odd + 1
end
else
negative = negative + 1
if a[i]%2 == 0
even = even + 1
else
odd = odd + 1
end
end
i = i-1
end
puts "EVEN : #{even} ODD : "#{odd} ZERO : #{zero} POSITIVE : #{positive} NEGATIVE : #{negative}"
```

4.
Take 10 integer inputs from user and store them in an array. Now, copy all the elements in another array but in reverse order.

5.
Write a program to find the sum of all elements of an array.

6.
Write a program to find the product of all elements of an array.

7.
Initialize and print each element in new line of a array inside array.
```a=[[1,2,3],[4,5,6]]
i = 0
while i<a.length
j = 0
while j < a.length
puts a[i][j]
j = j+1
end
i = i+1
end
```

8.
Find the largest and smallest elements of an array.
```a = [2,312,123,3,12,23,12,12]
largest = a[0]
i = 0
while i<a.length
if a[i]>largest
largest = a[i]
end
i = i+1
end
puts largest
#similarly find smallest
```

9.
Write a program to print sum, average of all numbers, smallest and largest element of an array.

10.
Write a program to check if elements of a array are same or not it read from front or back. E.g.-
 2 3 15 15 3 2
```# You can take any array
a = [1,2,3,3,2,1]
i = 0
mid = a.length/2
same = true
while i<mid
if a[i] != a[a.length-i-1]
puts "No"
same = false
break
end
i = i+1
end
if same == true
puts "Yes"
end
```

11.
Make a array by taking 10 input from user. Now delete all repeated elements of the array.
E.g.-
INPUT : [1,2,3,2,1,3,12,12,32]
OUTPUT : [1,2,3,12,32]
```a = [1,2,3,2,1,3,12,12,32]
i = 0
while i < a.length
j = i+1
while j < a.length
if a[i] == a[j]
a.delete_at(j)
end
j=j+1
end
i = i+1
end
puts a
```

12.
Take an array of 10 elements. Split it into middle and store the elements in two dfferent arrays. E.g.-
INITIAL array :
 58 24 13 15 63 9 8 81 1 78

After spliting :
 58 24 13 15 63
 9 8 81 1 78
Use slice

13.
Ask user to give integer inputs to make a array. Store only even values given and print the array.
```#Hint
if num%2 == 0
a.push(num)
end
```

Level 2

1.
Take a slit of length n where all the numbers are non-negative and unique. Find the element in the array possessing the highest value. Split the element into two parts where first part contains the next highest value in the array and second part hold the required additive entity to get the highest value. Print the array where the highest value get splitted into those two parts.
Sample input: 4 8 6 3 2
Sample output: 4 6 2 6 3 2
```a = [4,8,6,3,2]
i = 0
largest = a[0]
while i<a.length
if largest < a[i]
largest = a[i]
largest_index = i
end
i = i+1
end
puts largest_index
sec_largest =  a[0]
i = 0
while i<a.length
if largest != a[i] and sec_largest < a[i]
sec_largest = a[i]
end
i = i+1
end
diff = largest - sec_largest
puts "#{a[0..largest_index-1]+[sec_largest,diff]+a[largest_index+1..a.length-1]}"
```

2.
Write a program to shift every element of an array to circularly right. E.g.-
INPUT : 1 2 3 4 5
OUTPUT : 5 1 2 3 4
```a = [1,2,3,4,5]
b = [a[a.length-1]]+a[0..a.length-2]
puts "#{b}"
a = b
puts "#{a}"
```