gpt4 book ai didi

递归检查数组是否按偶数索引排序

转载 作者:太空宇宙 更新时间:2023-11-04 07:04:07 25 4
gpt4 key购买 nike

我正在尝试编写一个递归函数来检查给定数组是否在偶数索引上向上排序。例如我有一个 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 个函数写在一个函数中吗,也就是说,它会同时检查偶数索引上的 Up 和奇数索引上的 Down?
#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
}

最佳答案

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

关于递归检查数组是否按偶数索引排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34714120/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com