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

Run the following code and explain what the code does Run this code at least twice...
Run the following code and explain what the code does Run this code at least twice and take screenshots Explain what the code does What is the running result supposed to be What caused this issue? #include <thread> #include <iostream> using namespace std; const unsigned int NTHREADS = 20; const int ITERS = 10000000; int counter; void increment() {        for (int i = 0; i < ITERS; i++)                      counter++;               } void decrement() {        for (int i...
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:...
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...
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 +...
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
Question 2. The following code defines an array size that sums elements of the defined array...
Question 2. The following code defines an array size that sums elements of the defined array through the loop. Analyze the following code, and demonstrate the type of error if found? What we can do to make this code function correctly? #include <stdio.h> #define A 10 int main(int argc, char** argv) { int Total = 0; int numbers[A]; for (int i=0; i < A; i++) numbers[i] = i+1; int i =0; while (i<=A){    Total += numbers[i];    i++; }...
# 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...
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...
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