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.
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...
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.
3.1 Write code that creates an ArrayList object named list and fills list with these numbers...
3.1 Write code that creates an ArrayList object named list and fills list with these numbers (using one or a pair of for or while loops): 0 1 2 3 4 0 1 2 3 4 3.2 Consider the ArrayList object named list containing these Integers: list = { 1, 2, 3, 4, 5, 4, 3, 2, 1, 0 } What are the contents of list after this loop completes? for (int i = 1; i < 10; ++i) {...
First, create a project that creates an ordered linked list class (use the code provided, make...
First, create a project that creates an ordered linked list class (use the code provided, make sure to update the insert function so that it maintains an ordered list). To this class, add a function that prints the list in REVERSE order. Making use of recursion is the easiest and best way to do this, but certainly not the only way. Submit a .zip of your entire project.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT