# [SOLVED] Help with Matrix

• 02-13-2009, 04:58 AM
Bernard Robitaille
[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);

}

}
• 02-13-2009, 06:26 AM
Eranga
You have just added an empty statement here in if condition.

Code:

if (t1>notes[i]);
What's the point?
• 02-13-2009, 06:31 AM
Bernard Robitaille
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.
• 02-13-2009, 06:37 AM
Eranga
I'm not clear what you say. Can say what's doing on this code segment.

Code:

if (t1>notes[i]);
{
t1=notes[i];
x = i;
}

• 02-13-2009, 06:54 AM
Bernard Robitaille
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.
• 02-13-2009, 08:30 AM
Eranga
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.

Code:

t1=notes[i];
x = i;

Reason is, that if condition has an empty statement. You have added a colon at the end.

Code:

if (t1>notes[i])[B];[/B]
• 02-13-2009, 01:25 PM
CJSLMAN
Code problems
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
• 02-13-2009, 01:29 PM
Eranga
Our thread starter do something mess to me.

Code:

t1=notes[0];
Did you notice that Chris?
• 02-13-2009, 02:48 PM
CJSLMAN
yep...
Quote:

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).
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
• 02-13-2009, 07:16 PM
Bernard Robitaille
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.
• 02-14-2009, 03:19 AM
Eranga
Fine. If you have solve the problem please mark the thread solved.