Click here to Skip to main content
15,867,756 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello.I have a problem with a C code.
I want to check if an array contains a number twice or more...

For example, I have an array with a capacity of 20.
With a for loop I give random numbers(between 1 to 20) and store them to this array.But every time the program gives a random number,I check if this number is contained to array (with a do-while).

But every time I recieve a number twice or more...
Τhe rest of the code does not matter.
Ι want help only with check_array(.....) function...

Thanks!!!

What I have tried:

Code:


int check_array(int *pinakas , int N)
{
    int checked = 1;
    for(int i = 0; i < N; i++)
    {
        for(int j = i + 1; j < N; j++)
        {
            if(*(pinakas + j) == (*(pinakas + i)) )
            {
                checked = 0;
            }
        }
    }
    checked = 1;



void init_array(int *array , int N , int a , int b)
{
    srand(time(NULL));
    for(int i = 0; i < N; i++)
    {
        do
        {
            *(array + i) = a + rand() % (b - a + 1);
        }
        while(check_array(array , N) == 0);
    }
}
Posted
Updated 22-Jul-20 10:52am
v2
Comments
Joe Woodbury 22-Jul-20 14:51pm    
Given your requirements, you may want to consider using the Fisher-Yates inside-out shuffle. (https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_%22inside-out%22_algorithm)
Shao Voon Wong 24-Jul-20 23:59pm    
I do not see any relevance of your post to OP's question but I want to thank you for the link. Just learned something useful today. Shuffling is something I do quite often in my hobbyist projects. Up to today, mostly doing the naive shuffle. Thanks again!
Joe Woodbury 25-Jul-20 0:06am    
It solves the problem he presented; he wants 20 unique random numbers in an array in the range 1 to 20.

(BTW, I got to that link after reading about over-shuffling, which I'd been doing. Here's one: https://www.i-programmer.info/programming/theory/2744-how-not-to-shuffle-the-kunth-fisher-yates-algorithm.html)

Take out this line from the end of the function:
checked = 1;

And replace it with this:
C++
return(checked);
}

Then, look at your code - it's not the check that is wrong now, it's the init_array.
Try this:
int main()
{
    printf("Main started\n");
    int arr[100];
    init_array(arr, 20, 1, 15);
    printf("Init complete\n");
    int x = check_array(arr, 20);
    printf("%u\n", x);
    return 0;
}
Run you app and look at what you get printed:
Main started
Is all you get.
So, why doesn't it exit from init_complete?
Simple: it never gets out of your do ... while loop, so i never gets increased. And because the array is initialized to zeros, it will automatically find a match every time it tries to check ...

Have a look with the debugger and see if you can see what I mean.
 
Share this answer
 
v2
C++
int check_array(int *pinakas , int N)
{
    int checked = 1;
    for(int i = 0; i < N; i++)
    {
        for(int j = i + 1; j < N; j++)
        {
            if(*(pinakas + j) == (*(pinakas + i)) )
            {
                checked = 0;
            }
        }
    }
    checked = 1;
    // And you plan to return an answer ?

    // Looks like the end of function is missing here    
 
Share this answer
 
Try a simpler approach. I am not sure why you have a nested for loop to check the array. From your description a single loop is all you need. Here is an alternative function that will check the array :
C++
int check_array( int array[], int size, int value )
{
    int found = 0;
    int index;
    for( index = 0; index < size; ++n )
    {
        if( array[ index ] == value )
        {
            found = 1;    // the value is already in the array
            break;
        }
    }
    return found;
}
This makes it simple - either the value is in the array or it is not and that's all you need from it. Change your other logic to see if the new value is in the array. If it is then ignore it and get another value. If it is not then add it to the array.
C++
void init_array( int *array , int maxSize, int minVal, int maxVal )
{
    int span = maxVal - minVal + 1;
    int count = 0;
    int newvalue = 0;

    srand( time( NULL ) );
    while( count < maxSize )
    {
        // rand() mod span will give values from 0 to 19
        // offset it to give values from 1 to 20

        newvalue = minVal + ( rand() % span );

        if( ! check_array( array, count, newvalue ) )
        {
            // the new value is not in the array so add it

            array[ count ] = newvalue;
            ++count;
        }

        // if the new value is not in the array then
        // loop back around and get another value
    }
}
 
Share this answer
 
v2
Ok .I found it.I did something else.
I initialize the array with some different values in init_array().
Code:
C++
for(int i = 0; i < N; i++)
{
    *(array + i) = i-N;
    printf("%d  ",array[i]);
}



and I fixed the problem in check_array() -> return checked (not checked = 1)
Ιt works as I expected.(without repeating random numbers)

Anyway,thank you for your help!!!
I really appreciate it...
 
Share this answer
 
Comments
Patrice T 22-Jul-20 13:50pm    
It looks like you solved the problem.
You can accept your solution, and any other useful solution. It will close the question, and reward helpers.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900