how to compare element in array

This is a discussion on how to compare element in array within the C++ Programming forums, part of the General Programming Boards category; hi: I want to compare values in the 3 element array and see if they are equal, if equal display ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    10

    how to compare element in array

    hi:

    I want to compare values in the 3 element array and see if they are equal, if equal display message. The code compiled but result not expect. Can someone help

    Code:
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    int main()
    {
      int a[3]={1,2,3};
    
      for(int i=0; i<3; i++)
      {
         if(a[i] ==a[i++])
            cout<<"equal"<<endl;
         else
           cout<<"not equal"<<endl;
      }//end loop
    
      system("pause");
      return 0;
     }//end main
    RESULT: equal

    thank you

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    The increment operator, when placed AFTER the variable, only increments AFTER the variable has been used. So, that means you are comparing a[i]==a[i] then incrementing i. I would change it to this:
    Code:
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    int main()
    {
      int a[3]={1,2,3};
    
      for(int i=0; i<2; i++)
      {
         if(a[i] ==a[i+1])
            cout<<"equal"<<endl;
         else
           cout<<"not equal"<<endl;
      }//end loop
    
      system("pause");
      return 0;
     }//end main
    Note that you should only run the loop 2 times, because you can only (with this setup) compare 0 to 1, and 1 to 2.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    10
    thanks for your help. It work. But now I want to modify it so now can compare each element to see if they are all equal.

    Th code work fine but want to know way to improve it. Can you show me how to do it if possible.

    Code:
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    int main()
    {
      int a[3]={3,2,2};
      int tmp;
      bool flag=false; //stop condition for the loop
    
      for(int i=0; i<3; i++)
      {  tmp=a[0];
         if(a[i]== tmp && a[i+1]==tmp)
            flag=true;
         else
            flag= flag;
      }//end loop
    
      if(flag==true)
        cout<<"equal"<<endl;
      else
        cout<<"not equal"<<endl;
    
      system("pause");
      return 0;
     }//end main
    thanks

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,157
    Premise: if the first element is equal to all the other elements in the array, then all the elements in the array are equal.

    Code:
    bool flag = false;
    //loop through the array comparing the other elements with the first
    for (int i = 1; i < 3; i++) {
    	if (a[0] != a[i]) {
    		flag = true;
    		break;
    	}
    }
    //now if flag is still false, all elements are equal

  5. #5
    Registered User
    Join Date
    Nov 2003
    Posts
    10

    compare 3 by 3 array

    hi:

    I would like to compare each row in an n by 3 array to see if each element in first row is equal to next row ie
    Code:
    2 2 2            if  not equal, show the row have the same element.
    3 5 4
    4 6 8
    I have the algorithm for it but not sure am i on the right track or not. Any help will do

    ALGORITHM
    1 read in the rows and columns using a for loop
    2 extract each row in the array
    for(int row=0; row<(n/n); row++)......
    ...
    I don't know what to do then. any idea

    thanks

  6. #6
    Registered User
    Join Date
    Nov 2003
    Posts
    10

    compare n by 3 array

    hi:

    I would like to compare each row in an n by 3 array to see if each element in first row is equal to next row ie


    code:--------------------------------------------------------------------------------
    2 2 2 if not equal, show the row have the same element.
    3 5 4
    4 6 8
    --------------------------------------------------------------------------------

    I have the algorithm for it but not sure am i on the right track or not. Any help will do

    ALGORITHM
    1 read in the rows and columns using a for loop
    2 extract each row in the array
    for(int row=0; row<(n/n); row++)......
    ...
    I don't know what to do then. any idea

    thanks

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    27
    try something along the lines of:
    Code:
    for(int count=0; count<n; count++)
    {
        if (array[count, n]==array[count++, n])
            cout<<"Equal! :)";
        else
              cout<<"Not equal :(";
    }
    i havent tested this so someone correct me if it wouldnt work.
    "Computers aren't intelligent, they only think they are."

    **infected by Blizzarddog**
    I am a signature virus. Please add me to your signature so that I may multiply

  8. #8
    Registered User
    Join Date
    Nov 2003
    Posts
    177

    Smile

    hello hlam .
    I didnt undrestand what you want from the program .
    do you want to compare just the firt row with other rows or each row with the uper ?
    are the firt rows elements equal?
    and also
    you want to compare elements that are in same colomn or each elements with all elements that are in the downer row ?


    --------------------------------------------------------------------------------

    Bingo the clown
    the code you wrote just compare the last elements in the rows .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 09:32 AM
  2. count/display non-repeated element of array
    By azsquall in forum C++ Programming
    Replies: 15
    Last Post: 07-10-2008, 10:42 AM
  3. Replies: 19
    Last Post: 07-20-2007, 02:46 AM
  4. Sorting a 2-dimensional array
    By kmoyle73 in forum C++ Programming
    Replies: 3
    Last Post: 05-05-2004, 02:54 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21