locked
Comparing array elements??? RRS feed

  • Question

  • I'm trying to compare two elements in an array.. something like this..

    if (row1[3] > row2[4])
    {
       letter = 'a';
    }

    is this possable or can you please show me a better way..

    Thanks
    Monday, August 10, 2009 2:49 PM

Answers

  • pretty simple from a textbox numbers get put into an array if one element in the array is greater than the other then it put the letter in "char letter;


    int[] row1 = new int[5];
    int[] row2 = new int[6];
    char letter;

     

    if (row1[3] > row2[4])
    {
       letter = 'a';
    }


    The code you posted is alright, but you have not showed the full function. The code snippet needs to be inside a method (cannot be loosely inserted inside the class body directly).
    http://blog.voidnish.com
    • Marked as answer by Harry Zhu Monday, August 17, 2009 8:19 AM
    Monday, August 10, 2009 3:25 PM
    Moderator

All replies

  • Although you state that you are comparing two elements in an array, your code actually uses 2 different variables - row1 and row2. Unless they both refer to the same array object, you are comparing two items from 2 different arrays (and using non-matching indices).
    http://blog.voidnish.com
    Monday, August 10, 2009 2:54 PM
    Moderator
  • Yes, that will work.   Was there a particular aspect of this comparison that you were hoping would be better in some way?
    Monday, August 10, 2009 2:55 PM
  • I keep getting this error .. Invaild token 'if ' in class ..??



    Monday, August 10, 2009 3:05 PM
  • I keep getting this error .. Invaild token 'if ' in class ..??




    Post the entire function here. I am sure there's some syntactic error.
    http://blog.voidnish.com
    Monday, August 10, 2009 3:08 PM
    Moderator
  • pretty simple from a textbox numbers get put into an array if one element in the array is greater than the other then it put the letter in "char letter;


    int[] row1 = new int[5];
    int[] row2 = new int[6];
    char letter;

     

    if (row1[3] > row2[4])
    {
       letter = 'a';
    }

    Monday, August 10, 2009 3:21 PM
  • Thanks for the help .. fig out I put the code in the wrong location....LOL

    Later

    Monday, August 10, 2009 3:25 PM
  • pretty simple from a textbox numbers get put into an array if one element in the array is greater than the other then it put the letter in "char letter;


    int[] row1 = new int[5];
    int[] row2 = new int[6];
    char letter;

     

    if (row1[3] > row2[4])
    {
       letter = 'a';
    }


    The code you posted is alright, but you have not showed the full function. The code snippet needs to be inside a method (cannot be loosely inserted inside the class body directly).
    http://blog.voidnish.com
    • Marked as answer by Harry Zhu Monday, August 17, 2009 8:19 AM
    Monday, August 10, 2009 3:25 PM
    Moderator