Results 1 to 11 of 11
Thread: [SOLVED] Help with Matrix
- 02-13-2009, 03:58 AM #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);
}
}
- 02-13-2009, 05:26 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
You have just added an empty statement here in if condition.
What's the point?Java Code:if (t1>notes[i]);
- 02-13-2009, 05:31 AM #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.
- 02-13-2009, 05:37 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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; }
- 02-13-2009, 05:54 AM #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.
- 02-13-2009, 07:30 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
Reason is, that if condition has an empty statement. You have added a colon at the end.Java Code:t1=notes[i]; x = i;
Java Code:if (t1>notes[i])[B];[/B]
- 02-13-2009, 12:25 PM #7
Code problems
Two things wrong with this:Java Code:if (t1>notes[i]);
- t1 has not been initialized and ...
- Like Eranga has said, the if shouldn't end with a ","
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-13-2009, 12:29 PM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Our thread starter do something mess to me.
Did you notice that Chris?Java Code:t1=notes[0];
- 02-13-2009, 01:48 PM #9
yep...
The above isn't going to happen because t1 hasn't been assigned a value (as explained in my previous post).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 matrix doesn't have any values when the above assignments happen, so t1, t2 & t3 are initially equal to null.Java Code:t1=notes[0]; t2=notes[0]; t3=notes[0];
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,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-13-2009, 06:16 PM #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.
- 02-14-2009, 02:19 AM #11
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Fine. If you have solve the problem please mark the thread solved.
Similar Threads
-
Help with dox matrix printer
By Albert in forum Advanced JavaReplies: 7Last Post: 09-06-2011, 08:50 AM -
Matrix variations with repetition
By jnovice in forum New To JavaReplies: 4Last Post: 01-19-2009, 08:42 AM -
Help with matrix
By susan in forum New To JavaReplies: 1Last Post: 08-07-2007, 04:37 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks