You are on page 1of 10

4A_19F0217_LAB5

Arrays

19F0217
M SHAHID IMRA
BSCCS
Question 1:

Code:
#!/bin/bash

echo ""
echo " @@--------------------@@"
echo " %% Question 1 %%"
echo " @@--------------------@@"
echo ""

find_Repetition()
{
greatest_count=0
for((loop1=0;loop1<${#arr1[@]};loop1++))
do
number=${arr1[$loop1]}
count=0
for((loop2=0;loop2<${#arr1[@]};loop2++))
do
if(($number==${arr1[$loop2]}))
then
((count++))
fi
done
repetition_stored_arr[$number]=$count
if(($count > $greatest_count))
then
greatest_count=$count
fi
done
}

print_greater_Repeater()
{
for((loop=0;loop<${#repetition_stored_arr[@]};loop++))
do
if(($greatest_count==${repetition_stored_arr[$loop]}))
then
echo " The Number $loop Is greater_repeater having $greatest_count
repetitions!"
echo ""
fi
done
}

arr1=(0 1 0 1 2 3 1 2 3 0)
declare -A repetition_stored_arr
echo " Array is :"
for((loop=0;loop<${#arr1[@]};loop++))
do
echo " ${arr1[loop]}, "
done
echo ""
find_Repetition
print_greater_Repeater
echo " ThankYou :)"
echo ""

Output:
Question 2:

Code:
#!/bin/bash

echo ""
echo " @@--------------------@@"
echo " %% Question 2 %%"
echo " @@--------------------@@"
echo ""

find_Greatest()
{
greatest_digit=0
for((loop1=0;loop1<${#arr1[@]};loop1++))
do
if(($greatest_digit<${arr1[$loop1]}))
then
greatest_digit=${arr1[$loop1]}
fi
done
}

arr1=(0 1 2 3 4 4 3 2 9 10)
echo " Array is :"
for((loop=0;loop<${#arr1[@]};loop++))
do
echo " ${arr1[loop]}, "
done
echo ""
find_Greatest
echo " The Greatest Number in Array Is $greatest_digit!"
echo ""
echo " ThankYou :)"
echo ""
Output:
Question 3:

Code:
#!/bin/bash

echo ""
echo " @@--------------------@@"
echo " %% Question 3 %%"
echo " @@--------------------@@"
echo ""

Seperate_the_digits()
{
for((loop1=0;loop1<${#arr1[@]};loop1++))
do
for((loop2=0;loop2<$((${#arr1[@]}-1));loop2++))
do
if(( ${arr1[$loop2]} >= ${arr1[$(($loop2+1))]}))
then
temp=${arr1[$(($loop2+1))]}
arr1[$(($loop2+1))]=${arr1[$loop2]}
arr1[$loop2]=$temp
fi
done
done
}

arr1=(0 1 1 1 1 0 0 0 1 0)
echo " Before Seperating Array is :"
for((loop=0;loop<${#arr1[@]};loop++))
do
echo " ${arr1[loop]}, "
done
echo ""
Seperate_the_digits
echo " After Seperating Array is :"
for((loop=0;loop<${#arr1[@]};loop++))
do
echo " ${arr1[loop]}, "
done
echo ""
echo " ThankYou :)"
echo ""
Output:
Question 4:

Code:
#!/bin/bash

echo ""
echo " @@--------------------@@"
echo " %% Question 4 %%"
echo " @@--------------------@@"
echo ""

find_Repetition()
{
for((loop1=0;loop1<${#arr1[@]};loop1++))
do
number=${arr1[$loop1]}
for((loop2=0;loop2<${#arr1[@]};loop2++))
do
if(($number==${arr1[$loop2]}))
then
repetition_stored_arr[$loop1]=$number
fi
done
done
}

print_first_Repeater()
{
if((${#repetition_stored_arr[@]} > 0))
then
echo " The Number ${repetition_stored_arr[0]} Is first_repeater!"
echo ""
else
echo " All the Numbers are different!"
echo ""
fi
}

arr1=(0 1 2 3 4 4 3 2 1 0)
declare -A repetition_stored_arr
echo " Array is :"
for((loop=0;loop<${#arr1[@]};loop++))
do
echo " ${arr1[loop]}, "
done
echo ""
find_Repetition
print_first_Repeater
echo " ThankYou :)"
echo ""

Output:

You might also like