I am trying to make the random generator fill my array with no duplicates. An diplay the in order but aslo first line to print array[0], then second line array[0] and array[1], so on and so on until it display all twenty on one line.


Pease help

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

void insert(int [], int);      // function prototype
bool AlreadyThere(int array[], int value, int index);	// called [],asize,call from init_call

int main()
{


  int count;
  const int MAXNUM = 20;
  int id[MAXNUM];
  int newcode;
  bool lookingForNew;
  
  

 srand(time(NULL));
  
 for(count = 0; count<MAXNUM; count ++)
 {
	lookingForNew= true;
	while(lookingForNew)
	{
		newcode = 1.0 + (double)rand()/(double)(RAND_MAX + 1) * 50.0;
	//cout<<"x"<<newcode<<endl;
		//if(AlreadyThere(id,newcode,count))
			if (id[count] == count || count == 0)
	{
		
			id[count]=newcode;			
			lookingForNew = false;

			cout<<id[0]<<id[1]<<endl;

		}
	}
	}



  insert(id,newcode);
  



  return 0;
}

void insert(int idcode[], int newcode)
{
  int i, newpos, trlpos;

  // find correct position to insert the new code
  i = 0;
  while (idcode[i] < newcode)
    i++;
  
    newpos = i;    // found the position for the new code

    // find the end of the list
    while (idcode[i] != 51)
      i++;
    trlpos = i;

    // move idcodes over one position
    for (i = trlpos; i >= newpos; i--)
      idcode[i+1] = idcode[i];

    // insert the new code
    idcode[newpos] = newcode;
  

  return;
}

Code tags added. It's code, not c, btw. -Narue

Recommended Answers

All 6 Replies

Okay, you've explained what you're trying to do and the code shows how you're going about it. However, you've neglected to tell us what you're code doesn't do that you want it to. I don't know about everyone else, but I don't have time to analyze every program that somebody posts here. I'll only answer a question if there's a precise explanation of what the problem is. Posting what you're trying to do and a bunch of code is only 2/3 of a proper question.

The program won't run it keeps giving me errors:

error LNK2001: unresolved external symbol "bool __cdecl AlreadyThere(int * const,int,int)" (?AlreadyThere@@YA_NQAHHH@Z)
Debug/Program 4 LG.exe : fatal error LNK1120: 1 unresolved externals

I am not sure how to fix it. I think it has to with the function I names, but I am not sure what else to do.

Thanks,

lululg76

>unresolved external symbol "bool __cdecl AlreadyThere(int * const,int,int)"
Well that's easy (sort of). Write the function. As it is, you declare it here:

bool AlreadyThere(int array[], int value, int index);

But you never define the body.

I have gotten my AlreadyThereFuntcion to work. The random genertor is generationg random numbers. I just don't understand why the rest of my code isn't placing the numbers in order. I don't get any errors the display is just wrong.

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

void insert(int [], int);      // function prototype

bool AllReadyThere(int *array, int value, int index)
{
   for(int j = 0; j < value; j++)
      if(array[j] == value)
         return true;
   return false;
}
int main()
{



  int count;
  const int MAXNUM = 20;
  int id[MAXNUM];
  int newcode;
  bool lookingForNew;
  
  

 srand(time(NULL));
  
 for(count = 0; count<MAXNUM; count ++)
 {
	lookingForNew= true;
	while(lookingForNew)
	{
		newcode = 1.0 + (double)rand()/(double)(RAND_MAX + 1) * 50.0;
	//cout<<"x"<<newcode<<endl;
	if(!AllReadyThere(id,newcode,count))
	{		
			id[count]=newcode;			
			lookingForNew = false;
			cout<<id[count]<<endl;
		

		}
	}
	}



  insert(id,newcode);
  AllReadyThere(int *array, int value, int index);
  return 0;
}

void insert(int idcode[], int newcode)
{
  int i, newpos, trlpos;

  // find correct position to insert the new code
  i = 0;
  while (idcode[i] < newcode)
    i++;
  
    newpos = i;    // found the position for the new code

    // find the end of the list
    while (idcode[i] != 51)
      i++;
    trlpos = i;

    // move idcodes over one position
    for (i = trlpos; i >= newpos; i--)
      idcode[i+1] = idcode[i];

    // insert the new code
    idcode[newpos] = newcode;
  

  return;
}

any help is appreciated

Read this please. I'm getting tired of fixing your attempts at code tags.

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

void insert(int [], int);      // function prototype

bool AllReadyThere(int *array, int value, int index)
{
   for(int j = 0; j < value; j++)
      if(array[j] == value)
         return true;
   return false;
   
}
int main()
{



  int count;
  const int MAXNUM = 20;
  int id[MAXNUM];
  int newcode, moves;
  bool lookingForNew;
  
  


 srand(time(NULL));
  
 for(count = 0; count<MAXNUM; count ++)
 {
	lookingForNew= true;
	while(lookingForNew)
	{
		newcode = 1.0 + (double)rand()/(double)(RAND_MAX + 1) * 50.0;
	//cout<<"x"<<newcode<<endl;
	if(!AllReadyThere(id,newcode,count))
	{		
			id[count]=newcode;			
			lookingForNew = false;
			cout<<id[count]<<endl;
		
		}


	}
	}



  insert(id,newcode);
  
  return 0;
}

void insert(int idcode[], int newcode)
{
  int i, newpos, trlpos;

  // find correct position to insert the new code
  i = 0;
  while (idcode[i] < newcode)
    i++;
  
    newpos = i;    // found the position for the new code

    // find the end of the list
    while (idcode[i] != 51)
      i++;
    trlpos = i;

    // move idcodes over one position
    for (i = trlpos; i >= newpos; i--)
      idcode[i+1] = idcode[i];

    // insert the new code
    idcode[newpos] = newcode;
  

  return;
}

Could you help me. I can't figure out why the code after return 0; isn't putting the numbers in orders.

Thanks

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.