Thread: Searching an array for several of the same values

  1. #1
    Registered User
    Join Date
    Feb 2016
    Posts
    27

    Searching an array for several of the same values

    I have a project to submit in two days and I can't wrap my mind around something...Kindly help

    I have a variable storing a number, say int i=5

    I have an array of about 10 numbers stored and I want to check that array for values higher than int i (5)

    Lets say my array has 6, 4, 6, 7, 6, 2, 7, 1, 7, 7

    Essentially what I want to do is search the array for all the values higher than 5 and I want to retrieve those values and account for how much the numbers occur ...say my array has 3 sixes and 4 sevens, I want to be able to retrieve the array value for all the numbers higher than 5 and the count as well.
    I know there's some kind of search going on but idk what to do...
    Any help is appreciated...

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Get one thing to work at a time.

    First create a program that declares the int and the array.
    Then add in a loop that iterates through the array.
    Then add in a condition within the loop that checks to see if the value of that array slot is greater than 5.
    There are plenty of references out on the internet of how to iterate through an array.
    Once you get that down, you will start to grasp what should go next.
    Code:
    int get_random_number(void)
    {
       return 4; //chosen by fair dice roll.
                 //guaranteed to be random
    }

  3. #3
    Registered User
    Join Date
    Feb 2016
    Posts
    27
    I can loop through the array to see if any value is higher than the integer I specify. The part that I'm not getting is how I store all the larger values and how and I wanna keep track of the number of times that value appears :/

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    One common method is to use a second array to keep track of the number of occurrences for each value. When a value is found, increment that index in the second array.

    For example, if a 3 is found, increment arrayname[3].

    This approach of course assumes that valid input is always non-negative.

  5. #5
    Registered User
    Join Date
    Feb 2016
    Posts
    27
    Yes the vaild input is always non-negative in my case. But could you go back to the second array part. I lost you there.
    Thank you

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Amrita Ramnauth View Post
    But could you go back to the second array part. I lost you there.
    Let's do a quick example. We will assume the name of this second array is "count". We will also assume that valid numbers are from 0 to 9.

    Code:
    int count[10] = { 0 };    /* valid indices: 0 - 9 */
    I pull a number from my hat. It's a 4. So we increment that index:

    Code:
    count[4]++;
    I pull another number from my hat, this time 7.

    Code:
    count[7]++;
    I pull a third number from my hat - it's 4 again.

    Code:
    count[4]++;
    Now if we print out the array, we will see the values it contains:

    Code:
    count[0] = 0
    count[1] = 0
    count[2] = 0
    count[3] = 0
    count[4] = 2
    count[5] = 0
    count[6] = 0
    count[7] = 1
    count[8] = 0
    count[9] = 0
    This shows us that we've encountered two 4's, and one 7.

  7. #7
    Registered User
    Join Date
    Feb 2016
    Posts
    27
    Thank you Think I got it now.

  8. #8
    Registered User
    Join Date
    Feb 2016
    Posts
    27
    I'm trying to do as you said with running a count by checking through a second array but I'm running into some problems. Here, a card could have faces in the range 0-12. I'm setting all the values in the array as a default then checking it against a face value that was previously played by someone else to see if this player has cards higher than the one played and how much of these cards he actually has. The code is giving me some problems. Any help would be appreciated.

    Code:
    int Compare(CardPtr Hand, int faceValue){
        int arrDoubleCheck[NCARDS]; //Using a second array to keep track of the occurences of each face value
        int i,j=0; /*counters*/
        int chk; /*checker against first array*/
        for (i=0;i<NCARDS;i++){
            arrDoubleCheck[i] = 0;  //setting every card count as default 0
            chk = Hand[i].face; //keeping track of the index
            printf("\n Check %d and Hand %d, ", chk, Hand[i].face);
    
    
            if (Hand[i].face > faceValue){
                arrDoubleCheck[chk]++; //increments the value in position
            }
        }
        for (i=0;i<NCARDS;i++){
            printf("\nCounts in 2nd array: %d, ", arrDoubleCheck[i]);
        }
    
    
    return 0;
    }

  9. #9
    Registered User
    Join Date
    May 2015
    Posts
    228
    Why dont you sort the array before hand? It will make the searching process much easier. If the array is trivially small, use insertion sort.

  10. #10
    Registered User
    Join Date
    Feb 2016
    Posts
    27
    It works now. (y) Thanks

  11. #11
    Registered User
    Join Date
    Feb 2016
    Posts
    27
    But quick question, how can I clear the contents of an integer array?
    Setting it to zero won't be useful in this case because 0 sets the array to a value.
    I want to make the array empty well o.o

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 11-27-2011, 04:45 PM
  2. Using and searching structures values
    By ataias in forum C Programming
    Replies: 4
    Last Post: 12-14-2010, 09:29 AM
  3. Replies: 9
    Last Post: 08-23-2010, 02:31 PM
  4. Searching for values in a string and getting a picture
    By Se7en in forum C++ Programming
    Replies: 7
    Last Post: 06-24-2003, 04:29 AM
  5. Searching an array?
    By Munkey01 in forum C++ Programming
    Replies: 2
    Last Post: 02-21-2003, 01:29 PM

Tags for this Thread