Thread: Quick Array comparison question.

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    6

    Quick Array comparison question.

    Hi guys, I have a quick question on comparison in one array rather than two, I am not sure if it's possible but it sounds doable and I searched around and couldn't find anything.
    Basically I'm just storing values in each elements in one array sum[SIZE], and I want to compare the first element to the next, if it's the same value then return 1 else return 0, I have a feeling a for loop is involved but I just can't seem to grasp onto the concept of how to compare elements in one array.
    Hope someone can clarify the logic for me and much appreciated for any help ^.^

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    To compare the first element to the next you could do something link
    Code:
    int yourArray[SIZE];
    if(yourArray[0] == yourArray[1])
       // do something.
    You may want to check out this link: Arrays.


    Jim

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    6
    Thanks Jim!

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    To take this one step further:
    Quote Originally Posted by jimblumberg View Post
    To compare the first element to the next you could do something link
    Code:
    int yourArray[SIZE];
    if(yourArray[0] == yourArray[1])
       // do something.
    Code:
    int same( int a, int b )
    {
        return a == b;
    }
    ...
    if( same( array[0], array[1] ) )
        ...

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New at Array and Pointers , Quick Question
    By KMAN999 in forum C Programming
    Replies: 4
    Last Post: 06-20-2011, 02:20 PM
  2. Quick Question: 2D Array with char **
    By Phoenix_Rebirth in forum C Programming
    Replies: 4
    Last Post: 01-29-2009, 07:33 AM
  3. Quick Array question
    By sniperwire in forum C Programming
    Replies: 3
    Last Post: 10-20-2008, 09:17 AM
  4. A quick array question
    By Rad_Turnip in forum C Programming
    Replies: 4
    Last Post: 03-30-2006, 04:18 AM
  5. quick question about 2 dimensional array
    By kimimaro in forum C Programming
    Replies: 3
    Last Post: 03-11-2005, 10:12 AM