Thread: how to compare element of array with other element

  1. #16
    Registered User
    Join Date
    Jul 2020
    Posts
    28
    Quote Originally Posted by laserlight View Post
    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.
    Ok so I am sorting array

    Code:
    #include <stdio.h>  
    int main()
    {
        int i = 0; int j = 0; 
           
        int list[5] = {1, 2, 1, 3, 4};
           
        int temp;
           
        for (i = 0; i <5; i++)
         
        {
            
             
             for (j = i+1 ; j <5; j++)
             {
                  
                 if (list[i]>list[j]) // if the one is greater then other swap value 
                 {
                     temp = list[i];
                     list[i] = list[j];
                     list[j] = temp;
                        
                 }
                  
             }
              
              printf("%d", list[i]);
        }
           
        return 0;
    }
    11234

    how to know how many time number repeats in list ?

  2. #17
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    277
    Quote Originally Posted by Djsarkar View Post
    Ok so I am sorting array

    Code:
    #include <stdio.h>  
    int main()
    {
        int i = 0; int j = 0;
          
        int list[5] = {1, 2, 1, 3, 4};
          
        int temp;
          
        for (i = 0; i <5; i++)
        
        {
            
            
             for (j = i+1 ; j <5; j++)
             {
                  
                 if (list[i]>list[j]) // if the one is greater then other swap value
                 {
                     temp = list[i];
                     list[i] = list[j];
                     list[j] = temp;
                        
                 }
                  
             }
              
              printf("%d", list[i]);
        }
          
        return 0;
    }
    11234

    how to know how many time number repeats in list ?
    Code:
    Loop I = 0 to length
     Set REPEATS counter to zero
     Loop J = I + 1 to length
      If data at J equals data at I 
       Increment REPEATS
      Otherwise
       Exit inner loop
     If REPEATS is greater than zero    
      Report/store/whatever

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Sir Galahad View Post
    Code:
    Loop I = 0 to length
     Set REPEATS counter to zero
     Loop J = I + 1 to length
      If data at J equals data at I 
       Increment REPEATS
      Otherwise
       Exit inner loop
     If REPEATS is greater than zero    
      Report/store/whatever
    There is a small but critical piece missing though: after counting with the inner loop, I must be set to J in order to skip those elements that have already been counted.
    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

  4. #19
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    277
    Quote Originally Posted by laserlight View Post
    There is a small but critical piece missing though: after counting with the inner loop, I must be set to J in order to skip those elements that have already been counted.
    Yep. Good catch.

  5. #20
    Registered User
    Join Date
    Jul 2020
    Posts
    28
    Quote Originally Posted by Sir Galahad View Post
    Code:
    Loop I = 0 to length
     Set REPEATS counter to zero
     Loop J = I + 1 to length
      If data at J equals data at I 
       Increment REPEATS
      Otherwise
       Exit inner loop
     If REPEATS is greater than zero    
      Report/store/whatever
    I guess method is for without sorting array

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Instead of guessing, why don't you go through the algorithm with unsorted input and see what you get, and then implement it and try it for an unsorted array to confirm?
    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. #22
    Registered User
    Join Date
    Jul 2020
    Posts
    28
    Quote Originally Posted by laserlight View Post
    Instead of guessing, why don't you go through the algorithm with unsorted input and see what you get, and then implement it and try it for an unsorted array to confirm?
    I have set one counter to count repeated number but it's not work as I want

    Code:
    #include <stdio.h> int main()
    {
        int list [5] = {1, 2, 1, 2, 4};
          
        int i = 0;  int j = 0; int counter = 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]);
               
               counter++;
            }
    
    
          }
           
        }
        printf("  %d \n", counter);
        counter = 0; 
        return 0;
    }
    Number 1 is repeated in list
    1
    Number 2 is repeated in list
    2
    2
    2
    2

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That's because you didn't implement the algorithm correctly. Look carefully at what should be in which loop.
    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. #24
    Registered User
    Join Date
    Jul 2020
    Posts
    28
    Quote Originally Posted by laserlight View Post
    That's because you didn't implement the algorithm correctly. Look carefully at what should be in which loop.
    I don't understand how your algorithm will implement
    I am trying to fix flow chart

    how to compare element of array with other element-img_20200803_165037-jpg

  10. #25
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Okay, try it with sorted input. Does it work?
    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. #26
    Registered User
    Join Date
    Jul 2020
    Posts
    28
    No the process shown in flowchart to find repeated number in list not to count number

  12. #27
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Sorry, I don't understand what you're trying to say.
    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. #28
    Registered User
    Join Date
    Jul 2020
    Posts
    28
    I don't understand the algorithm for non-sorted. I am having trouble counting the number that is repeated more than once. I tried so many times but still struggling

  14. #29
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Djsarkar
    I don't understand the algorithm for non-sorted.
    Do you understand the algorithm for counting repeats when the input is sorted (i.e., see posts #14, #17, and #18)?

    The algorithm for non-sorted input is easy: just sort, then apply the algorithm for sorted input.
    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

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