Robert
Robert

Reputation: 13

Calling a sort function on an array c++

I am using an insertion sort function to sort a 5000 int ascending array. when i pass the array's address to the function call, i get [Error] name lookup of 'i' changed for ISO 'for' scoping [-fpermissive]. Maybe I should use a vector but I am unfamiliar with them. Maybe I coded something wrong?

#include <iostream>
#define SIZE 5000 //array size
using namespace std;

void insertionSort(int arr[], int length) {
      int i, j, tmp;
      for (i = 1; i < length; i++) {
            j = i;
            while (j > 0 && arr[j - 1] > arr[j]) {
                  tmp = arr[j];
                  arr[j] = arr[j - 1];
                  arr[j - 1] = tmp;
                  j--;
            }
      }
}

int main() {
    int a[SIZE];
    int count = 1;

    for(int i=0; i<SIZE; i++) {
        a[i] = count;
        count++;
    }

    for(int i = 0; i < SIZE; i++) {
        printf("%d\t", a[i]);
    }

    insertionSort(&a[i], SIZE);
}

Upvotes: 1

Views: 1039

Answers (2)

Andreas DM
Andreas DM

Reputation: 10998

// insertionSort(&a[i], SIZE); Here you haven't declared i in funciton main(), only in your for loop.
And the call to insertionSort is outside of your loop.

Passing (&a)[0] and a mean the same thing.

Upvotes: 2

Michael Schmitt
Michael Schmitt

Reputation: 139

You probably want to call

 insertionSort(a, SIZE)

Upvotes: 2

Related Questions