简体   繁体   中英

Given array, on even indexes make sure it goes up, on odd indexes down

Given an array 2 18 3 15 3 9 7 4 I want to check recursively, with one function, if the array is going up on the even indexes, and down on the odd indexes. (by the way, this array is sorted, and my code returns false on that)

What I have done:

I've created a funtion which checks if the array is going up on the even indexes and it is working. I've created another function which check if the array is going down on the odd indexes and it is not working (returning zero instead of one).

What I want to achieve:

[Solved] 1. I'd like to get help on getting the second function to work, it's really odd that it does not work because it is similar to the first function.

On this function, I've changed this line if (num <= 0) to this if (num <= 1) . and it is sorted now.

  1. I want to make just one function, called for eg SortedUpDown which will check simultaneously recursively if the array is going up on odd and down on even indexes.

And here is my full code:

#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
}

Here is my solution to the second part of the question:

int SortedUpDown(int * arr, int num) {
  if (num < 3) {
    return 1; // it's always sorted 
  }

  if (arr[0] > arr[2]) {
    return 0;
  }

  if (num > 3 && arr[1] < arr[3]) {
    return 0;
  }
  return SortedUpDown(arr + 2, num - 2);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM