Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

please also put commends, thank you! Problem 1 Write a program: 1. Generate a tw

ID: 3669232 • Letter: P

Question

please also put commends, thank you!

Problem 1 Write a program: 1. Generate a two dimensional test vector of integers by using random number generator, integers should be in [0,70], the vector size should be 4 rows and 5 columns.(4*5) 2. Let the user input number to be searched, use linear search function to search the number in the two dimensional vector 3. The function should accept one two dimensional vector of integers and a number to search for. If the number is found (if there are duplicate numbers, get the first one) print out the number and "Number found!", and output the rows and columns. Ifnot print out "Number isn't found!". 4. You may need iterator to search, keep doing steps above until get negative integers 5. Name your function "LinearSearch"

Explanation / Answer

1. Generating 2D Matrix:

#!/bin/bash
declare -a matrix
num_rows=4
num_columns=5

for ((i=1;i<=num_rows;i++)) do
for ((j=1;j<=num_columns;j++)) do
matrix[$i,$j]=$RANDOM
done
done

f1="%$((${#num_rows}+1))s"
f2=" %9s"

printf "$f1" ''
for ((i=1;i<=num_rows;i++)) do
printf "$f2" $i
done
echo

for ((j=1;j<=num_columns;j++)) do
printf "$f1" $j
for ((i=1;i<=num_rows;i++)) do
printf "$f2" ${matrix[$i,$j]}
done
echo
done

2. Linear search Function:

lsearch()
{
status=-1

for((i=0;i<count;i++))
do
Temp=$1

if [ $Temp -eq ${ARRAY[i]} ]
then

status=0
searches=$((i+1))
return

# return $((i+1))
# Bash function can return value between 0-255, That's why I assigned
# result to a global variable. This is one of the method to capture
# return value of a function.

fi

done
}
clear

echo "Enter Array Elements : "

read -a ARRAY

count=${#ARRAY[@]}

search=y

3.while [ "$search" == "y" -o "$search" == "Y" ]

do

echo -n "Enter element to be searched : "
read num
lsearch $num

if [ $status -eq 0 ]
then
echo "$num found after $searches searches"
else
echo "$num not found"
fi

echo -n "Do you want another search (y/n): "
read search

done