Results 1 to 20 of 23
 12162011, 11:58 AM #1Member
 Join Date
 Dec 2011
 Posts
 11
 Rep Power
 0
Make the sum of all elements of only one dimension of an array
Hi all,
I'm trying to make the sum of all elements of only one
dimension of an array, I created a method to accomplish such a sum, but not know what is happening, by the way I realized that this did not seem to enter into the method. See my code:
Java Code:import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; public class ReadFile { /**************************************************** * Method created to receive the two 4D arrays ****************************************************/ public static double[][][][] criaMatriz4D(int dim1, int dim2, int dim3, int dim4, DataInputStream input) throws IOException{ double[][][][] matriz = new double[dim1][dim2][dim3][dim4]; for (int l = 0; l < dim4; l++) { for (int k = 0; k < dim3; k++) { for (int j = 0; j < dim2; j++) { for (int i = 0; i < dim1; i++) { matriz[i][j][k][l] = input.readDouble(); } } } } return matriz; } /**************************************************** * Method created to receive the two 3D arrays ****************************************************/ public static float[][][] criaMatriz3D(int dim2, int dim3, int dim4, DataInputStream input) throws IOException{ float[][][] matriz = new float[dim2][dim3][dim4]; for (int k = 0; k < dim4; k++) { for (int j = 0; j < dim3; j++) { for (int i = 0; i < dim2; i++) { matriz[i][j][k] = input.readFloat(); } } } return matriz; } /****************************************************** * Method to realize the sum ******************************************************/ public static double soma(double[][][][] mat_modImag, int dim1, int dim2, int dim3, int dim4){ System.out.println("Here =" ); int sum = 0; for(int l = 0; l < dim4; l++){ sum += mat_modImag[dim1][dim2][dim3][l]; System.out.println("Aqui ="); } System.out.println("the sum is =" +sum); return sum; } public static void main(String args[]) throws IOException { // Reading the binary file DataInputStream input = new DataInputStream(new FileInputStream(new File("C:/Documents and Settings/Web/data.dtb"))); // Reading the dimensions saved in the binary int dim1 = 4; // fixed dimension int dim2 = input.readInt(); // size read from binary file int dim3 = input.readInt(); // size read from binary file int dim4 = input.readInt(); // size read from binary file System.out.println("Dimension1= " +dim1); System.out.println("Dimension2= " +dim2); System.out.println("Dimension3= " +dim3); System.out.println("Dimension4= " +dim4); double [][][][] mat_modImag = criaMatriz4D(4, dim2, dim3, dim4, input); double [][][][] mat_conjImag = criaMatriz4D(4, dim2, dim3, dim4, input); float [][][] mat_modScatter = criaMatriz3D(dim2, dim3, dim4, input); float [][][] mat_conjScatter = criaMatriz3D(dim2, dim3, dim4, input); // Close input.close(); } // Method to print the matrix private static void imprimeMatriz(double[][][][] matriz) { System.out.println(""); for (int i = 0; i < matriz.length; i++) { for (int j = 0; j < matriz[i].length; j++) { for (int k = 0; k < matriz[i][j].length; k++) { System.out.print(" "); for (int l = 0; l < matriz[i][j][k].length; l++) { //System.out.print(matriz[i][j][k][l] + " "); } System.out.println(); } System.out.println(""); } System.out.println(""); } } }
I do not know what I did wrong. Can you help me??
Thanks,
Ana Carol.
 12162011, 12:51 PM #2Banned
 Join Date
 Dec 2011
 Posts
 143
 Rep Power
 0
Re: Make the sum of all elements of only one dimension of an array
Cut and Paste the error you are getting and
tell us what output you are expecting that is not coming out or is coming out wrong.
 12162011, 12:59 PM #3Member
 Join Date
 Dec 2011
 Posts
 11
 Rep Power
 0
Re: Make the sum of all elements of only one dimension of an array
I inserted a breakpoint inside the method, but the only thing that displays is below:
<terminated>ReadFile [Java Application]
<disconnected>ReadFile at localhost:2138
<terminated, exit value: 0>C:\Arquivos de programas\Java\jre6\bin
\javaw.exe (09/12/2011 13:50:44)
I want to sum all the elements of only one dimension of the array of four dimensions. My array is this: DOUBLE = Array[4, 16, 32, 32].
I want to sum all elements of dimension four, for example.
Thanks,
 12162011, 01:32 PM #4Banned
 Join Date
 Dec 2011
 Posts
 143
 Rep Power
 0
Re: Make the sum of all elements of only one dimension of an array
Put a print statement first thing in your main method.
Do you see the output?
It looks like your output may be going somewhere unexpected.
 12162011, 01:39 PM #5Member
 Join Date
 Dec 2011
 Posts
 11
 Rep Power
 0
Re: Make the sum of all elements of only one dimension of an array
Well.... when i put this line in my main method:
/ / Print the positions of the matrices
System.out.println (mat_modImag [0] [0] [16] [16]);
I can see the result, show me the values the positions.
Thanks,
 12162011, 01:46 PM #6
Re: Make the sum of all elements of only one dimension of an array
Can you post what the program prints out?
Add some comments to the print out that explains what is wrong with it
and show what the output should be.
 12162011, 01:55 PM #7Banned
 Join Date
 Dec 2011
 Posts
 143
 Rep Power
 0
 12162011, 01:59 PM #8Member
 Join Date
 Dec 2011
 Posts
 11
 Rep Power
 0
Re: Make the sum of all elements of only one dimension of an array
But this is correct, is printing the value of the position correct.
My problem is in the method in line: public static double soma(double[][][][] mat_modImag, int dim1, int dim2, int dim3, int dim4){ ...
 12162011, 02:01 PM #9Member
 Join Date
 Dec 2011
 Posts
 11
 Rep Power
 0
Re: Make the sum of all elements of only one dimension of an array
It simply prints the values of the locations below, but the result of the sum of all elements of only one dimension of the array does not print and also does not display any error.
What it shows is that by bringing the breakpoint.
I tried to send my binary file to you test my code and see, but the forum does not allow extension .bin
Thanks...
 12162011, 02:10 PM #10
Re: Make the sum of all elements of only one dimension of an array
For testing can you manually make a 4 dim array in your code and call the method that you want to test?
If the read the data methods have been tested and are working you can just ignore them for testing the suma method by giving it a 4 dim array that you create in the program.
 12162011, 02:14 PM #11Member
 Join Date
 Dec 2011
 Posts
 11
 Rep Power
 0
Re: Make the sum of all elements of only one dimension of an array
I did this. I created a 4dim array and the test ok, but this my method isn't calculated. I do not know what to do.
 12162011, 02:31 PM #12
Re: Make the sum of all elements of only one dimension of an array
but this my method isn't calculated
Without the code and the data you are using, no one can help you solve your problem.
 12162011, 03:39 PM #13Member
 Join Date
 Dec 2011
 Posts
 11
 Rep Power
 0
Re: Make the sum of all elements of only one dimension of an array
My code is this:
Java Code:import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; public class ReadFile { /**************************************************** * Method created to receive the two 4D arrays ****************************************************/ public static double[][][][] criaMatriz4D(int dim1, int dim2, int dim3, int dim4, DataInputStream input) throws IOException{ double[][][][] matriz = new double[dim1][dim2][dim3][dim4]; for (int l = 0; l < dim4; l++) { for (int k = 0; k < dim3; k++) { for (int j = 0; j < dim2; j++) { for (int i = 0; i < dim1; i++) { matriz[i][j][k][l] = input.readDouble(); } } } } return matriz; } /**************************************************** * Method created to receive the two 3D arrays ****************************************************/ public static float[][][] criaMatriz3D(int dim2, int dim3, int dim4, DataInputStream input) throws IOException{ float[][][] matriz = new float[dim2][dim3][dim4]; for (int k = 0; k < dim4; k++) { for (int j = 0; j < dim3; j++) { for (int i = 0; i < dim2; i++) { matriz[i][j][k] = input.readFloat(); } } } return matriz; } /****************************************************** * Method to realize the sum ******************************************************/ public static double soma(double[][][][] mat_modImag, int dim1, int dim2, int dim3, int dim4){ System.out.println("Here =" ); int sum = 0; for(int l = 0; l < dim4; l++){ sum += mat_modImag[dim1][dim2][dim3][l]; System.out.println("Aqui ="); } System.out.println("the sum is =" +sum); return sum; } public static void main(String args[]) throws IOException { // Reading the binary file DataInputStream input = new DataInputStream(new FileInputStream(new File("C:/Documents and Settings/Web/data.dtb"))); // Reading the dimensions saved in the binary int dim1 = 4; // fixed dimension int dim2 = input.readInt(); // size read from binary file int dim3 = input.readInt(); // size read from binary file int dim4 = input.readInt(); // size read from binary file System.out.println("Dimension1= " +dim1); System.out.println("Dimension2= " +dim2); System.out.println("Dimension3= " +dim3); System.out.println("Dimension4= " +dim4); double [][][][] mat_modImag = criaMatriz4D(4, dim2, dim3, dim4, input); double [][][][] mat_conjImag = criaMatriz4D(4, dim2, dim3, dim4, input); float [][][] mat_modScatter = criaMatriz3D(dim2, dim3, dim4, input); float [][][] mat_conjScatter = criaMatriz3D(dim2, dim3, dim4, input); // Close input.close(); // Print the positions of the matrices System.out.println(mat_modImag[0][0][16][16]); System.out.println(mat_conjImag[0][0][19][16]); System.out.println(mat_modScatter[1][2][1]); System.out.println(mat_conjScatter[14][16][3]); } // Method to print the matrix private static void imprimeMatriz(double[][][][] matriz) { System.out.println(""); for (int i = 0; i < matriz.length; i++) { for (int j = 0; j < matriz[i].length; j++) { for (int k = 0; k < matriz[i][j].length; k++) { System.out.print(" "); for (int l = 0; l < matriz[i][j][k].length; l++) { //System.out.print(matriz[i][j][k][l] + " "); } System.out.println(); } System.out.println(""); } System.out.println(""); } } }
I want to make the sum of all four elements of the dimension of the matrix criaMatriz4D....in my main method i call: double [][][][] mat_modImag = criaMatriz4D(4, dim2, dim3, dim4, input);
Error:
<terminated>ReadFile [Java Application]
<disconnected>ReadFile at localhost:2138
<terminated, exit value: 0>C:\Arquivos de programas\Java\jre6\bin\javaw.exe (16/12/2011 11:50:44)
 12162011, 03:48 PM #14Banned
 Join Date
 Dec 2011
 Posts
 143
 Rep Power
 0
Re: Make the sum of all elements of only one dimension of an array
See post #7
It will tell us if your errors are going to your terminal or somewhere else  use System.err.println()
If they are going somewhere else, we need to find where..
 12162011, 03:52 PM #15
Re: Make the sum of all elements of only one dimension of an array
<disconnected>ReadFile at localhost:2138
For testing can you manually make a 4 dim array in your code and call the method that you want to test?
If the read the data methods have been tested and are working you can just ignore them for testing the soma method by giving it a 4 dim array that you create in the program.
 12162011, 03:54 PM #16Member
 Join Date
 Dec 2011
 Posts
 11
 Rep Power
 0
Re: Make the sum of all elements of only one dimension of an array
I did this:
Java Code:System.err.println (mat_modImag [0] [0] [16] [16]); System.out.println(mat_conjImag[0][0][19][16]); System.out.println(mat_modScatter[1][2][1]); System.out.println(mat_conjScatter[14][16][3]);
6.027453177311578E4
19.0
22.0
7.408909111813356E4 (this line in "red")
 12162011, 04:00 PM #17
Re: Make the sum of all elements of only one dimension of an array
Are those outputs correct?
If not, show what the correct output should be.
You need to provide the input for the methods if you want anyone to test them.
Manually put a 4 dim array into your program so that we can test with the same code and data as you are using.
 12162011, 04:08 PM #18Member
 Join Date
 Dec 2011
 Posts
 11
 Rep Power
 0
Re: Make the sum of all elements of only one dimension of an array
Yes, those outputs is correct. I don't know do this. My problem is in the method sum:
Java Code:/****************************************************** * Method to realize the sum ******************************************************/ public static double sum(double[][][][] mat_modImag, int dim1, int dim2, int dim3, int dim4){ System.out.println("Here =" ); int sum = 0; for(int l = 0; l < dim4; l++){ sum += mat_modImag[dim1][dim2][dim3][l]; System.out.println("Aqui ="); } System.out.println("the sum is =" +sum); return sum; }
See:
Here i call the method createMatrix4D and createMatrix3D, but I do not call the method of the sum. I don't know this.
Java Code:double [][][][] mat_modImag = createMatrix4D(4, dim2, dim3, dim4, input); double [][][][] mat_conjImag = createMatrix4D(4, dim2, dim3, dim4, input); float [][][] mat_modScatter = createMatrix3D(dim2, dim3, dim4, input); float [][][] mat_conjScatter = createMatrix3D(dim2, dim3, dim4, input);
 12162011, 04:13 PM #19
Re: Make the sum of all elements of only one dimension of an array
I do not call the method of the sum. I don't know this.
Look at the definition for the sum method. What arguments does it take? What order are the arguments in?
The order of the arguments is different from the other methods.
What does the method return? That is also different from the other methods you have coded.
 12162011, 04:20 PM #20Member
 Join Date
 Dec 2011
 Posts
 11
 Rep Power
 0
Re: Make the sum of all elements of only one dimension of an array
I do not know how to call the method of the sum.
I tried so: double integ_modImag = soma(mat_modImag, dim1, dim2, dim3, dim4);
But returns this error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at ReadFile.soma(ReadFile.java:60)
at ReadFile.main(ReadFile.java:94) (refers to line: double integ_modImag = soma(mat_modImag, dim1, dim2, dim3, dim4);)
Similar Threads

Single to multidimension array
By Migy in forum New To JavaReplies: 2Last Post: 04052011, 06:12 PM 
Taking out elements from a matrix thus reducing its dimension
By theodorekon in forum New To JavaReplies: 5Last Post: 04152010, 05:55 PM 
[SOLVED] how to align JTextFiels[dimension] and JComboBox[dimension]
By Wolverine in forum AWT / SwingReplies: 5Last Post: 05182009, 12:42 PM 
noob: twodimension array
By bobmasta5 in forum New To JavaReplies: 5Last Post: 03152009, 11:42 PM 
Arraylist to a 2 dimension array conversion
By mars123 in forum New To JavaReplies: 1Last Post: 12062007, 11:24 AM
Bookmarks