Thread: Need Help software is not exactly performs the required

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    11

    Wink Need Help software is not exactly performs the required

    Hello friends I need your help.
    My program is such an array size in 1000 where the numbers should be between 0-999. These numbers should be determined randomly (rand loop) and the number must not be repeated. Would be considered the main part, I have to count how many times I used rand ().

    My idea is that one loop where it initializes all the 1000 numbers, and another loop to check whether the number appears twice, if the number appears twice is set it again until that not appear twice (maybe this is not the best way but ...)

    It is my exercise (Here I need your help)-

    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    int main()
    {
        int i, j;
        int arr[1000];
        int loop = 0;
    
        for (i = 0; i<10000; i++)
        {
            arr[i] = rand() % 1000;
            loop++;
        }
    
        for (j = 0; j<10000; j++)
        {
            if(arr[j] == arr[j + 1])
            {
                arr[i] = rand() % 1000;
                loop++;
            }
        }
    
        printf("%d\n",loop);
    }
    
    So I appreciate your help
    Thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > int arr[1000];
    > for (i = 0; i<10000; i++)
    Your first problem is massive array overruns.


    Before you start writing code, check your algorithm for finding duplicates on paper with a small array, such as

    1 2 4 1 5

    Do you think " if(arr[j] == arr[j + 1]) " is going to find the 2nd duplicate 1 in this array?


    If you want a better approach, consider filling the array with unique numbers using a for loop, then shuffle it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Just populate your array with 1000 (not 10000, as pointed out by Salem) distinct values and then randomly shuffle.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. l value required
    By anil_ in forum C++ Programming
    Replies: 16
    Last Post: 10-06-2011, 01:00 PM
  2. Software Best Practices Training topics required
    By gangesTech in forum C++ Programming
    Replies: 2
    Last Post: 08-28-2010, 11:15 PM
  3. Program performs an illegal operation
    By skytreader in forum C Programming
    Replies: 3
    Last Post: 02-18-2010, 10:11 AM
  4. Help required
    By ItsMeHere in forum C Programming
    Replies: 4
    Last Post: 06-01-2006, 08:26 AM
  5. little help required
    By Lynux-Penguin in forum C Programming
    Replies: 4
    Last Post: 04-27-2002, 01:34 PM

Tags for this Thread