Question

In: Computer Science

Use BASH to make a code that takes any list of numbers and calculate the Median...

Use BASH to make a code that takes any list of numbers and calculate the Median and Mode.

Solutions

Expert Solution


# Total numbers 
n=7 
   
# copying the value of n 
m=$n 
   
# initialized sum by 0 
sum=0 
   
# array initialized with 
# some numbers 
array=(3 4 1 8 9 10 2) 
#sorted array for median
sortedarray=( $( printf "%s\n" "${array[@]}" | sort -n ) )
   
# loop until n is greater 
# than 0 
while [ $n -gt 0 ] 
do
    # copy element in a  
    # temp variable 
    num=${sortedarray[`expr $n - 1`]}     
   
    # add them to sum 
    sum=`expr $sum + $num`
   
    # decrement count of n 
    n=`expr $n - 1`
done 
   
# displaying the average  
# bc is bash calculator 
avg=`echo "$sum / $m" | bc -l`
#k storing the modulus
k=$(( $m % 2 )) 
if [ $k -eq 0 ] #if m is odd then median will be the middle index value of the sorted array
then
    i=$(( $m / 2 )) #index
    p1=$(( $i)) #first index
    p2=$(( $i - 1)) #second index
    s=$(( ${sortedarray[$p1]}+ ${sortedarray[$p2]} )) #sum of this 2
    med=$(( s/2 )) #median
    
else #if even then median will be sum of middle 2 values divided by 2 of the sorted array
   i=$(( $m / 2 )) #index
   med=${sortedarray[$i]} #median
fi

echo "The array is : ${array[@]}"

printf 'Average %0.3f \n' "$avg"

printf 'Median %0.3f' "$med"

Tried with n=8


Related Solutions

Bash script: Create a bash script that takes numbers as parameters, calculates sum and prints the...
Bash script: Create a bash script that takes numbers as parameters, calculates sum and prints the result. If no parameters passed, prompt a user (only one time) to enter numbers to sum and print the result. Submit your program as LastName_FirstName.sh file.
I know this code takes in a set of numbers into a doubly linked list and...
I know this code takes in a set of numbers into a doubly linked list and sorts it using insertion sort. Could you explain exactly how this code is working? main.c Source Code: #include #include #include "node.h" int main() { struct mynode *head=NULL; int value; printf("Give first value: \n"); scanf("%d",&value); printf("Give next values, and input 0 to end list: \n"); do{ if(value>0){    head = pushNode(head, value);    scanf("%d",&value); } }while (value>0); printf("Before insertion sort: "); printlist(head); head=insertsort(head); printf("After insertion...
Create a bash script that takes numbers as parameters, calculates sum and prints the result. If...
Create a bash script that takes numbers as parameters, calculates sum and prints the result. If no parameters passed, prompt a user (only one time) to enter numbers to sum and print the result.
Develop a C-code to calculate average, median and standard deviation, using 5 numbers as data you...
Develop a C-code to calculate average, median and standard deviation, using 5 numbers as data you input from the keyboard from calculations and compare program output with the hand calculations shown with formula for average, median & standard deviation.. Use suggested approach below for developing the code (other approaches are also possible): In main     define an array of size = 5, Read in 5 decimal numbers and print the chosen numbers for input; Store input as a array and work...
Pick any 5 different two digit numbers and calculate mean, median, standard deviation, range, 25th and...
Pick any 5 different two digit numbers and calculate mean, median, standard deviation, range, 25th and outlier if any.
Does it make sense to calculate the median for nominal variables?
Does it make sense to calculate the median for nominal variables?
In this assignment, you will calculate and print a list of Fibonacci Numbers . Fibonacci numbers...
In this assignment, you will calculate and print a list of Fibonacci Numbers . Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and characterized by the fact that every number after the first two is the sum of the two preceding ones: The sequence Fn of Fibonacci numbers is defined by the recurrence relation:  which says any Nth Fibonacci number is the sum of the (N-1) and (N-2)th Fibonacci numbers. Instructions Your task will be...
In Python syntax, create a list of 10 numbers (any numbers). Create your list 3 times,...
In Python syntax, create a list of 10 numbers (any numbers). Create your list 3 times, each time using a different syntax to define the list. Write a while loop that prints the numbers from 1 to 10. Convert your previous loop into a for loop. Rewrite your for loop to only have one number in the range. Write a for loop to print out each item of your list of 10 numbers, all on the same line, separated by...
In Python syntax, create a list of 10 numbers (any numbers). Create your list 3 times,...
In Python syntax, create a list of 10 numbers (any numbers). Create your list 3 times, each time using a different syntax to define the list. Write a while loop that prints the numbers from 1 to 10. Convert your previous loop into a for loop. Rewrite your for loop to only have one number in the range. Write a for loop to print out each item of your list of 10 numbers, all on the same line, separated by...
Make a list of what you believe to be rights belonging to all humans. Use any...
Make a list of what you believe to be rights belonging to all humans. Use any of the theories of rights to support your assertion that these rights belong to all.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT