Results 1 to 11 of 11
  1. #1
    Join Date
    Dec 2008
    Posts
    10
    Rep Power
    0

    Default [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. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

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

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

  3. #3
    Join Date
    Dec 2008
    Posts
    10
    Rep Power
    0

    Default

    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. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    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. #5
    Join Date
    Dec 2008
    Posts
    10
    Rep Power
    0

    Default

    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. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    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. #7
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default 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
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Our thread starter do something mess to me.

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

  9. #9
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default 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
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

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

    Default 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.

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Help with dox matrix printer
    By Albert in forum Advanced Java
    Replies: 7
    Last Post: 09-06-2011, 09:50 AM
  2. Matrix variations with repetition
    By jnovice in forum New To Java
    Replies: 4
    Last Post: 01-19-2009, 09:42 AM
  3. Help with matrix
    By susan in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:37 AM

Posting Permissions

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