0

I'm getting various errors and warnings that I don't understand.

  • error: "subscripted value is neither array nor pointer nor vector" in the top two functions with the arrays

  • warning: passing argument 1 of ‘SelectionSort’ makes integer from pointer without a cast int s = SelectionSort(arr, len);

  • note: expected ‘int’ but argument is of type ‘int *’: void SelectionSort(int arr, int len) {

  • error: void value not ignored as it ought to be: int s = SelectionSort(arr, len);

It sounds like they're simple fixes but types and values are my weak point! Any pointers? (pun unintended)

int find_maxind(int arr, int len) {
    if(len == 1) return 0;
    int ind = find_maxind(arr,len-1);
    return (arr[ind] > arr[len-1])?ind:len-1;
}

void SelectionSort(int arr, int len) { //this cannot change
    if(len <= 1) return;
    int max_ind = find_maxind(arr,len);
    int temp = arr[max_ind];
    arr[max_ind] = arr[len-1];
    arr[len-1] = temp;
    SelectionSort(arr, len-1);
}

int main(){ //testing the sorting
  int arr[] = {1,2,3,4,5,6,7,8,9,11,13,15,17};
  int len = 13;
  int s = SelectionSort(arr, len);
}
5
  • 1
    When posting questions about build errors, please include the complete error output, unedited and in full. Also point out where in the source the errors are, for example by comments. Commented Oct 21, 2016 at 7:14
  • int find_maxind(int arr, int len) { --> int find_maxind(int arr[], int len) { Commented Oct 21, 2016 at 7:14
  • Your functions are declared to take int arr which is exactly one integer named array. You wanted to pass an array though, so do int[] arr Commented Oct 21, 2016 at 7:14
  • int s = SelectionSort(arr, len); is also wrong for obvious reasons Commented Oct 21, 2016 at 7:17
  • Um... void SelectionSort(int arr, int len) - you wanted an array there, right ? Likewise for int find_maxind(int arr, int len) ? Commented Oct 21, 2016 at 7:18

4 Answers 4

3

Complete working code.

int find_maxind(int arr[], int len) {
if(len == 1) return 0;
int ind = find_maxind(arr,len-1);

return (arr[ind] > arr[len-1])?ind:len-1;
}

void SelectionSort(int arr[], int len) { //this cannot change
     if(len <= 1) return;
     int max_ind = find_maxind(arr,len);
     int temp = arr[max_ind];
     arr[max_ind] = arr[len-1];
     arr[len-1] = temp;
     SelectionSort(arr, len-1);
}

int main(){ //testing the sorting
     int arr[] = {1,2,3,4,5,6,7,8,9,11,13,15,17};
     int len = 13;
     SelectionSort(arr, len);
}

Basically your program needs some changes:

 int find_maxind(int arr, int len) to int find_maxind(int arr[], int len)

 void SelectionSort(int arr, int len) to void SelectionSort(int arr[], int len)
  

Because you're passing arr in SelectionSort(arr, len);

I hope you got the answer.

Sign up to request clarification or add additional context in comments.

5 Comments

you were passing "arr" in SelectionSort(arr, len); so in function: 1. instead of "find_maxind(int arr, int len)", change to "int find_maxind(int arr[], int len)".
Saif, its better to include that explaination in the answer.
included explanation in answer. :)
That's much better :)
Thanks, I did try using arr[] but not for the void which I don't think I'm supposed to change. But it seems I don't have much choice. Thanks!
3

There are a couple issues here:

int find_maxind(int arr, int len) {

Is taking only one integer instead of your desired array. Change it to something like this:

int find_maxind(int[] arr, int len) {

As for your second error: Your SelectionSort function is defined to have a void return type, meaning it does not return anything. So when you try to assign its return value to s, the compiler moans:

  int s = SelectionSort(arr, len);

See, the function doesn't return anything (certainly no int), so assigning its return value to anything is nonsensical.

There are more problems, but these are the ones that your errors mention. I'd recommend you read up a little more on pointers and arrays in C before you tackle something like this. Also on return types.

Comments

2

Try the code below it is working:

#include<stdio.h>

int find_maxind(int arr[], int len) 
{
    if(len == 1) return 0;
    int ind = find_maxind(arr,len-1);
    return (arr[ind] > arr[len-1])?ind:len-1;
}

void SelectionSort(int arr[], int len) 
{ //this cannot change
    if(len <= 1) return;
    int max_ind = find_maxind(arr,len);
    int temp = arr[max_ind];
    arr[max_ind] = arr[len-1];
    arr[len-1] = temp;
    SelectionSort(arr, len-1);
}

int main(){ //testing the sorting
  int arr[] = {17,2,3,4,5,6,7,8,9,11,13,15,1};
  int len = 13;
  SelectionSort(arr, len);
  for (int i=0;i<len;i++)
  {
      printf("%d\n",arr[i]);
  }
}

You are passing an array and receiving with an int type that will surely create a problem.

You can catch the array with an array type or a pointer type but never with a primary type of variable as you are doing in your code right now.

Comments

1
  1. Your functions are not taking arrays as inputs. You are just passing integers. You should change functions find_maxind and SelectionSort to int find_maxind(int* arr, int len) and void SelectionSort(int* arr, int len)

  2. The function SelectionSort does not return anything. So in the main function, change the call from int s = SelectionSort(arr, len); to SelectionSort(arr, len);

  3. Your array arr is already sorted. I do not see the point in calling selection sort on it. You can change this array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.