Domanda

Okay, I have found 2 short codes that work, but I want to understand how they work. I have googled and check the links such as:

http://www.tutorialspoint.com/java/lang/math_pow.htm http://www.tutorialspoint.com/java/lang/math_sqrt.htm

But the explanation there is not clear. So in other words I wish know/understand what's going on in each line of both codes.

A) The below program fragment computes (given an integer array data) and prints the geometric mean of all the entries in data:

   double product = 1;// For example, I understand why it is 1, since if it was 0 then the product would be keep getting 0, since any number *0 is always zero.
    for(int i=0; i<data.length; i++)//okay for loop is getting out all the intergers from the data array.
    product*=data[i];//This is I am not too sure, I guess each item in array is getting multiplied with each other????
    double gmean=Math.pow(product,1.0/data.length); // Yes, I hate this line, because I don't understand it, can someone explain this line please? Please use easy English, I am not as smart as you.

B) This second code fragment computes (given an integer array data) and prints the quadratic mean of all the entries in data:

double sum=0; //Okay the sum should be 0 because at the moment nothing has been summed up.
for(int i=0; i<data.length; i++)// Now getting out all the items in array called data.
sum+= data[i]*data[i];//Now I am not too sure, all the items in the array called data is getting multiplied with each other and then getting added up? I am not too sure, if would be good if someone could explain this with easy English.
double qmean = Math.sqrt(sum/data.length);// I hate this line, because I don't understand it.
System.out.println(qmean);// Displays the final result.

Okay as you can see, I do understand some lines in the code, while there are some lines, I didn't understand, it would be so fantastic if someone could explain the lines, I didn't really understand using easy English and not in a complicated way.

Thanks in advance.

È stato utile?

Soluzione 2

math.pow(a,b) means a^b and math.sqrt(a) is the square root of a. you don't understand the java method or the mathematical logic in your content?

Altri suggerimenti

Suppose data is {3,2,7}. data.length is 3. This will compute sqrt((9+4+49)/3).

double sum=0;
// sum is now zero

for(int i=0; i<data.length; i++)
// Execute the following statement with i having each value starting from 0,
// incrementing by 1 each time (i++), as long as i remains less than 3.
    sum+= data[i]*data[i];
    // The sum+= statement is executed three times, with i each of 0, 1, and 2.
    // The first time adds 9 to sum getting 9
    // The second time adds 4 to sum getting 13
    // The third time adds 49 to sum, getting 62

double qmean = Math.sqrt(sum/data.length);
// make qmean equal to sqrt(62/3).

System.out.println(qmean);// Displays the final result.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top