Hello friends,

Need help in generating a random number from an array.Once a number is generated, the number has to be deleted from the array so that only the remaining numbers can be generated from the array.And it should go on until all the numbers from the array are deleted.

Hope replies asap from you friends.

Recommended Answers

All 12 Replies

Member Avatar for iamthwee

Which part are you stuck on?

I am able to generate a random number from an array but was unable to delete the number that have already appeared once.....I was stuck at this point

Show us what code you already have. It might enlighten us on the problem you are encountering.

the problem is the erase command may not work in C.Need help in C coding

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

int pick()
{
int arr[5]={2,3,4,5,6};
int random;
srand(time(NULL));
random = arr[rand() % 5 ];
return random;
}

int main()
{
pick();
int i = 0;
while(i<5)
{
printf("random number from the array %d\n",pick());
}
getch();
}

This is code i have written and stuck up on how to delete the number that have already appeared once

sorry its the for loop in the place of while loop

for(i=0;i<5;i++) in the place of while(i<5)

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

int pick()
{
int arr[5]={2,3,4,5,6};
int random;
srand(time(NULL));
random = arr[rand() % 5 ];
return random;
}

int main()
{
pick();
int i = 0;
while(i<5)
{
printf("random number from the array %d\n",pick());
}
getch();
}

This is code i have written and stuck up on how to delete the number that have already appeared once

Not 100% sure what you are trying to do and what you mean by "delete", but this code snippet might be useful:

http://www.daniweb.com/code/snippet1034.html

Also, what "erase" command are you referring to in your earlier post. Here's an "erase" command from the vector library.

http://www.cplusplus.com/reference/stl/vector/erase.html

You would have to have a vector rather than an array to use it.

Vernondozier thanks for ur valuable info in the first link.Its informative.But the same functinality cannot be implemented in C as we are using namespace in here.Please help me out.

Hope reply from you asap.

Vernondozier thanks for ur valuable info in the first link.Its informative.But the same functinality cannot be implemented in C as we are using namespace in here.Please help me out.

Hope reply from you asap.

I think you are going to have to clarify what you mean here. One, if this is C, you're in the wrong forum. Two, if this is C, namespace is a C++, not a C, concept. Regarding the first link, it'll work in C too, with some modification. You can't use iostream, namespace, or bool in C, but the logic and the arrays would be the same and have nothing to do with namespaces. You set up a boolean array (integer array with 0 or 1 as values in C), set all elements to false/unpicked, then as you pick each element, set the flag for that index from false/unpicked to true/picked in order to prevent repeats.

Vernondozier the code i have developed with your logic is below

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <conio.h>

int random ()
{
    srand (time (NULL));

    int picked[8], i;
    for (i = 0; i < 9; i++)
         picked[i] = 0;
    
    int array[9];  
    int value;
     
    for (i = 0; i < 9; i++)
    {
        value = rand () % 9;
        if (picked[value])
            i--;  // already picked.  for-loop increments, so decrement here
        else
        {
            array[i] = value;
            picked[value] = 1; // hasn't been picked yet.  Assign to array,
                                // flag as picked.
        }
     }
     
     // display
     for (i = 0; i < 9; i++)
        printf("Values in the array are %d\n", array[i]);   
       getch();
      return value; 
}
int main()
{
     int s;
    s = random();

    printf("value=%d", s);
    getch();
    
}

But i was stuck at the point how to delete the value in the array.The next time it has entered the loop the value that has come before should not appear once again and the loop should run until it has no values in the array.

Hoping a reply as soon as possible.

Vernondozier the code i have developed with your logic is below

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <conio.h>

int random ()
{
    srand (time (NULL));

    int picked[8], i;
    for (i = 0; i < 9; i++)
         picked[i] = 0;
    
    int array[9];  
    int value;
     
    for (i = 0; i < 9; i++)
    {
        value = rand () % 9;
        if (picked[value])
            i--;  // already picked.  for-loop increments, so decrement here
        else
        {
            array[i] = value;
            picked[value] = 1; // hasn't been picked yet.  Assign to array,
                                // flag as picked.
        }
     }
     
     // display
     for (i = 0; i < 9; i++)
        printf("Values in the array are %d\n", array[i]);   
       getch();
      return value; 
}
int main()
{
     int s;
    s = random();

    printf("value=%d", s);
    getch();
    
}

But i was stuck at the point how to delete the value in the array.The next time it has entered the loop the value that has come before should not appear once again and the loop should run until it has no values in the array.

Hoping a reply as soon as possible.

Not sure what you mean by "delete". The code snippet I wrote/linked to ensures that no number is picked more than once. You could say that a number in the INTEGER array is "deleted" after it is selected by flagging it as "picked" in the BOOLEAN array, but nothing is "deleted" in the normal sense of the word.

On a separate note, you are going to have trouble here:

int picked[8], i;
    for (i = 0; i < 9; i++)
         picked[i] = 0;

When i is 8, you are assigning a value to picked[8] , which is a segmentation fault, since you only allocated storage for indexes 0 through 7.

What your code does (after you fix the segmentation fault) in your random () function is randomly populate a 9 element array with the values 0 through 8 so that each number from 0 through 8 show up once and exactly once in a random order.

Your value variable will contain array[8] at the end, so that's what be returned by the random () function. The arrays called picked and array go out of scope at the end of the random () function and are deleted automatically.

I'm not 100% sure what you are trying to do in this program and in particular, what you mean by "delete". Arrays can be resized, arrays can be deleted, but array elements are whatever is stored at a particular address, and that address is either part of the array or it isn't. Perhaps you want a container like a stack, queue, or vector where you can delete/"pop" elements, rather than an array? And again, is this C or C++?

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.