# Thread: [SOLVED] Help with Matrix

1. Member
Join Date
Dec 2008
Posts
10
Rep Power
0

## [SOLVED] Help with Matrix

Ok, i recieved a basic program to build which required me to build a matrix of a size chosen by the user and with his inputs of the numbers.I have to then find the third smallest value. For some reason i can't seem to get that third number. This is my program, one of my problems is that it doesnt really work right and 2 it carries the initialisation problem. ( Its a french class so ignore the french words;) )

import java.util.Scanner;

public class Laboratoire3s
{

public static void main(String[] args)
{
int i,x,y;
double t1,t2,t3;
Scanner scan = new Scanner(System.in);

System.out.println("Entrez le nombre de case dans votre tableau");
double[] notes = new double[scan.nextInt()];
t1=notes[0];
t2=notes[0];
t3=notes[0];
System.out.println("Maintenant, entrez les entier destine a remplire votre tableau");
for (i=0;i<notes.length;i++)
notes[i]=scan.nextInt();
y=0;
x=0;

for (i=0;i<notes.length;i++)
{
if (t1>notes[i]);
{
t1=notes[i];
x = i;
}

if (notes[i]<t2 && x != i)
{
t2=notes[i];
y = i;
}
if(notes[i]<t2 && y != i)
t3=notes[i];
}

System.out.println(t1 +" et "+ t2 +" et " + t3);

}

}

2. You have just added an empty statement here in if condition.

Java Code:
`if (t1>notes[i]);`
What's the point?

3. Member
Join Date
Dec 2008
Posts
10
Rep Power
0
its to say if the value of that piece of the matrix is smaller than the one in it already if so it will put it.

4. I'm not clear what you say. Can say what's doing on this code segment.

Java Code:
```if (t1>notes[i]);
{
t1=notes[i];
x = i;
}```

5. Member
Join Date
Dec 2008
Posts
10
Rep Power
0
This piece of code checks if the value of the case at the [i] location in the matrix is lower than the one already in t1, if it is it will deposit it into t1 as shown on t1=notes[i] the x is to memorise at which case this number was taken to prevent the two other following numbers, t2 and t3 from taking that same case as the lowest. I am very sorry about the inapropriate lingo, since i am doing this class in french the translating is rather difficult. My main problem is the initialisation of my variables, seeing as they are initialised at either case [0] or at 0 this prevents me from checking for the second a third numbers accuratly. i would really like to find a sugestion on how to sort my matrix in order to find the third lowest value. Any sugestion would be greatly appreciated.

6. Are you sure that if the condition true, things you have explain is happen. Even the condition is true or false the following two lines are executing.

Java Code:
```t1=notes[i];
x = i;```
Reason is, that if condition has an empty statement. You have added a colon at the end.

Java Code:
`if (t1>notes[i])[B];[/B]`

7. ## Code problems

Java Code:
`if (t1>notes[i]);`
Two things wrong with this:
• t1 has not been initialized and ...
• Like Eranga has said, the if shouldn't end with a ","

Luck,
CJSL

8. Our thread starter do something mess to me.

Java Code:
`t1=notes[0];`
Did you notice that Chris?

9. ## yep...

This piece of code checks if the value of the case at the [i] location in the matrix is lower than the one already in t1
The above isn't going to happen because t1 hasn't been assigned a value (as explained in my previous post).
Java Code:
```t1=notes[0];
t2=notes[0];
t3=notes[0];```
The matrix doesn't have any values when the above assignments happen, so t1, t2 & t3 are initially equal to null.

Won't something like a bubble sort work in this case? Something like:
• Fill the array with ints (not sure why it's being called a matrix when it's a 1d array)
• sort the array elements
• Print notes[3]

Does that sound right?

Luck,
CJSL

10. Member
Join Date
Dec 2008
Posts
10
Rep Power
0

## Thank you

Thank you very much, i hadnt learned the name of an Array, with it i simply used the sort array command and took the third value in the array. AGain thanks to your help i was able to fix the bugs in my program. Good day sir.

#### Posting Permissions

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