LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-21-2023, 03:41 AM   #1
ajiten
Member
 
Registered: Jun 2023
Posts: 377

Rep: Reputation: 4
Multiple errors on compiling quicksort program's code file in C, on Cygwin.


Have the Quicksort program, in which had to implement only the main, cmp(comparator), & selectPivotIndex functions.
Rest program functions are copied from the book: Algorithms in a nutshell, by George T. Heineman.


Have built the following code, and compiled using the :
Code:
 gcc --save-temps --verbose quicksort.c -o  quicksort
Though, once the errors subside, will use the option:
Code:
 cc -pedantiC -W -Wall -Werror quicksort.c -o quicksort

Code:
#include<stdio.h>
int partition(void **, int, int *, int, int , int);
int cmp(const void *, const void*);
void do_qsort(void **, int *, int, int);
int selectPivotIndex(void **, int int);

int main(){
	void ** vals;//array of pointers
        int total_elements;
	return do_qsort(vals, total_elements-1, int(* cmp)(const void *, const void*));
}

void do_qsort(void **ar, int right, int(*cmp)(const void *, const void *), int left, int right){
	int pivotIndex;
	if (right<= left) return;
        pivotIndex = selectPivotIndex(ar, left, right);
	pivotIndex = partition(ar, cmp, left, right, pivotIndex);
	do_qsort(ar, cmp, left, pivotIndex-1);  do_qsort(ar, cmp, pivotInex+!, right);
}

int partition(void ** ar, int(* cmp)(const void*, const void*), int left, int right, int PivotIndex){
       int idx, store; void *pivot = ar[pivotIndex]; void *tmp = ar[right]; 
       ar[right] = ar[pivotIndex];  ar[pivotIndex]= tmp;
       store = left;
       for(idx= left; idx<right;idx++){
	       if(cmp(ar[idx], pivot <=0){
	              tmp = ar[idx]; ar[idx]=ar[store]; ar[store]=tmp; store++;
	       }
       }
       tmp= ar[right];  ar[right] = ar[store];  ar[store] = tmp;

       return store;
}

int cmp(const void * a, const void *b)
{
      if ((int*)a<=(int*)b)  return 0;
      else return 1;
} 

int selectPivotIndex(void ** ar, int left, int right)
{
      int median;
      median = (left+right)/2;
      if(median %2==0) median = median +1;
      return median;
}
Have got many errors, as shown below:

Code:
quicksort.c: In function ‘main’:
quicksort.c:10:49: error: expected expression before ‘int’
   10 |         return do_qsort(vals, total_elements-1, int(* cmp)(const void *, const void*));
      |                                                 ^~~
quicksort.c:10:45: warning: passing argument 2 of ‘do_qsort’ makes pointer from integer without a cast [-Wint-conversion]
   10 |         return do_qsort(vals, total_elements-1, int(* cmp)(const void *, const void*));
      |                               ~~~~~~~~~~~~~~^~
      |                                             |
      |                                             int
quicksort.c:4:24: note: expected ‘int *’ but argument is of type ‘int’
    4 | void do_qsort(void **, int *, int, int);
      |                        ^~~~~
quicksort.c:10:16: error: too few arguments to function ‘do_qsort’
   10 |         return do_qsort(vals, total_elements-1, int(* cmp)(const void *, const void*));
      |                ^~~~~~~~
quicksort.c:4:6: note: declared here
    4 | void do_qsort(void **, int *, int, int);
      |      ^~~~~~~~
quicksort.c: At top level:
quicksort.c:13:90: error: redefinition of parameter ‘right’
   13 | void do_qsort(void **ar, int right, int(*cmp)(const void *, const void *), int left, int right){
      |                                                                                      ~~~~^~~~~
quicksort.c:13:30: note: previous definition of ‘right’ with type ‘int’
   13 | void do_qsort(void **ar, int right, int(*cmp)(const void *, const void *), int left, int right){
      |                          ~~~~^~~~~
quicksort.c:13:6: error: conflicting types for ‘do_qsort’; have ‘void(void **, int,  int (*)(const void *, const void *), int,  int)’
   13 | void do_qsort(void **ar, int right, int(*cmp)(const void *, const void *), int left, int right){
      |      ^~~~~~~~
quicksort.c:4:6: note: previous declaration of ‘do_qsort’ with type ‘void(void **, int *, int,  int)’
    4 | void do_qsort(void **, int *, int, int);
      |      ^~~~~~~~
quicksort.c: In function ‘do_qsort’:
quicksort.c:17:36: warning: passing argument 2 of ‘partition’ makes integer from pointer without a cast [-Wint-conversion]
   17 |         pivotIndex = partition(ar, cmp, left, right, pivotIndex);
      |                                    ^~~
      |                                    |
      |                                    int (*)(const void *, const void *)
quicksort.c:2:24: note: expected ‘int’ but argument is of type ‘int (*)(const void *, const void *)’
    2 | int partition(void **, int, int *, int, int , int);
      |                        ^~~
quicksort.c:17:41: warning: passing argument 3 of ‘partition’ makes pointer from integer without a cast [-Wint-conversion]
   17 |         pivotIndex = partition(ar, cmp, left, right, pivotIndex);
      |                                         ^~~~
      |                                         |
      |                                         int
quicksort.c:2:29: note: expected ‘int *’ but argument is of type ‘int’
    2 | int partition(void **, int, int *, int, int , int);
      |                             ^~~~~
quicksort.c:17:22: error: too few arguments to function ‘partition’
   17 |         pivotIndex = partition(ar, cmp, left, right, pivotIndex);
      |                      ^~~~~~~~~
quicksort.c:2:5: note: declared here
    2 | int partition(void **, int, int *, int, int , int);
      |     ^~~~~~~~~
quicksort.c:18:22: warning: passing argument 2 of ‘do_qsort’ makes integer from pointer without a cast [-Wint-conversion]
   18 |         do_qsort(ar, cmp, left, pivotIndex-1);  do_qsort(ar, cmp, pivotInex+!, right);
      |                      ^~~
      |                      |
      |                      int (*)(const void *, const void *)
quicksort.c:13:30: note: expected ‘int’ but argument is of type ‘int (*)(const void *, const void *)’
   13 | void do_qsort(void **ar, int right, int(*cmp)(const void *, const void *), int left, int right){
      |                          ~~~~^~~~~
quicksort.c:18:27: warning: passing argument 3 of ‘do_qsort’ makes pointer from integer without a cast [-Wint-conversion]
   18 |         do_qsort(ar, cmp, left, pivotIndex-1);  do_qsort(ar, cmp, pivotInex+!, right);
      |                           ^~~~
      |                           |
      |                           int
quicksort.c:13:42: note: expected ‘int (*)(const void *, const void *)’ but argument is of type ‘int’
   13 | void do_qsort(void **ar, int right, int(*cmp)(const void *, const void *), int left, int right){
      |                                     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
quicksort.c:18:9: error: too few arguments to function ‘do_qsort’
   18 |         do_qsort(ar, cmp, left, pivotIndex-1);  do_qsort(ar, cmp, pivotInex+!, right);
      |         ^~~~~~~~
quicksort.c:13:6: note: declared here
   13 | void do_qsort(void **ar, int right, int(*cmp)(const void *, const void *), int left, int right){
      |      ^~~~~~~~
quicksort.c:18:66: error: ‘pivotInex’ undeclared (first use in this function); did you mean ‘pivotIndex’?
   18 |         do_qsort(ar, cmp, left, pivotIndex-1);  do_qsort(ar, cmp, pivotInex+!, right);
      |                                                                  ^~~~~~~~~
      |                                                                  pivotIndex
quicksort.c:18:66: note: each undeclared identifier is reported only once for each function it appears in
quicksort.c:18:77: error: expected expression before ‘,’ token
   18 |         do_qsort(ar, cmp, left, pivotIndex-1);  do_qsort(ar, cmp, pivotInex+!, right);
      |                                                                             ^
quicksort.c:18:61: warning: passing argument 2 of ‘do_qsort’ makes integer from pointer without a cast [-Wint-conversion]
   18 |         do_qsort(ar, cmp, left, pivotIndex-1);  do_qsort(ar, cmp, pivotInex+!, right);
      |                                                             ^~~
      |                                                             |
      |                                                             int (*)(const void *, const void *)
quicksort.c:13:30: note: expected ‘int’ but argument is of type ‘int (*)(const void *, const void *)’
   13 | void do_qsort(void **ar, int right, int(*cmp)(const void *, const void *), int left, int right){
      |                          ~~~~^~~~~
quicksort.c:18:48: error: too few arguments to function ‘do_qsort’
   18 |         do_qsort(ar, cmp, left, pivotIndex-1);  do_qsort(ar, cmp, pivotInex+!, right);
      |                                                ^~~~~~~~
quicksort.c:13:6: note: declared here
   13 | void do_qsort(void **ar, int right, int(*cmp)(const void *, const void *), int left, int right){
      |      ^~~~~~~~
quicksort.c: At top level:
quicksort.c:21:5: error: conflicting types for ‘partition’; have ‘int(void **, int (*)(const void *, const void *), int,  int,  int)’
   21 | int partition(void ** ar, int(* cmp)(const void*, const void*), int left, int right, int PivotIndex){
      |     ^~~~~~~~~
quicksort.c:2:5: note: previous declaration of ‘partition’ with type ‘int(void **, int,  int *, int,  int,  int)’
    2 | int partition(void **, int, int *, int, int , int);
      |     ^~~~~~~~~
quicksort.c: In function ‘partition’:
quicksort.c:22:41: error: ‘pivotIndex’ undeclared (first use in this function); did you mean ‘PivotIndex’?
   22 |        int idx, store; void *pivot = ar[pivotIndex]; void *tmp = ar[right];
      |                                         ^~~~~~~~~~
      |                                         PivotIndex
quicksort.c:26:38: warning: passing argument 2 of ‘cmp’ makes pointer from integer without a cast [-Wint-conversion]
   26 |                if(cmp(ar[idx], pivot <=0){
      |                                ~~~~~~^~~
      |                                      |
      |                                      int
quicksort.c:26:38: note: expected ‘const void *’ but argument is of type ‘int’
quicksort.c:26:42: error: expected ‘)’ before ‘{’ token
   26 |                if(cmp(ar[idx], pivot <=0){
      |                  ~                       ^
      |                                          )
quicksort.c:29:8: error: expected expression before ‘}’ token
   29 |        }
      |        ^
 
Old 06-21-2023, 06:03 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,966

Rep: Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332
First of all you do not need to open a new thread for this.
Next, there is a typo here - a ) is missing:
Code:
if(cmp(ar[idx], pivot) <=0)
Your code is syntactically incorrect.
And I think that cmp should return -1, 0, 1, so your implementation is incorrect: https://www.geeksforgeeks.org/compar...of-qsort-in-c/
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Quicksort algorithm's incomplete implementation in book, in the C language. ajiten Programming 16 06-21-2023 01:58 AM
qsort vs 'Magnetica' Quicksort Sanmayce Programming 19 10-09-2022 01:19 AM
Could I get rid of Cygwin.dll/Cygwin1.dll in program compiled under Cygwin ? Kunsheng Programming 1 05-17-2009 08:30 PM
Have you ever used cygwin, i've met a problem with the command stat from cygwin andy820303 Linux - Newbie 0 03-11-2009 03:37 AM
Errors, Errors, and more Errors (KDE 3.4.x GUI Errors) Dralnu Linux - Software 2 05-13-2006 08:30 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration