944,221 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2495
  • C++ RSS
Apr 5th, 2005
0

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

Expand Post »
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
C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lulug76 is offline Offline
9 posts
since Apr 2005
Apr 5th, 2005
0

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

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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 5th, 2005
0

Sorry I forgot to add the errors I am getting.

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lulug76 is offline Offline
9 posts
since Apr 2005
Apr 5th, 2005
0

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

>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:
C++ Syntax (Toggle Plain Text)
  1. bool AlreadyThere(int array[], int value, int index);
But you never define the body.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 5th, 2005
0

Ok I have gotten most of the code to work.

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.
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lulug76 is offline Offline
9 posts
since Apr 2005
Apr 5th, 2005
0

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

Read this please. I'm getting tired of fixing your attempts at code tags.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 5th, 2005
0

Sorry about that.

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lulug76 is offline Offline
9 posts
since Apr 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Linefeed problem at the end of a record
Next Thread in C++ Forum Timeline: C++ tips





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC