Check recursively if array is sorted up on the even indexes

0

I'm trying to write a recursive function which checks whether the given array is sorted upwards on the even indexes. for example I have an array of size 5. and the numbers are 1 , 2 , 3 , 4 ,5. It will return 1 because arr[0] < arr[2] < arr[4].

The problem on that line:

if (i >= arr[isEven]) return 1; //Sorted

Here's the code:

#include <stdio.h>
#include <conio.h>

int SortedUpDown(int arr, int num);

int main()
{
    int sizeOfArr = 0, i = 0,*arr, num = 0, checkIfSorted = -1;
    printf("Enter the size of the array : \n");
    scanf("%d", &sizeOfArr);
    printf("Enter %d numbers to the array \n: ", sizeOfArr);
    arr = (int *)malloc(sizeOfArr * sizeof(int));
    for (i = 0; i < sizeOfArr; i++)
    {
        scanf("%d", &num);
        *(arr + i) = num;
        printf("%d ", arr[i]);
    }
    checkIfSorted = SortedUpDown(*arr,sizeOfArr);
    getch();

    return 0;
}

int SortedUpDown(int *arr, int num)
{
    int i = 0, isEven = 0;
    if (num % 2 == 0) isEven = num - 2; //if The array is even, (for example arr[6] then check 0,1,2,3,4,5 only 0,2,4)
    else isEven = num - 1; //if the array is odd, (for example arr[7] then check 0,1,2,3,4,5,6 only 0,2,4,6)
    if (i >= arr[isEven]) return 1; //Sorted
    if (arr[i] > arr[i + 2]) return 0; //Not sorted
    return i += 2, SortedUpDown(arr, num); //Advance i by 2.
}

Error thrown:

Exception thrown at 0x00AE18BC in ConsoleApplication1.exe: 0xC0000005: Access violation reading location 0x00000011.

If there is a handler for this exception, the program may be safely continued.

Edit: I want to check now if it is going downwards for odd indexes and it does not work, here's my code:

I've got 2 questions:

  1. Why it doesn't work?

    1. Can you help me write those 2 functions in one function, which means, it will check Up on even and Down on odd indexes simultanously?
#include <stdio.h>
#include <stdlib.h> 
#include <conio.h>  

int SortedDown(int *arr, int num); 
int SortedUp(int *arr, int num);

int main(void)
{
  int sizeOfArr = 0, i = 0, *arr, num = 0, checkIfSortedDown = -1, checkIfSortedUp = -1;
  printf("Enter the size of the array : \n");
  scanf("%d", &sizeOfArr);
  printf("Enter %d numbers to the array \n: ", sizeOfArr);
  arr = (int *)malloc(sizeOfArr * sizeof(int));
  for (i = 0; i < sizeOfArr; i++)
  {
      scanf("%d", arr + i);
      printf("%d ", arr[i]);
  }
  puts("");
  checkIfSortedDown = SortedDown(arr, sizeOfArr);
  checkIfSortedUp = SortedUp(arr, sizeOfArr);
  if (checkIfSortedDown && checkIfSortedUp)
      puts("Sorted");
  else
      puts("Not Sorted");
  free(arr);
  getch();

  return 0;
}

int SortedDown(int *arr, int num) {
  --num;//size to last index
  if (num % 2 != 0) //if index is not even
      --num;
  if (num <= 0)
      return 1;//Sorted
  else if (arr[num - 2] > arr[num])
      return 0;//Not sorted
  else
      return SortedDown(arr, num - 2 + 1);//+1 : last index to size
}

int SortedUp(int *arr, int num) {
  --num;//size to last index
  if (num % 2 == 0)
      --num;
  if (num <= 0)
      return 1;//Sorted
  else if (arr[num - 2] < arr[num])
      return 0;//Not sorted
  else
      return SortedUp(arr, num - 2 + 1);//+1 : last index to size
}
c
arrays
asked on Stack Overflow Jan 11, 2016 by Ilan Aizelman WS • edited Jan 11, 2016 by Ilan Aizelman WS

2 Answers

2
#include <stdio.h>
#include <stdlib.h> //need this header
#include <conio.h>  //not standard

int SortedUpDown(int *arr, int num);//prototype to match implementation 

int main(void)
{
    int sizeOfArr = 0, i = 0, *arr, num = 0, checkIfSorted = -1;
    printf("Enter the size of the array : \n");
    scanf("%d", &sizeOfArr);
    printf("Enter %d numbers to the array \n: ", sizeOfArr);
    arr = (int *)malloc(sizeOfArr * sizeof(int));
    for (i = 0; i < sizeOfArr; i++)
    {
        scanf("%d", arr + i);
        printf("%d ", arr[i]);
    }
    puts("");
    checkIfSorted = SortedUpDown(arr, sizeOfArr);//pass arr, not *arr
    if(checkIfSorted)
        puts("Sorted");
    else
        puts("Not Sorted");
    free(arr);
    getch();

    return 0;
}

int SortedUpDown(int *arr, int num){
    --num;//size to last index
    if (num % 2 != 0)
        --num;
    if(num <= 0)
        return 1;//Sorted
    else if (arr[num-2] > arr[num])
        return 0;//Not sorted
    else
        return SortedUpDown(arr, num-2+1);//+1 : last index to size
}
answered on Stack Overflow Jan 11, 2016 by BLUEPIXY
1

Looks like you run into an infinite loop when you call SortedUpDown() from within SortedUpDown() since i is set to zero on every invocation.

Try using static to preserve the value of 'i';

static int i = 0;

EDIT:

In addition I fixed a few issues starting with the fn declaration as a pointer.

int SortedUpDown(int *arr, int num);

And calling it as such:

checkIfSorted = SortedUpDown(arr,sizeOfArr);

Below is the full code:

#include <stdio.h>
#include <stdlib.h>

int SortedUpDown(int *arr, int num);

int main()
{
    int sizeOfArr = 0, i = 0,*arr, num = 0, checkIfSorted = -1;
    printf("Enter the size of the array : \n");
    scanf("%d", &sizeOfArr);
    printf("Enter %d numbers to the array \n: ", sizeOfArr);
    arr = (int *)malloc(sizeOfArr * sizeof(int));
    for (i = 0; i < sizeOfArr; i++)
    {
        scanf("%d", &num);
        *(arr + i) = num;
        printf("%d ", arr[i]);
    }
    checkIfSorted = SortedUpDown(arr,sizeOfArr);
    printf("\ndone %d\n",checkIfSorted);
    //getch();

    return 0;
}

int SortedUpDown(int *arr, int num)    
{
    static int i = 0;
    if (i >= num - 1) return 1;
    if (arr[i] > arr[i + 1]) return 0;
    return i++, SortedUpDown(arr, num);
}
answered on Stack Overflow Jan 11, 2016 by Assaf Stoler • edited Jan 11, 2016 by Assaf Stoler

User contributions licensed under CC BY-SA 3.0