Question

In: Computer Science

Code the following in bash and run. Please show full code and don’t forget to comment...

Code the following in bash and run. Please show full code and don’t forget to comment your code.

Make a code that takes any list of numbers and calculates and displays the mean, median and mode.

Solutions

Expert Solution

Mean is also called average. The program to calculate mean is:

# Total numbers

n=5

   # copying the value of n

m=$n

   # initialized sum by 0

sum=0

   # array initialized with

# some numbers

array=(1 2 3 4 5)

   # loop until n is greater

# than 0

while [ $n -gt 0 ]

do

    # copy element in a

    # temp variable

    num=${array[`expr $n - 1`]}    

  

    # add them to sum

    sum=`expr $sum + $num`

  

    # decrement count of n

    n=`expr $n - 1`

done

  

# displaying the average

# by piping with bc command

# bc is bash calculator

# command

avg=`echo "$sum / $m" | bc -l`

printf '%0.3f' "$avg"

To compute median:

!/bin/bash

# This program computes the median of a list of integers on the command line.
# If there are no arguments, then it asks the user to enter them on
# separate lines.  Then it prints the median (middle
# value after sorting).  If there are an even number of numbers, then
# the median is the average of the 2 middle values, rounding down

# Copy the command line into array n
i=0  # current element of n
finished=0  # 1 (true) when there is no more interactive input expected
while (($# > 0)); do
  ((n[i] = $1))
  shift
  ((i++))
  finished=1
done

# Else if there are no arguments then ask the user to enter some numbers
if ((!finished)); then
  echo Enter some numbers, one per line.  Enter a blank line when done.
  while ((!finished)); do
    read x
    if [ "$x" == "" ]; then
      finished=1
    else
      ((n[i++]=x))
    fi
  done
fi

# If n is empty then give up
size=${#n[*]}
if ((size==0)); then
  echo Error: at least one number is expected
  exit 1
fi

# Bubble sort n
((i=size-1))
while ((i>0)); do
  j=0
  while ((j<i)); do
    if ((n[j]>n[j+1])); then  #swap so n[j] <= n[j+1]
      ((t=n[j]))
      ((n[j]=n[j+1]))
      ((n[j+1]=t))
    fi
    ((++j))
  done
  ((--i))
done

# Compute the median from the middle 1 or 2 elements
((mid = size/2))
if ((size  % 2 == 0)); then
  ((median = (n[mid] + n[mid-1]) / 2))
else
  ((median = n[mid]))
fi

# Print the sorted array and the median
echo The median of ${n[*]} is $median

Related Solutions

Please do it in C++. Please comment on the code, and comments detail the run time...
Please do it in C++. Please comment on the code, and comments detail the run time in terms of total operations and Big O complexities. 1. Implement a class, SubstitutionCipher, with a constructor that takes a string with the 26 uppercase letters in an arbitrary order and uses that as the encoder for a cipher (that is, A is mapped to the first character of the parameter, B is mapped to the second, and so on.) Please derive the decoding...
This question is about java program. Please show the output and the detail code and comment...
This question is about java program. Please show the output and the detail code and comment of the each question and each Class and interface. And the output (the test mthod )must be all the true! Thank you! Question1 Create a class Animal with the following UML specification: +-----------------------+ | Animal | +-----------------------+ | - name: String | +-----------------------+ | + Animal(String name) | | + getName(): String | | + getLegs(): int | | + canFly(): boolean | |...
Write this code in java and don't forget to comment every step. Write a method which...
Write this code in java and don't forget to comment every step. Write a method which asks a baker how hot their water is, and prints out whether it is OK to make bread with the water. If the water is at or above 110F, your method should print "Too Warm." If the water is below 90.5F, print "Too Cold." If it is in between, print "Just right to bake!" For example, if the user inputs the number 100.5, the...
* Show the output of the following BASH code: i=7 i=$((i-5)) if [ $i -eq 4...
* Show the output of the following BASH code: i=7 i=$((i-5)) if [ $i -eq 4 ] # -eq is == then         echo "Four" elif [ $i -eq 2 ] then         echo "Two" else         echo $i fi
List the adult reference range for the following tests:   use your book and don’t forget labtestsonline.org as...
List the adult reference range for the following tests:   use your book and don’t forget labtestsonline.org as a resource Reticulocyte % Serum Iron Serum ferritin             Male             Female TIBC Transferrin saturation % Complete the following chart for the differential diagnosis of Microcytic/Hypochromic Anemias Anemia Fe Level Ferritin TIBC % Saturation Reticulocyte Iron Deficiency Iron deficiency with treatment for 12 days Anemia Beta Thalassemia Minor Hereditary Hemochromatosis Sideroblastic Anemia Explain ferritin levels replace the necessity of performing bone marrow aspirations on cases of...
Please code in C#-Visual Studio Tasks The program needs to contain the following A comment header...
Please code in C#-Visual Studio Tasks The program needs to contain the following A comment header containing your name and a brief description of the program Output prompting the user for the following inputs: Name as a string Length of a rectangle as a double Width of a rectangle as a double Length of a square as an int After accepting user input, the program outputs the following: User name Area of a rectangle with dimensions matching the inputs Area...
Java Please comment code Create an Interactive JavaFX Application Create an application with the following controls:...
Java Please comment code Create an Interactive JavaFX Application Create an application with the following controls: A Label control with the following text displayed: "First Number:" A Label control with the following text displayed: "Second Number:" An empty TextField control beside the First Number label. An empty TextField control beside the Second Number label. Five buttons: Button 1 labeled + Button 2 labeled - Button 3 labeled * Button 4 labeled / Button 5 labeled = An empty Label control...
Please write code in java and comment . thanksItem classA constructor, with a String...
Please write code in java and comment . thanksItem classA constructor, with a String parameter representing the name of the item.A name() method and a toString() method, both of which are identical and which return the name of the item.BadAmountException ClassIt must be a RuntimeException. A RuntimeException is a subclass of Exception which has the special property that we wouldn't need to declare it if we need to use it.It must have a default constructor.It must have a constructor which...
Please write the following swap functions and print code in main to show that the functions...
Please write the following swap functions and print code in main to show that the functions have adequately . Your code should, in main(), print the values prior to being sent to the swap function. In the swap function, the values should be swapped and then in main(), please print the newly swapped values. 1) swap integer values using reference parameters 2) swap integer values using pointer parameters 3) swap pointers to integers - you need to print the addresses,...
Please write code in java and comment . thanks Item class A constructor, with a String...
Please write code in java and comment . thanks Item class A constructor, with a String parameter representing the name of the item. A name() method and a toString() method, both of which are identical and which return the name of the item. BadAmountException Class It must be a RuntimeException. A RuntimeException is a subclass of Exception which has the special property that we wouldn't need to declare it if we need to use it. It must have a default...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT