#1
 ByteCoder 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
 kelfro 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
 ByteCoder 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
 ByteCoder 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:

 Thread Tools Search this Thread Show Printable Version Email this Page Search this Thread: Advanced Search Similar Threads for: "Function that returns an array" Thread Thread Starter Forum Replies Last Post Pass Array into a function Viensterrr Software Development 3 24-12-2009 12:55 PM Two dimensional array to a function Chrisch Software Development 3 27-11-2009 12:47 PM Function that returns a 2d array Zool Software Development 3 10-10-2009 08:46 AM How to return an array from function Rilex Software Development 3 02-10-2009 09:18 AM function that takes a 2D array as a parameter Umang Software Development 4 06-05-2009 11:59 PM

All times are GMT +5.5. The time now is 08:22 PM. 