gpt4 book ai didi

c - 确定数组(预定义数组)的奇数和偶数并将它们存储到 2 个单独的数组中

转载 作者:行者123 更新时间:2023-11-30 21:03:32 25 4
gpt4 key购买 nike

嗨,我正在尝试确定数组(预定义数组)的奇数和偶数并将它们存储到 2 个单独的数组中,但存在一些问题。它没有显示正确的结果。请参阅此处:

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

void main()
{
long int ARR[10]={2,5,4,7,8,0,9};
long int OAR[10], EAR[10];
int i, j = 0, k = 0, n;


for (i = 0; i <ARR[i]; i++)
{
if (ARR[i] % 2 == 0)
{
EAR[j] = ARR[i];
j++;
}
else
{
OAR[k] = ARR[i];
k++;
}
}

printf("The elements of OAR are \n");
for (i = 0; i < j; i++)
{
printf("%ld\n", OAR[i]);
}
printf("The elements of EAR are \n");
for (i = 0; i < k; i++)
{
printf("%ld\n", EAR[i]);
}
getch();
}

结果是:

The elements of OAR are
5
7
1995706722

The elements of EAR are
2
4

最佳答案

改变

for (i = 0; i <ARR[i]; i++)  
^ Wrong condition

for (i = 0; i < 7; i++)  

并将数组OAREAR初始化为0:

long int OAR[10] = {0}, EAR[10] = {0}; 

关于c - 确定数组(预定义数组)的奇数和偶数并将它们存储到 2 个单独的数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24345123/

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