简体   繁体   English

在C中的数组内交换奇数和偶数

[英]swapping odd and even numbers inside an Array in C

Input array is for example Array[10] = {12,23,0,-7,138,22,7,99,10,-2} 输入数组例如是Array [10] = {12,23,0,-7,138,22,7,99,10,-2}

I want to print out the array with even numbers on one end and odd numbers on the other, something like this: Array[10] = {12,0,-2,10,22,138,-7,99,7,23} 我想打印出一端为偶数,另一端为奇数的数组,如下所示:Array [10] = {12,0,-2,10,22,138,-7,99,7,23}

int main()
{
    int N = 0, i = 0, j = N-1, index = 0;
    printf("Enter N (0>N<10): ");
    scanf("%d", &N);

    int Array[N];
    for(i=0;i<N;i++){
        printf("Enter the value number %d :", i+1);
        scanf("%d", &Array[i]);
    }
    printf("Array[%d] ={",N);
    for(i=0;i<N;i++){
        printf("%d\t", Array[i]);
    }
    printf("}");

    // swaping odd and even numbers

    int Array2[N];
    while (i < j){
        if(Array[i]%2 == 0){
            Array2[i] = Array[index];
            i++;
        }
        else{
            Array2[j] = Array[index];
            j++;
        }
        index++;
    }

    // view the new array

    printf("\n Array[%d] ={",N);
    for(i=0;i<N;i++){
        printf("%d\t", Array2[i]);
    }
    printf("}");

    return 0;
}

This doesn't seem to work. 这似乎不起作用。 Any help would be appreciated. 任何帮助,将不胜感激。

Note: I know the Array[N] part is not how it's supposed to be done, it's just to simplify things. 注意:我知道Array [N]部分不是应该怎么做的,只是为了简化事情。

In your else statement: 在您的else语句中:

else{
            Array2[j] = Array[index];
            j++;
        }

You need j--; 您需要j--; not j++ . 不是j++

In your else statement: 在您的else语句中:

if(Array[i]%2 == 0){

don't you want to inspect the item you are sorting ( Array[index] ) ? 您是否不想检查要排序的项目( Array[index] )?

At the beginning of the program in this declaration 在该程序的开头声明

int N = 0, i = 0, j = N-1, index = 0;

j is set to -1 because N is initialized by 0 . j设置为-1是因为N0初始化。

So this loop 所以这个循环

while (i < j){
//...

will be iterate never independing on whether it makes any sense.:) 永远不会依赖于是否有意义将进行迭代。:)

As for the loop then it does not swap even and odd numbers. 至于循环,则不交换偶数和奇数。

If I have understood your approach correctly you need something like the folloing. 如果我正确理解了您的方法,那么您需要像下面这样的东西。 You can modify the demonstrative program such a way that the values of elements of the array will be enetered by the user. 您可以修改演示程序,使用户可以确定数组元素的值。

#include <stdio.h>

#define N   10

int main( void )
{
    int a[N] = { 12, 23, 0, -7, 138, 22, 7, 99, 10, -2 };
    int i, j;

    printf( "Array[%d] = { ", N );
    i = 0;
    do
    {
        printf( "%d", a[i] );
    } while ( ++i < N && printf( ", " ) );
    printf( " };\n");

    i = 0; j = N;
    while ( i != j )
    {
        if ( a[i] % 2 == 0 )
        {            
            ++i;
        }
        else if ( a[--j] % 2 == 0 )
        {
            int tmp = a[j];
            a[j] = a[i];
            a[i] = tmp;
        }
    }

    printf( "Array[%d] = { ", N );
    i = 0;
    do
    {
        printf( "%d", a[i] );
    } while ( ++i < N && printf( ", " ) );
    printf( " };\n");

    return 0;
}

The program output is 程序输出为

Array[10] = { 12, 23, 0, -7, 138, 22, 7, 99, 10, -2 };
Array[10] = { 12, -2, 0, 10, 138, 22, 7, 99, -7, 23 };

Dry run it, and you will understand the concept 空运行它,您将了解概念

#include <stdio.h>
int main()
{
    int arr[10];
    int i,temp;
    printf("Enter elements into arry\n");
    for(i=0;i<=9;i++)
    {
        printf("Element #%d-->",i);
        scanf("%d",&arr[i]);
    }
        for(i=0;i<=9;i=i+2)
        {
            if((arr[i]%2)!=0)
            {
                if((arr[i+1]%2)!=0)
                {
                    arr[i]=arr[i];
                    arr[i+1]=arr[i+1];
                }
                else if((arr[i+1]%2)==0)
                {
                    temp=arr[i];
                    arr[i]=arr[i+1];
                    arr[i+1]=temp;
                }
            }
            else
            {
                if((arr[i+1]%2)==0)
                {
                    arr[i]=arr[i];
                    arr[i+1]=arr[i+1];
                }
                else if((arr[i+1]%2)!=0)
                {
                    temp=arr[i];
                    arr[i]=arr[i+1];
                    arr[i+1]=temp;
                }
            }
        }
        for(i=0;i<=9;i++)
    {
        printf("Element #%d-->%d\n",i,arr[i]);
    }

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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