# Thread: Make the sum of all elements of only one dimension of an array

1. Member
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;

/****************************************************
* 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] =

}
}
}
}
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] =

}
}
}

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 {

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

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.

2. Banned
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.

3. Member
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, 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,

4. Banned
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.

5. Member
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,

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.

7. Banned
Join Date
Dec 2011
Posts
143
Rep Power
0

## Re: Make the sum of all elements of only one dimension of an array

Originally Posted by ana_carol
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,
What if you change it to System.err.println (mat_modImag [0] [0] [16] [16]);

can you still see the output?

8. Member
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){ ...

9. Member
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...

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.

11. Member
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.

12. ## Re: Make the sum of all elements of only one dimension of an array

but this my method isn't calculated
Please explain and show the input and the output and explain what is wrong with it.

Without the code and the data you are using, no one can help you solve your problem.

13. Member
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;

/****************************************************
* 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] =

}
}
}
}
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] =

}
}
}

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 {

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

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, exit value: 0>C:\Arquivos de programas\Java\jre6\bin\javaw.exe (16/12/2011 11:50:44)

14. Banned
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..

15. ## Re: Make the sum of all elements of only one dimension of an array

What does that message mean? What does localhost have to do with your program?

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.

16. Member
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]);```
And shows me this:

6.027453177311578E-4
19.0
22.0
7.408909111813356E-4 (this line in "red")

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.

18. Member
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);```

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.
Have you tried to write code to call the sum() method? What happened?
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.

20. Member
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.main(ReadFile.java:94) (refers to line: double integ_modImag = soma(mat_modImag, dim1, dim2, dim3, dim4);)

Page 1 of 2 12 Last

#### Posting Permissions

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