如果数组在偶数索引上排序,则递归检查

时间:2016-01-11 04:17:15

标签: c arrays

我正在尝试编写一个递归函数,该函数检查给定数组是否在偶数索引上向上排序。例如,我有一个size 5数组。数字为1 , 2 , 3 , 4 ,5。它将return 1,因为arr[0] < arr[2] < arr[4]

该行的问题:

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

以下是代码:

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

抛出错误:

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.

编辑:我想现在检查奇数索引是否向下并且它不起作用,这是我的代码:

我有两个问题:

  1. 为什么它不起作用?

    1. 你能帮我把这两个函数写在一个函数中吗,这意味着,它会同时检查奇数索引上的偶数和向下吗?
  2. #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
    }
    

2 个答案:

答案 0 :(得分:2)

<%= link_to image_tag(user.avatar.try(:med_thumb)), user %>
<%= link_to image_tag(user.avatar.try(:image, :med_thumb)), user %>
<%= link_to image_tag(user.avatar.try(image(:med_thumb))), user %>

答案 1 :(得分:1)

当您从SortedUpDown()中调用SortedUpDown()时,看起来您遇到了无限循环,因为在每次调用时我都设置为零。

尝试使用static来保留'i'的值;

static int i = 0;

编辑:

此外,我修复了一些问题,从fn声明开始作为指针。

int SortedUpDown(int *arr, int num);

并称之为:

checkIfSorted = SortedUpDown(arr,sizeOfArr);

以下是完整代码:

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