TechArena Community Function that returns an array

#1
17-04-2009
 Member Join Date: Oct 2008 Posts: 27
Function that returns an array

Hello, I am beginner in java.

I am setting up a program that operates on square matrices. (display, sum, product...). I want to create a function that returns a table with the minimum of each row of the matrix.

Here's what I do:
Code:
```public int [] minLignes () (
int [] rep = new int [this.TAB.length];
for (int i = 0; i <this.TAB.length i + +)
for (int j = 0 j <this.TAB.length j + +)
if (this.TAB [i] [j]> this.TAB [i +1] [j +1])
REP [i] = TAB [i] [j];
REP return;
)```
Maybe this is not good, but this is not what interests me. What I want to know is how to display in the main program, the answer to this?
Code:
```MatriceCarree M = new MatriceCarree (3);

M.saisie ();

M.affichage () / / so far all is well!

M.minLignes () / /?```
Thank you for helping me!

Program:
Code:
```import java.util.Scanner;

(public class MatriceCarree
private int [] [] tab;

public MatriceCarree () (
this (3);
)

public MatriceCarree (int N) (
this.TAB = new int [N] [N];
)

public void input () (
Scanner sc = new Scanner (System.in);
System.out.println ( "Beginning of the seizure");
for (int i = 0; i <this.TAB.length i + +)
for (int j = 0 j <this.TAB.length j + +) (
System.out.println ( "Enter an integer for the case of a line
+ (I + 1) + "and column" + (j + 1));
TAB [i] [j] = sc.nextInt ();
)
)

public void display () (
int count = 0;
System.out.println ("");
for (int i = 0; i <this.TAB.length i + +)
for (int j = 0 j <this.TAB.length j + +) (
compt + +;
System.out.print (TAB [i] [j] + "");
if (% this.TAB.length compt == 0)
System.out.println ("");
)
)

public MatriceCarree transposed () (
MatriceCarree Rep MatriceCarree = new ();
for (int i = 0; i <this.TAB.length i + +)
for (int j = 0 j <this.TAB.length j + +)
Rep.TAB [i] [j] = tab [j] [i];
Rep return;
)

public MatriceCarree sum (MatriceCarree C) (
MatriceCarree REP MatriceCarree = new ();
for (int i = 0; i <this.TAB.length i + +)
for (int j = 0 j <this.TAB.length j + +)
REP.TAB [i] [j] = C. TAB [i] [j] + this.TAB [i] [j];
REP return;
)

public MatriceCarree product (MatriceCarree C) (
MatriceCarree REP MatriceCarree = new ();

for (int i = 0; i <this.TAB.length i + +)
for (int j = 0 j <this.TAB.length j + +)
for (int k = 0 k <this.TAB.length; k + +)
REP.TAB [i] [j] = (C. TAB [i] [k] * this.TAB [k] [j])
+ REP.TAB [i] [j];
REP return;
)

public boolean estSymetrique () (
for (int i = 0; i <this.TAB.length i + +)
for (int j = 0 j <this.TAB.length j + +)
if (this.TAB [i] [j]! this.TAB = [j] [i])
return false;
return true;
)

public int [] minLignes () (
int [] rep = new int [this.TAB.length];
for (int i = 0; i <this.TAB.length i + +)
for (int j = 0 j <this.TAB.length j + +)
if (this.TAB [i] [j]> this.TAB [i +1] [j +1])
REP [i] = TAB [i] [j];
REP return;
)

public void affichagetableau (int [] tab) (
for (int i = 0; i <tab.length i + +)
System.out.println (tab [i]);

)
)```
#2
17-04-2009
 Member Join Date: Apr 2008 Posts: 2,001
Re: Function that returns an array

Ta minLignes function () te returns an array of integer. It means that you recover quanq you call this function:
int [] result = M.minLignes ();

I think that for the display of values of the result, you must know because you've already done something similar in your program.
#3
17-04-2009
 Member Join Date: Oct 2008 Posts: 27
Re: Function that returns an array

Hello and thank you for help.

Code:
`int [] result = M.minLignes ();`
to store the table result.

Code:
```MatriceCarree M = new MatriceCarree (3);

M.saisie ();

M.affichage ();

int [] result = M.minLignes ();

for (int i = 0; i <3; i + +)

System.out.println (result [i]);```
But this returns me an error:

...

Enter an integer for the case of row 3 and column 1

2

Enter an integer for the case of row 3 and column 2

2

Enter an integer for the case of row 3 and column 3

2

2 2 2

2 2 2

2 2 2

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3

at MatriceCarree.minLignes (MatriceCarree.java: 76)

at TestMatrice.main (TestMatrice.java: 27)

#4
17-04-2009
 Member Join Date: Oct 2008 Posts: 27
Re: Function that returns an array

Hey, there it is, there is an error, it's just that my function should return the smallest number of each row of the matrix that is false ...

If someone has an idea!

Thank you

Code:
```public int [] minLignes () (

int [] rep = new int [this.TAB.length];

for (int i = 0; i <this.TAB.length i + +)

for (int j = 0 j <this.TAB.length j + +)

if (this.TAB [i] [j] == this.TAB [i] [j]) / / Here it is false ...

REP [i] = TAB [i] [j];

REP return;

)```

 Tags: