arnofrederiks
arnofrederiks

Reputation: 17

Searching an Array in C

I'm having a hard time with an exercise that goes like this:

Write a program that counts the amount of appearances of every different number in an array. The integers are numbers between 0 - 9. Take this array as an example:

int numbers[] = {1, 5, 4, 7, 1, 4, 1, 7, 5, 5, 3, 1};

create a pointer that points to the first element of the array. Loop through the array and count the amount of appearances of the pointed number. After you've counted the amount of appearances of the number 1, the pointer will point to the number 5. count the amount of appearances of 5 and so on. After you've looped a couple of times and the pointer points to 1 again (element 4 of the array) the program may not count 1 again, so you need to keep the score somewhere of the numbers you've already counted.

The output should look something like:

The amount of appearances of 1 is: 4

The amount of appearances of 3 is: 1

The amount of appearances of 4 is: 2

The amount of appearances of 5 is: 3

The amount of appearances of 7 is: 2

the code I have now counts the appearance of every element:

int main()
{   
    int getallen[] = {1, 5, 4, 7, 1, 4, 1, 7, 5, 5, 3, 1};
    int i , j, *aimedNumber, appearance = 0, arraySize = sizeof(getallen) / sizeof(int);

    for(i=0;i<arraySize;i++)                        
    {
        aimedNumber = getallen[i];                  // every loop, the pointer points to the next element

        for(j=0; j<arraySize; j++)                  // loops through the array comparing the pointer
        {
            if(getallen[j]==aimedNumber)            // if the element of the array == pointer
            {
                appearance++;                       // +1 to the appearance
            }
        }

        printf("the appearance of %i in the array is: %i\n", aimedNumber, appearance);
        appearance = 0;                             // after checking the appearance of the pointed number...
    }                                               // reset the appearance variable

    return 0;
}

But I still need to have something that checks if I already counted a number and if I did, make sure that the number will not be counted again.

Thanks in advance!

Upvotes: 0

Views: 367

Answers (5)

bruceg
bruceg

Reputation: 2513

I'd just put in another loop after you set aimedNumber but before you go into the inner loop to check the previous values of the array against aimedNumber

skip = 0;
for (j=0; j < i; j++) {
  if (getallen[j] == aimedNumber) {
    skip = 1;
    break;
  }
}
if (skip) {
  continue;
}

Upvotes: 0

machine_1
machine_1

Reputation: 4454

Your program should look something like the following :

#include <stdio.h>

int main(void)
{
    //Original array of numbers
    int numbers[] = {1, 5, 4, 7, 1, 4, 1, 7, 5, 5, 3, 1};

    //array of 10 integers to count the number of occurences
    int num_of_occ[10] = {0};

    int *ptr = numbers;

    //tmp variable to hold the value of numbers array in the loop
    int tmp ,n = 0 ;

    while( n < 12 )
    {
        tmp = *ptr;
        num_of_occ[tmp]++;
        ptr++;
        n++;
    }

    //print the occurences
    for( int n = 0 ; n < 10 ; n++ )
    {
        if( num_of_occ[n] != 0 )
        {
            printf("The ammount of appearences of %d : %d\n", n , num_of_occ[n]);
        }
    }
}

Upvotes: 0

zb22
zb22

Reputation: 3231

You will need to make another array called tempArr for example with the size of getallen array, initialize it with 10's(because the numbers in getallen array are between 0-9) and before counting the next number, scan tempArr and check if the number already exists, if it does, skip to the next number.

Upvotes: 0

Some programmer dude
Some programmer dude

Reputation: 409176

You have the limitations that the numbers in the array can only be between 0 and 9 (inclusive) and that actually makes it a lot simpler, because then you can have a "counter" array of ten integers (one each for the number in the array you "search"), and for each number in the main array you increase the corresponding value (using the number as index) in the counter array. Then simply print out the values in the counter array that are non-zero.

Something like the following

int count[10] = { 0 };  // Initialize all to zero

for (i = 0; i < arraySize; ++i)
    ++count[getallen[i]];

// Now print out all non-zero values from count,
// and the index is the number in getallen

Upvotes: 1

bznein
bznein

Reputation: 908

Since you don't seem to need the getallen array after counting, you can modify it while counting, i.e. when you are looking for all the 1s in your array, every time you read an 1 you can set it to, let's say, -1 (you know that all your number will be in the [0,9] range) so that when you get back to the outer loop, you can skip entries with values of -1

Edit: this is the answer if you have to follow that kind of behaviour described in your example; otherwise a solution with an extra array, as Joachim Pileborg's one, is better.

Upvotes: 0

Related Questions