Sunday, 17 June 2012

Size of array A is 10 while size of B is 30. Scan 10 integers in both the array and concat array A to B. Then apply sorting algorithm according to the user choice


clear
echo enter the elements for array A..
for (( i=0; i<10; i++ ))
do
            echo enter `expr $i + 1` element.
            read a[$i]
done
echo enter the elements for array B..
for (( j=0; j<10; j++ ))
do
            echo enter `expr $j + 1` element.
            read b[$j]
done
for (( k=10; k<20; k++ ))
do
            z=`expr $k - 10`
            b[$k]=${a[$z]}
done
for (( i=0; i<20; i++ ))
do
            for (( j=`expr $i + 1`; j<20; j++ ))
            do
                        if [ ${b[$i]} -gt ${b[$j]} ]
                        then
                                    x=${b[$i]}
                                    b[$i]=${b[$j]}
                                    b[$j]=$x
                        fi
            done
done
clear
echo 1.Ascending  2.Descending
echo enter your choice...
read c
if [ $c = 1 ]
then
            echo the ascending order is....
            for (( i=0; i<20; i++ ))
            do
                        echo ${b[$i]}
            done
elif [ $c = 2 ]
then
            echo the descending order is...
            for (( i=20; i>0; i-- ))
            do
                        echo ${b[$i]}
            done
else
            echo wrong choice......
fi

No comments:

Post a Comment