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++?