Question

In: Computer Science

Run the following code and explain the results (expectation of the array). Test it on the...

Run the following code and explain the results (expectation of the array). Test it on the following arrays: [1.0, 2.0, 3.0, 4.0, 5.0, 6.0] [1.0, 4.0, 6.0,2.0 , 3.0, 5.0] [5.0, 3.0, 2.0, 6.0, 4.0, 1.0] [6.0, 5.0, 4.0, 3.0, 2.0, 1.0] // Java code to calculate expected // value of an array import java.io.*; class GFG { // Function to calculate expectation static float calc_Expectation(float a[], float n) { // Variable prb is for probability of each // element which is same for each element float prb = (1 / n); // calculating expectation overall float sum = 0; for (int i = 0; i < n; i++) sum += a[i] * prb; // returning expectation as sum return sum; } // Driver program public static void main(String args[]) { float expect, n = 6f; float a[] = { 1f, 2f, 3f, 4f, 5f, 6f }; // Function for calculating expectation expect = calc_Expectation(a, n); // Display expectation of given array System.out.println("Expectation of array E(X) is : " + expect); } }

Solutions

Expert Solution

Given program:

import java.io.*;
public class GFG {
// Function to calculate expectation
  
static float calc_Expectation(float a[], float n) {
  
// Variable prb is for probability of each
  
// element which is same for each element
  
float prb = (1 / n);
  
// calculating expectation overall
  
float sum = 0;
  
for (int i = 0; i < n; i++)
  
sum += a[i] * prb;
  
// returning expectation as sum
  
return sum;
  
} // Driver program
  
public static void main(String args[]) {
  
float expect, n = 6.0f;
  
float a[] = {1f, 2f, 3f, 4f, 5f, 6f};
  
// Function for calculating expectation
  
expect = calc_Expectation(a, n);
  
// Display expectation of given array
  
System.out.println("Expectation of array E(X) is : " + expect);
}
}

Explanation:

the array is taken input to calc_Expectation() function

probability is 1/n = 1/6 in all cases given

multiply all elements if array with prb and add them to variable sum

for all the given arrays the output will be same because elements inside the array are same.

Output:

for array: a = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]

Output:

for array: a = [1.0, 4.0, 6.0,2.0 , 3.0, 5.0]

Output:

for array: a = [5.0, 3.0, 2.0, 6.0, 4.0, 1.0]

Output:

for array: a = [6.0, 5.0, 4.0, 3.0, 2.0, 1.0]


Related Solutions

The following code will generate a Decision Tree. You need to run the code and explain...
The following code will generate a Decision Tree. You need to run the code and explain the tree. After you get the Tree. You need to explain how does it draw like that. install.packages("rpart.plot") # install package rpart.plot ########################################## # section 7.1.1 Overview of a Decision Tree ########################################## library("rpart") library("rpart.plot") # Read the data setwd("c:/data/") banktrain <- read.table("bank-sample-test.csv",header=TRUE,sep=",") ## drop a few columns to simplify the tree drops<-c("age", "balance", "day", "campaign", "pdays", "previous", "month") banktrain <- banktrain [,!(names(banktrain) %in% drops)]...
Compile and run the following code then answer the following questions: a.) Run this command from...
Compile and run the following code then answer the following questions: a.) Run this command from the shell prompt: ./a.out ls -F Explain, in your own words, why you see the screen output you do and how that output relates to both the content of the program AND the nature of the shell command used to invoke it. Be sure to comment on the pointer arithmetic done inside the line: execvp(*(argv+1), argv+1); b.) Run this command from the shell prompt:...
What are the contents of the array after the for-loop in the following code?
(IN C)What are the contents of the array after the for-loop in the following code?int array[ SIZE ][ SIZE ] = { { 4, 5, 6, 7, 8 },{ 1, 2, 3, 4, 5 },{ 3, 6, 7, 8, 9 },{ 2, 3, 4, 5, 6 },{ 5, 6, 7, 8, 9 } };int i;int *ptr = array[ 0 ];for( i = 0; i < SIZE * SIZE; i++ ) {if( i % SIZE < 2 ) {*( ptr +...
Sample Code and Models Run each of the models below and explain the code function and...
Sample Code and Models Run each of the models below and explain the code function and your findings for each system, do they agree/disagree with what you understand and why ?? Matlab Code % Winter 2018 Control Engineering % Lab No.3 - Root Locus problems % Mark Clarke clear s = tf('s') K = 1150; %Proportional Controller Gain, May need to be altered? % Enter Model 1 % This is a model of a simple 2nd order with no zeros...
verilog code of a 64 x64 array with 128 parameters please include test bench
verilog code of a 64 x64 array with 128 parameters please include test bench
1. Adapt the custom array list implementation code with the following changes: (a) Add code to...
1. Adapt the custom array list implementation code with the following changes: (a) Add code to the ensureCapacity() method to print out a message including how many elements are copied to the new array on resizing Array List Implementation: public class MyArrayList<E> implements MyList<E> { public static final int INITIAL_CAPACITY = 16; private E[] data = (E[])new Object[INITIAL_CAPACITY]; private int size = 0; // Number of elements in the list public MyArrayList() { }    public MyArrayList(E[] objects) { for...
# Do not delete this cell. You have to run this cell to test your code...
# Do not delete this cell. You have to run this cell to test your code in the test cases. score = dict() # This function will search the given text in order to find a given word def is_word_there(text, word):        """         What it takes?             a text to be searched             a word to be searched within a text                    what it does?             searches if a word occurs in a text                    What...
The following code was meant to print out the elements in an array in reverse order....
The following code was meant to print out the elements in an array in reverse order. However, it does not behave correctly. public static void reverse(int[] a, int index) {       if (index == (a.length - 1))         System.out.printf("%d%n", a[index]);       else {         reverse(a, index); What does it do? Explain why it behaves in this way and There is more than one error in the code. Correct the code so that it will recursively print out the elements of...
c++ I need a code that will fill an array size of 1000, an array of...
c++ I need a code that will fill an array size of 1000, an array of size 2000, and an array size of 10000, with random int values. Basically like this: array1[1000] = filled all with random numbers array2[2000] = filled all with random numbers array3[10000] = filled all with random numbers C++ no need for print
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT