Thread: how to compare element of array with other element

  1. #1
    Registered User
    Join Date
    Jul 2020
    Posts
    28

    how to compare element of array with other element

    let's assume array are storing five elements

    list = { 1, 2, 3,4 ,1}

    I want to compare array element as given below



    I don't have any idea how to do that ?

    1-2
    1-3
    1-4
    1-1

    2-3
    2-4
    2-1

    3-4
    3-1

    4-1

    Code:
    #include <stdio.h>
    
    
    int main()
    {
        int list [5] = {1, 2, 3, 4, 1};
        
        int i = 0; const int j = 0;
        
        for (i=1; i<5; i++)
        {
            if (list[j] == list[i])
            {
    
    
            }
            
            
        }
    
    
        return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Use nested for loops. That's where your j variable can come into play.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2020
    Posts
    28
    Quote Originally Posted by laserlight View Post
    Use nested for loops. That's where your j variable can come into play.
    OK thank you

    Code:
    #include <stdio.h>
    
    int main()
    {
        int list [5] = {1, 2, 3, 4, 1};
        
        int i = 0;  int j = 0;
        
        for (i=1; i<5; i++)
        {
          for ( j = i + 1; j < 5; j++)
          
           if (list[i] == list[j])
            {
    
    
            }
            
        }
    
    
        return 0;
    }
    given

    list = { 1, 2, 1, 2 ,4}

    I want to know which numbers is being repeated more then in one in array

    How to find out ?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It looks like you're practically there. Just print something indicating that the numbers are repeated.

    If you mean repeated more than once in that the numbers have to appear three or more times, then you should learn how to sort an array, then use the more efficient approach of first sorting the array, then detecting runs of three or more consecutive numbers that are the same.

    Alternatively, if the range of the numbers is sufficiently small (e.g., your current 1 to 4 inclusive range), then creating an array of counts such that each number corresponds to an index in the array would work: you just loop over the input and increment the corresponding count, and at the end check which numbers have counts of 3 or more.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jul 2020
    Posts
    28
    Quote Originally Posted by laserlight View Post
    It looks like you're practically there. Just print something indicating that the numbers are repeated.

    e.
    My program fails to find repeated number for list 1, 2, 1, 3, 4

    Code:
    #include <stdio.h>
    
    int main()
    {
        int list [5] = {1, 2, 1, 3, 4};
        
        int i = 0;  int j = 0;
        
        for (i=1; i<5; i++)
        {
          for ( j = i + 1; j < 5; j++)
          {
          
           if (list[i] == list[j])
            {
               printf(" Number %d is repeated \n", list[i]);
            }
          }
            
        }
    
    
        return 0;
    }

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That's because you start i at 1 instead of 0.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jul 2020
    Posts
    28
    Quote Originally Posted by laserlight View Post
    That's because you start i at 1 instead of 0.
    right
    Code:
    #include <stdio.h> 
    int main()
    {
        int list [5] = {1, 2, 1, 2, 4};
         
        int i = 0;  int j = 0;
         
        for (i=0; i<5; i++)
        {
          for ( j = i + 1; j < 5; j++)
          {
           
           if (list[i] == list[j])
            {
               printf(" Number %d is repeated in list \n", list[i]);
            }
          }
             
        }
     
     
        return 0;
    }
    Number 1 is repeated in list
    Number 2 is repeated in list

    so now I can find out number is repeated so how to find out how many time number has been appeared in list

    1 2 1 2 3
    1 two times
    2 two times

    1 2 1 3 1

    1 three times

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Refer to my post #4.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jul 2020
    Posts
    28
    Quote Originally Posted by laserlight View Post
    Refer to my post #4. and at the end check which numbers have counts of 3 or more.
    But I do not understand how to count when repeated number found

    Code:
    #include <stdio.h> int main()
    {
        int list [5] = {1, 2, 1, 2, 1};
          
        int i = 0;  int j = 0;
        
        int test = 0;
          
        for (i=0; i<5; i++)
        {
          for ( j = i + 1; j < 5; j++)
          {
            
           if (list[i] == list[j])
            {
                test ++;
               printf(" Number %d is repeated in list, %d \n", list[i], test);
               
               
            }
          }
              
        }
      
      
        return 0;
    }

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Which approach are you trying to implement? I outlined two ways:

    1. Sort then make a single pass to count consecutive numbers of the same value.

    2. Use a second array to map the numbers to their counts. In particular, you may be able to use the numbers as the indices of this second array.
    Last edited by laserlight; 08-01-2020 at 03:43 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Jul 2020
    Posts
    28
    I think I'm following the first approach. Do I need loop after the if condition to count the repeated number?

    The size of my list is small because I want to write the program just for small list. So there will be less chance of mistakes

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    With the first approach, start by sorting the array. Don't worry about the counting yet. You can use the qsort function, or implement your own sort.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Jul 2020
    Posts
    28
    Quote Originally Posted by laserlight View Post
    With the first approach, start by sorting the array. Don't worry about the counting yet. You can use the qsort function, or implement your own sort.
    I can find repeat number in array but problem with counting how many time they are repeating

    I still do not understand counting

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Suppose you had input like this:
    1 1 1 2 2 3 3 4 4 4 5
    Now, if you want to count the input, it's quite easy: you start with the first number, and set count=1. Then you keep looping over the remaining numbers, incrementing the count until you find a number that doesn't match. So, you end up with count=3, then you encounter 2. Great. You print "1 appears 3 times", reset count=1, and now you loop over the remaining elements until you find an element not equal to 2. So on and so forth.

    But what happens if you had input like this?
    1 2 5 1 4 3 4 2 1 3 4
    Clearly, you cannot use the same approach. What you could do is to use a variation of your original "check for a repeat" algorithm to count instead, but while that would work for the very first time you encounter each unique number, subsequently you'll end up printing nonsense. You could adjust your algorithm to always check the entire array instead of just the remaining elements, but then you'll end printing the same output multiple times.

    So, one way to fix this is to sort the array, then apply the consecutive matching algorithm I outlined earlier. Another way to fix this is to track the numbers that have been counted, hence the second approach in which I talked about using a second array.

    The reason why I wrote "don't worry about the counting yet" is that if you want to do the first approach, you need to be sure that you're sorting correctly first. If you try to implement the sort and count at one go, you'll probably fail, and you'll have a hard time figuring out why.

    If you prefer, you can focus on the counting first, but then you must assume that your input is sorted. In that case, only when you have the counting done right do you do the sorting.
    Last edited by laserlight; 08-02-2020 at 01:05 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    If you don't mind losing the original values in the array and there is a value that can be considered invalid then a third option is to "cross out" already counted values in the array after you've counted them. E.g. if the array is only supposed to contain positive numbers then values in the array that are already counted can be set to -1 and ignore values of -1 in your inner loop

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strange error when compare element of stracture and word
    By digioleg54 in forum C Programming
    Replies: 3
    Last Post: 10-12-2017, 03:41 PM
  2. Insert element after the last element in array in c
    By amaturequestion in forum C Programming
    Replies: 3
    Last Post: 04-09-2015, 08:29 AM
  3. Compare input with array element
    By coffee_cup in forum C Programming
    Replies: 14
    Last Post: 12-03-2012, 12:03 PM
  4. Replies: 60
    Last Post: 05-31-2010, 10:57 AM
  5. how to compare element in array
    By hlam in forum C++ Programming
    Replies: 7
    Last Post: 11-29-2003, 12:05 AM

Tags for this Thread