Please check my code, it has no error but there is something wrong.. I am trying to generate unique ten numbers from 0-99. then insert/store the generated number in an array(check[]) because later, after filling the array (check[]) with ten numbers, I am going to use it to another fuction with its ten unique elements. This is a part of my battleship game assignment.

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

int generator ();

char comp [99];
char user [99];
int check [];

void main (){
clrscr ();
generator ();
getch ();
}

int generator () {
int cIndex, i, j , num;
srand((int)time(NULL));

for (i=0; i<=10 ; i++)  {
cIndex = rand()%99+0;

	 for(j=0; j<=9 ; j++)  {
	   if (cIndex!=check[j])  {
	       check[j]=check[cIndex];
	   }
	   else  {
		;;
 	   }

	 }

  }/*ENDFOR*/

printf ("%d",check[cIndex]);

}

Also, for the benefit of guys like me, could someone explain a bit about rand() function, its usage and general info about it?

PS: I havent come across this function in the books I have read about C thus far...

Please check my code, it has no error but there is something wrong.. I am trying to generate unique ten numbers from 0-99. then insert/store the generated number in an array(check[]) because later, after filling the array (check[]) with ten numbers, I am going to use it to another fuction with its ten unique elements. This is a part of my battleship game assignment.

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

int generator ();

char comp [99];
char user [99];
int check [];

void main (){
clrscr ();
generator ();
getch ();
}

int generator () {
int cIndex, i, j , num;
srand((int)time(NULL));

for (i=0; i<=10 ; i++)  {
cIndex = rand()%99+0;

	 for(j=0; j<=9 ; j++)  {
	   if (cIndex!=check[j])  {
	       check[j]=check[cIndex];
	   }
	   else  {
		;;
 	   }

	 }

  }/*ENDFOR*/

printf ("%d",check[cIndex]);

}

1. Never use void main. main returns int like this
int main(void)
{
}
2. What is the size of check[] in your code?
3. If generator() isn't returning anything then it should be void generator().
4.In the else statement what are the two ;; doing?

why are you using nested loop why dont u first fill the array with rand and make another temp array to hold the variables with same size and
do another for loop to chk if that array==temp if its = then just rand() and its better to use srand(GetTickCount()); produces much random result i think ..

This article has been dead for over six months. Start a new discussion instead.