I am very new to C and I am hoping for some pointers. I am attempting to take an input of 7 integers of an array and search through them to see if any number appears only once. Here is what I have so far

#include stdio.h
#define ARRSIZE 7
int main(void)
{
int intarr[ARRSIZE], i, target;
printf("Please input %d integers :", ARRSIZE);
scanf("%d", &target);
for(i = 0; i<ARRSIZE; i++)
{
    scanf("%d", &intarr[i]);
}
{
if(i < intarr[i])

Here is where I become lost. I get how to search for it. If number x appears in the array, dont count, if it appears only once, count. I am just having problems putting it into code. Anyways any help would be appreciated.

Recommended Answers

All 8 Replies

Your best bet is to sort the array (qsort() is good). Then it is easy to find duplicates since they will have to be adjacent to each other. IE (after sorting):

int lastfound = INT_MAX;
for (int i = 0; i < arraysize; i++)
{
    if (array[i] != lastfound)
    {
        lastfound = i;
    }
    else
    {
        /* We have a duplicate */
    }
}

How you deal with the duplicates is up to you. For whatever it's worth, setting lastfound to INT_MAX before the loop is just a "guard" value. It is unlikely (not impossible) that a valid value of the array COULD be the same as INT_MAX (2147483647, or 2^31 - 1).

I see what you are saying but I am attempting to do it with a more basic (and probably worse) approach. Only reason I say that is that it is an assignment and we have yet to go over the methods you've mentioned. Playing around thought I was able to construct a bit more code, however it errors when taking in the 3rd value.

#define size 7
int main(void)
{
int array[size], target, i, prev, count;
//Initialize the array
printf("Please input %d integers which are sorted in the ascending order and a target value.", size);
scanf("%d", &target);
prev = array[0];
count = 1;
for(i = 0; i<size; i++)
{
    scanf("%d", &array[i]);
    if (array[i] == prev) count++;
    else
    {
        printf("%d=%d", prev, count);
        prev = array[i];
        count = 1;
    }
}

printf("%d = %d ", prev, count);
return 0;
}

It's only 7 integers, so sorting is an extra added complexity. It is a good way, but if you can't find duplicates, you'll get lost in the sort.

Write down 7 numbers randomly on a piece of paper -- making sure to have one duplicate.

Now, looking at 2 numbers at a time, what steps will you go through to check for duplicates? Write those steps down. Now you have a preliminary 'program' you can translate into code.

Hint: Think two loops, nested (one inside the other).

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

Come to think of it, the pattern (algorithm, pseudocode) is very much like a sort. You just don't move the values around.

Walt, you are reading my mind (regarding nested loops)! :-) That would be the typical approach if you don't have a sorted array.

I honestly have no idea. I know the first for loop should be for x in array[?] but I do not know what value to put after array or what the second for loop should contain. Please bare with my complete inexperience.

If you have the numbers 3,5,9,2,6,5,1, what is the very first thing you do to see if there are duplicates?
Check the 3 & 5, right?
What's next? 3 & 9.
Next, 3 & 2.
Get the pattern?

When done with th 3, what do you do?

check 5 & 9, 5 & 2 etc

I was able to figure it out. Thank you all for the help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.