I am trying to get this code to work. Please help.

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2005
Posts: 9
Reputation: lulug76 is an unknown quantity at this point 
Solved Threads: 0
lulug76 lulug76 is offline Offline
Newbie Poster

I am trying to get this code to work. Please help.

 
0
  #1
Apr 5th, 2005
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
  1. #include <iostream.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <stdio.h>
  5.  
  6. void insert(int [], int); // function prototype
  7. bool AlreadyThere(int array[], int value, int index); // called [],asize,call from init_call
  8.  
  9. int main()
  10. {
  11.  
  12.  
  13. int count;
  14. const int MAXNUM = 20;
  15. int id[MAXNUM];
  16. int newcode;
  17. bool lookingForNew;
  18.  
  19.  
  20.  
  21. srand(time(NULL));
  22.  
  23. for(count = 0; count<MAXNUM; count ++)
  24. {
  25. lookingForNew= true;
  26. while(lookingForNew)
  27. {
  28. newcode = 1.0 + (double)rand()/(double)(RAND_MAX + 1) * 50.0;
  29. //cout<<"x"<<newcode<<endl;
  30. //if(AlreadyThere(id,newcode,count))
  31. if (id[count] == count || count == 0)
  32. {
  33.  
  34. id[count]=newcode;
  35. lookingForNew = false;
  36.  
  37. cout<<id[0]<<id[1]<<endl;
  38.  
  39. }
  40. }
  41. }
  42.  
  43.  
  44.  
  45. insert(id,newcode);
  46.  
  47.  
  48.  
  49.  
  50. return 0;
  51. }
  52.  
  53. void insert(int idcode[], int newcode)
  54. {
  55. int i, newpos, trlpos;
  56.  
  57. // find correct position to insert the new code
  58. i = 0;
  59. while (idcode[i] < newcode)
  60. i++;
  61.  
  62. newpos = i; // found the position for the new code
  63.  
  64. // find the end of the list
  65. while (idcode[i] != 51)
  66. i++;
  67. trlpos = i;
  68.  
  69. // move idcodes over one position
  70. for (i = trlpos; i >= newpos; i--)
  71. idcode[i+1] = idcode[i];
  72.  
  73. // insert the new code
  74. idcode[newpos] = newcode;
  75.  
  76.  
  77. return;
  78. }
Code tags added. It's code, not c, btw. -Narue
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,867
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 755
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: I am trying to get this code to work. Please help.

 
0
  #2
Apr 5th, 2005
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.
New members chased away this month: 5
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 9
Reputation: lulug76 is an unknown quantity at this point 
Solved Threads: 0
lulug76 lulug76 is offline Offline
Newbie Poster

Sorry I forgot to add the errors I am getting.

 
0
  #3
Apr 5th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,867
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 755
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: I am trying to get this code to work. Please help.

 
0
  #4
Apr 5th, 2005
>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:
  1. bool AlreadyThere(int array[], int value, int index);
But you never define the body.
New members chased away this month: 5
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 9
Reputation: lulug76 is an unknown quantity at this point 
Solved Threads: 0
lulug76 lulug76 is offline Offline
Newbie Poster

Ok I have gotten most of the code to work.

 
0
  #5
Apr 5th, 2005
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.
  1. #include <iostream.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <stdio.h>
  5.  
  6. void insert(int [], int); // function prototype
  7.  
  8. bool AllReadyThere(int *array, int value, int index)
  9. {
  10. for(int j = 0; j < value; j++)
  11. if(array[j] == value)
  12. return true;
  13. return false;
  14. }
  15. int main()
  16. {
  17.  
  18.  
  19.  
  20. int count;
  21. const int MAXNUM = 20;
  22. int id[MAXNUM];
  23. int newcode;
  24. bool lookingForNew;
  25.  
  26.  
  27.  
  28. srand(time(NULL));
  29.  
  30. for(count = 0; count<MAXNUM; count ++)
  31. {
  32. lookingForNew= true;
  33. while(lookingForNew)
  34. {
  35. newcode = 1.0 + (double)rand()/(double)(RAND_MAX + 1) * 50.0;
  36. //cout<<"x"<<newcode<<endl;
  37. if(!AllReadyThere(id,newcode,count))
  38. {
  39. id[count]=newcode;
  40. lookingForNew = false;
  41. cout<<id[count]<<endl;
  42.  
  43.  
  44. }
  45. }
  46. }
  47.  
  48.  
  49.  
  50. insert(id,newcode);
  51. AllReadyThere(int *array, int value, int index);
  52. return 0;
  53. }
  54.  
  55. void insert(int idcode[], int newcode)
  56. {
  57. int i, newpos, trlpos;
  58.  
  59. // find correct position to insert the new code
  60. i = 0;
  61. while (idcode[i] < newcode)
  62. i++;
  63.  
  64. newpos = i; // found the position for the new code
  65.  
  66. // find the end of the list
  67. while (idcode[i] != 51)
  68. i++;
  69. trlpos = i;
  70.  
  71. // move idcodes over one position
  72. for (i = trlpos; i >= newpos; i--)
  73. idcode[i+1] = idcode[i];
  74.  
  75. // insert the new code
  76. idcode[newpos] = newcode;
  77.  
  78.  
  79. return;
  80. }

any help is appreciated
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,867
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 755
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: I am trying to get this code to work. Please help.

 
0
  #6
Apr 5th, 2005
Read this please. I'm getting tired of fixing your attempts at code tags.
New members chased away this month: 5
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 9
Reputation: lulug76 is an unknown quantity at this point 
Solved Threads: 0
lulug76 lulug76 is offline Offline
Newbie Poster

Sorry about that.

 
0
  #7
Apr 5th, 2005
  1. #include <iostream.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <stdio.h>
  5.  
  6. void insert(int [], int); // function prototype
  7.  
  8. bool AllReadyThere(int *array, int value, int index)
  9. {
  10. for(int j = 0; j < value; j++)
  11. if(array[j] == value)
  12. return true;
  13. return false;
  14.  
  15. }
  16. int main()
  17. {
  18.  
  19.  
  20.  
  21. int count;
  22. const int MAXNUM = 20;
  23. int id[MAXNUM];
  24. int newcode, moves;
  25. bool lookingForNew;
  26.  
  27.  
  28.  
  29.  
  30. srand(time(NULL));
  31.  
  32. for(count = 0; count<MAXNUM; count ++)
  33. {
  34. lookingForNew= true;
  35. while(lookingForNew)
  36. {
  37. newcode = 1.0 + (double)rand()/(double)(RAND_MAX + 1) * 50.0;
  38. //cout<<"x"<<newcode<<endl;
  39. if(!AllReadyThere(id,newcode,count))
  40. {
  41. id[count]=newcode;
  42. lookingForNew = false;
  43. cout<<id[count]<<endl;
  44.  
  45. }
  46.  
  47.  
  48. }
  49. }
  50.  
  51.  
  52.  
  53. insert(id,newcode);
  54.  
  55. return 0;
  56. }
  57.  
  58. void insert(int idcode[], int newcode)
  59. {
  60. int i, newpos, trlpos;
  61.  
  62. // find correct position to insert the new code
  63. i = 0;
  64. while (idcode[i] < newcode)
  65. i++;
  66.  
  67. newpos = i; // found the position for the new code
  68.  
  69. // find the end of the list
  70. while (idcode[i] != 51)
  71. i++;
  72. trlpos = i;
  73.  
  74. // move idcodes over one position
  75. for (i = trlpos; i >= newpos; i--)
  76. idcode[i+1] = idcode[i];
  77.  
  78. // insert the new code
  79. idcode[newpos] = newcode;
  80.  
  81.  
  82. return;
  83. }

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

Thanks
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 2372 | Replies: 6
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC