# Thread: Some problems with arrays and loops

1. Member
Join Date
Mar 2008
Posts
32
Rep Power
0

## Some problems with arrays and loops

Hi all

My problem is that i cannot covert simple program which I wrote to the one that would reduce the size of it and do the same. Main problem are the arrays and loops. What I am trying to do is read in the file as shown below and run it trough the algorithms which are in the "numbers class" and out put the results on screen and in the file. I have made some progress with reading the file in and string it into array (code was from the forum) Yes I am re using code.

I did this and it works I know it is simple:

Java Code:
```import java.io.*;
import static java.lang.Math.*;

class numbers
public static double Euclidean(double[] array1, double[] array2)
{
//Store the numbers of the both arrays
double x1= array1[0];
double y1=array2[0];
double x2= array1[1];
double y2= array2[1];
double x3=array1[2];
double y3=array2[2];

//Compute the minus part of the formula
double minusResult1 = y1-x1;
double minusResult2 = y2-x2;
double minusResult3 = y3-x3;

double square1 = pow(minusResult1, 2);
double square2 = pow(minusResult2, 2);
double square3 = pow(minusResult3, 2);

//Sum the squares
double sumOfSquares = square1+square2+square3;

//Get the square root of the sumOfSquares
double finalResult = sqrt(sumOfSquares);

return finalResult;
}

public static double Manhattan(double[] array1, double[] array2)
{

//Store the numbers of the both arrays
double x1= array1[0];
double y1=array2[0];
double x2= array1[1];
double y2= array2[1];
double x3=array1[2];
double y3=array2[2];

//Compute the minus part of the formula
double minusResult1 = y1-x1;
double minusResult2 = y2-x2;
double minusResult3 = y3-x3;
double abs1 = abs(minusResult1 );
double abs2 = abs(minusResult2);
double abs3 = abs(minusResult3 );

//Sum the squares
double sumOfAbs = abs1+abs2+abs3;

//Get the square root of the sumOfAbs
double finalResult = sqrt(sumOfAbs);

return finalResult;
}

public static void main(String[] args)
{

double[] gene={-9.30, 5.65, -5.02};
double[] gene1={-4.10, 5.68, 0.56};
double EuclideanDist = Euclidean(gene, gene1);
System.out.println("Euclidean distance = " + EuclideanDist);

double ManhattanDist = Manhattan(gene, gene1);
System.out.println("Manhattan = " + ManhattanDist);

}

}```
And I am trying to use loops and arrays so I don't have to code all of this manually but I am making no progress.

Java Code:
```import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.Writer;

public class csv {
public static double Euclidean(double[] array1, double[] array2){
// Declare Array
String[] myarray1;
String[] myarray2;
String[] ResultArray;

arrayX = new String[3];
arrayY = new String[61];
ResultArray = new String[61];

double array1 = new double [arrayX];
double array2 = new double [arrayY];
double array3 = new double [ResultArray];

array3 = array2 - array1;

// Read each line into the array
for (int i = 0; i < ResultArray.length; i++){
double square = pow(array3,2);
}

double sumOfSquares = sum(square);

//Get the square root of the sumOfSquares
double finalResult = sqrt(sumOfSquares);

return finalResult;

}

public static void main(String[] args) throws Exception {

// Set file name & path
String filepath = "Data_Set_1.txt";
//String output = "Output_Data_Set_1.txt";

FileInputStream in = new FileInputStream(filepath);
String strLine;

//Write file
//FileOutputStream out = new FileOutputStream(output);
//BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(out));

// Declare Array which will hold 61 lines
String[] myarray;
myarray = new String[61];

// Read each line into the array
for (int i = 0; i < myarray.length; i++){
}

in.close();

// Print out array info in desired order. Delete " characters
for (int i = 1; i < myarray.length; i++){
System.out.println(myarray[i].replace("\"", ""));

}

}
}```
Java Code:
```Experiment 1	Experiment 2	Experiment 3
-9.30		5.65		-5.02
-4.10		5.68		0.56
4.75		0.11		2.86
-1.54		-10.01		-1.99
-6.29		5.62		-2.78
-13.02		1.31		1.61
-5.85		3.79		-6.56
-12.71		-0.45		0.62
-3.05		-12.65		-5.07
2.18		-5.82		-9.73
1.74		-0.99		-3.66
-7.88		0.93		-8.33
-6.34		-1.85		-4.00
-0.71		-2.64		-5.64
2.77		-4.16		-3.83
-0.25		0.58		0.77
2.52		-5.80		-8.80
5.53		-9.11		-1.92
6.63		2.53		7.94
-7.11		8.09		-6.47
-0.30		-6.18		-6.25
-0.33		4.58		-8.32
-2.39		1.44		3.94
7.70		9.39		4.73
4.89		-6.17		6.98
6.05		-6.54		-1.94
6.78		7.83		-7.72
4.86		2.31		2.33
6.93		3.95		10.61
0.04		-7.31		-1.49
-4.78		3.41		10.69
5.55		-6.48		-0.56
-3.18		-4.32		-3.32
3.67		-9.79		-13.03
-11.01		-3.69		-5.13
-2.24		-2.19		-12.74
8.84		2.10		1.28
0.69		7.11		-5.35
-9.19		-6.10		-11.01
2.37		6.58		2.67
6.40		6.03		3.58
-0.54		-1.32		-0.69
-0.45		0.01		-0.25
0.52		0.96		0.28
0.86		1.07		0.96
-0.47		-1.36		-0.81
-0.64		-1.47		-0.32
0.34		-0.47		0.46
-0.27		-0.62		-0.43
-0.49		-0.22		-0.01
-9.80		2.34		-5.95
6.74		-8.43		4.21
-7.60		-6.57		-5.13
-6.53		-2.55		-13.86
-3.82		7.12		-11.45
5.49		-7.60		0.40
6.95		-2.61		-5.14
0.21		-6.98		0.84
1.65		3.77		3.33
6.16		3.25		-14.31```
I f some one can help me or give me some pointers I would appreciate it .
Thank you.

2. Originally Posted by BHCluster

And I am trying to use loops and arrays so I don't have to code all of this manually but I am making no progress.
What mean 'progress'? On performance, like taken to complete the task....

3. Member
Join Date
Mar 2008
Posts
32
Rep Power
0
ok
what i am trying to figure out is how to do this
class numbers
public static double Euclidean(double[] array1, double[] array2)
{
//Store the numbers of the both arrays
double x1= array1[0];
double y1=array2[0];
double x2= array1[1];
double y2= array2[1];
double x3=array1[2];
double y3=array2[2];

//Compute the minus part of the formula
double minusResult1 = y1-x1;
double minusResult2 = y2-x2;
double minusResult3 = y3-x3;

double square1 = pow(minusResult1, 2);
double square2 = pow(minusResult2, 2);
double square3 = pow(minusResult3, 2);

//Sum the squares
double sumOfSquares = square1+square2+square3;

//Get the square root of the sumOfSquares
double finalResult = sqrt(sumOfSquares);

return finalResult;
}
61 times with out doing one by one manually, and what I have ended up is:
public static double Euclidean(double[] array1, double[] array2){
// Declare Array
String[] myarray1;
String[] myarray2;
String[] ResultArray;

arrayX = new String[3];
arrayY = new String[61];
ResultArray = new String[61];

double array1 = new double [arrayX];
double array2 = new double [arrayY];
double array3 = new double [ResultArray];

array3 = array2 - array1;

// Read each line into the array
for (int i = 0; i < ResultArray.length; i++){
double square = pow(array3,2);
}

double sumOfSquares = sum(square);

//Get the square root of the sumOfSquares
double finalResult = sqrt(sumOfSquares);

return finalResult;

}
So what I am trying to say is can some one please tell me if I am doing this right or wrong.
Thanks

4. About the file "file to be read"... it would be nice if you cascade it down...

so that, you may implement it in easier way... " by using scanner"....

kind regards,
sukatoa

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•