I can't sort out Insertion Sort!

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

Join Date: Mar 2008
Posts: 7
Reputation: melystar is an unknown quantity at this point 
Solved Threads: 0
melystar melystar is offline Offline
Newbie Poster

I can't sort out Insertion Sort!

 
0
  #1
Mar 30th, 2008
Hello, I'm a newbie to C. I'm trying to understand a source file about insertion sort and shell's sort, but when I try to run the programme on Microsoft VS C++ expressed edition, it always comes out with this error message!

sort_main.obj : error LNK2019: unresolved external symbol _insertion_sort referenced in function _main
C:\Documents and Settings\melystar\My Documents\Visual Studio 2008\Projects\sorting\Debug\sorting.exe : fatal error LNK1120: 1 unresolved externals

I have no idea what it means, could someone please explain to me?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,833
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: I can't sort out Insertion Sort!

 
0
  #2
Mar 30th, 2008
Does it compile on another compiler? Could be some specific option in Visual C++ that needs to be changed in the linker or whatever. Hard to say. Or it could be in the code itself. Posting the code would help. People (i.e. me) can try to compile it and see if they get the same results.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 7
Reputation: melystar is an unknown quantity at this point 
Solved Threads: 0
melystar melystar is offline Offline
Newbie Poster

Re: I can't sort out Insertion Sort!

 
0
  #3
Mar 30th, 2008
  1. #define _CRT_SECURE_NO_DEPRECATE
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. #include "sort.h"
  7.  
  8.  
  9. int main(void)
  10. {
  11. int n;
  12. int *A = NULL, *A_copy = NULL;
  13. int sort_ascending, random;
  14. work_counter work;
  15.  
  16. int i;
  17.  
  18. /* Read in the sequence length */
  19. printf("Enter the length of the sequence: ");
  20. scanf("%d", &n);
  21.  
  22. /* Allocate memory for arrays */
  23. A = (int *)malloc(n*sizeof(int));
  24. A_copy = (int *)malloc(n*sizeof(int));
  25. if ((A == NULL) || (A_copy == NULL))
  26. {
  27. printf("Not enough memory available\n");
  28. exit(EXIT_FAILURE);
  29. }
  30.  
  31. /* Determine sequence type */
  32. printf("Enter 0 for a given seqence, 1 for a random sequence: ");
  33. scanf("%d", &random);
  34.  
  35. if (random)
  36. {
  37. /* Generate random sequence */
  38. printf("Generating random integers in range [%d,%d]\n", 0, RAND_MAX);
  39. for (i = 0; i < n; i++)
  40. {
  41. A[i] = rand();
  42. A_copy[i] = A[i];
  43. }
  44. }
  45. else
  46. {
  47. /* Read in given sequence */
  48. printf("Enter given sequence\n");
  49. for (i = 0; i < n; i++)
  50. {
  51. scanf("%d", &(A[i]));
  52. A_copy[i] = A[i];
  53. }
  54. }
  55.  
  56. /* Determine sequence ordering */
  57. printf("Enter 0 to sort descending, 1 to sort ascending: ");
  58. scanf("%d", &sort_ascending);
  59.  
  60. /* Output original sequence */
  61. printf("*** Original sequence\n");
  62. printf("A = [ ");
  63. for(i = 0; i < n; i++)
  64. printf("%d ",A[i]);
  65. printf("]\n");
  66. printf("\n");
  67.  
  68. /* Perform insertion sort */
  69. printf("*** Insertion sort\n");
  70.  
  71. work.comparisons = 0;
  72. work.swaps = 0;
  73.  
  74. insertion_sort(n,A,1,sort_ascending,&work);
  75.  
  76. printf("A = [ ");
  77. for(i = 0; i < n; i++)
  78. printf("%d ",A[i]);
  79. printf("]\n");
  80. printf("comparisons = %d, swaps = %d\n", work.comparisons, work.swaps);
  81. printf("\n");
  82.  
  83. /* Perform Shells sort */
  84.  
  85. /* Free allocated memory */
  86. free(A); A = NULL;
  87. free(A_copy); A_copy = NULL;
  88.  
  89. return 0;
  90. }
Thanks very much!
Last edited by Narue; Mar 30th, 2008 at 8:58 am. Reason: Added code tags, please do it yourself next time.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 7
Reputation: melystar is an unknown quantity at this point 
Solved Threads: 0
melystar melystar is offline Offline
Newbie Poster

Re: I can't sort out Insertion Sort!

 
0
  #4
Mar 30th, 2008
And this is the sort.h file:
  1. /* Create an operations counter variable type */
  2. struct work_counter_struct
  3. {
  4. int comparisons;
  5. int swaps;
  6. };
  7.  
  8. typedef struct work_counter_struct work_counter;
  9.  
  10.  
  11. /*
  12.  * Shells sort routine
  13.  */
  14. void shells_sort(int n, int *A, int sort_ascending, work_counter *work);
  15.  
  16.  
  17. /*
  18.  * Insertion sort routine
  19.  */
  20. void insertion_sort(int n, int *A, int inc, int sort_ascending, work_counter *work);
Last edited by Narue; Mar 30th, 2008 at 8:58 am. Reason: Added code tags
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,833
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: I can't sort out Insertion Sort!

 
0
  #5
Mar 30th, 2008
Do you have the implementation code for the functions in sort.h? It needs that file too in order to compile.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 7
Reputation: melystar is an unknown quantity at this point 
Solved Threads: 0
melystar melystar is offline Offline
Newbie Poster

Re: I can't sort out Insertion Sort!

 
0
  #6
Mar 30th, 2008
It says on the exercise that I have to make a sort.c file that implements the insertion sort algorithm. So does the new sort.c have anything to do with the sort_main.c file? The sort_main.c file is the big block of code that I typed out.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,833
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: I can't sort out Insertion Sort!

 
0
  #7
Mar 30th, 2008
Well there's your problem. If sort.c doesn't exist yet, then you can't call these two functions yet:

  1. void shells_sort(int n, int *A, int sort_ascending, work_counter *work);
and
  1. void insertion_sort(int n, int *A, int inc, int sort_ascending, work_counter *work);
You declare these functions in your sort.h file, which is fine, but that's not enough to allow you to call them from sort_main.c, as you do here:
  1. insertion_sort(n,A,1,sort_ascending,&work);
No such function exists yet. All your sort.h file does is alert the compiler that such a function may be out there so that it doesn't just give an error when it sees that call in sort_main.c. It'll still give you an error if you call it without the actual function existing. Add this file to your project in Visual C++:

  1. // Filename : sort.c
  2. #include "sort.h"
  3.  
  4.  
  5. void shells_sort(int n, int *A, int sort_ascending, work_counter *work)
  6. {
  7. }
  8.  
  9.  
  10. void insertion_sort(int n, int *A, int inc, int sort_ascending, work_counter *work)
  11. {
  12. }

See if that makes the project compile. They are empty functions that do nothing, but they would at least exist now, so hopefully that'll be enough to make the project at least compile.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 7
Reputation: melystar is an unknown quantity at this point 
Solved Threads: 0
melystar melystar is offline Offline
Newbie Poster

Re: I can't sort out Insertion Sort!

 
0
  #8
Mar 30th, 2008
Oh! That clears up a lot of things ^^ Could you have a look at this code that I wrote?
  1. #include <stdio.h>
  2.  
  3. void insertion_sort(int A[6], int n)
  4. {
  5. int i, j, key;
  6.  
  7. for(j = 2; j<n; j++)
  8. {
  9. key = A[j];
  10. i = j-1;
  11. while((i > 0) && (A[i] > key));
  12. {
  13. A[i + 1] = A[i];
  14. i=i-1;
  15. }
  16. A[i+1]=key;
  17. }
  18.  
  19. for(key = 0; key < n; key++)
  20. {
  21. printf("%d", A[key]);
  22.  
  23. }
  24. }
  25.  
  26. int main(void)
  27. {
  28. int A[6]={5,2,4,6,1,3};
  29. int n=6;
  30.  
  31. insertion_sort(A,n);
  32. }
It compiles but does not print anything, even though I did include a print statement.
Last edited by Narue; Mar 30th, 2008 at 8:59 am. Reason: Added code tags
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,802
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: 747
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: I can't sort out Insertion Sort!

 
0
  #9
Mar 30th, 2008
>while((i > 0) && (A[i] > key));
Remove the semicolon. What you have right now is a loop that either doesn't run at all, or runs forever. Here's the equivalent loop with braces:
  1. while((i > 0) && (A[i] > key))
  2. {
  3. ;
  4. }
Also, your algorithm is incorrect, but I'll give you a chance to fix it on your own first.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 7
Reputation: melystar is an unknown quantity at this point 
Solved Threads: 0
melystar melystar is offline Offline
Newbie Poster

Re: I can't sort out Insertion Sort!

 
0
  #10
Mar 30th, 2008
Thanks, I managed to figure it out before I went back on the forums. Sorry if I broke any rules. I thought that if I did include the code which I tried, it wouldn't be considered cheating...
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC