简体   繁体   English

编写C程序以将数组中的数字排列为一系列奇数和偶数

[英]Write a C program to arrange the numbers in an array as a series of odd and even numbers

I've made an attempt to create the above program,but there's some error.Here's my program 我已经尝试创建上面的程序,但是有一些错误。这是我的程序

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

void main()
{
    int a[20],o[20],e[20],c[40],i,j,k,l,n;
    printf("enter size of array");
    scanf("%d",&n);
    printf("enter array");
    for(i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
    }
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
        {
            for(k=1;k<=(n-j);k++)
            {
                if(a[i]%2==0)
                {
                    o[j]=a[i];
                    break;
                }
        else
        {
            e[k]=a[i];
        }
        }
        }
    }
    for(i=1;i<=j;i++)
    {
        c[l]=o[i];
    }
    for(i=1;i<=k;i++)
    {
        c[l+k]=e[k];
    }
    printf("The new array is");
    for(l=1;l<=(j+k);l++)
    {
        printf(" %d ",c[l]);
    }
    getch();
}

Can anybody please help me rectify the error in the above program?Can someone also give me a few tips on how to become a good C programmer? 有人可以帮我纠正上述程序中的错误吗?有人可以给我一些如何成为一名优秀C程序员的提示吗?

The problem is probably because you never initialize the variable l . 问题可能是因为您从未初始化变量l Using an uninitialized variables yields in undefined behaviour (google that). 使用未初始化的变量会产生未定义的行为 (google that)。

Also your array indexes start at 1 as for example here: 同样,您的数组索引从1开始,例如在这里:

for (i = 1; i <= n; i++)

In C array indexes start at 0, so you should write this: 在C数组中,索引从0开始,因此您应该这样写:

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

This appies to all your other for loops. 这适用于所有其他for循环。

  • There may be other problems though. 但是可能还有其他问题。
  • Your program looks overly complicated 您的程序看起来过于复杂
  • Using variable names such as odd instead of o and even instead of e would make your program much easier to read. 使用odd而不是oeven而不是e等变量名将使程序更易于阅读。
  • Your program is poorly formatted. 您的程序格式不正确。 Correct formatting is essential to readbility 正确的格式对于可读性至关重要

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

相关问题 在 C 中编写一个程序,它将打印两个不同系列数组中的偶数和奇数。 [号码将由用户插入] - Write a program in C which will print the even numbers and the odd numbers in two different series of array. [Numbers will be inserted by users] 编写一个 C 程序,从给定的整数数组创建新数组,将所有偶数移到所有奇数之前 - Write a C program to create new array from a given array of integers shifting all even numbers before all odd numbers 在C中的数组内交换奇数和偶数 - swapping odd and even numbers inside an Array in C 在数组中交换奇数和偶数,C - swap odd and even numbers inside an Array, C 写出偶数和奇数的程序 - program that writes the even and odd numbers 想要一个 C 程序写入文件,读取它并将偶数和奇数存储在单独的文件中 - Want a C program to write into a file, read it and store even and odd numbers in a separate file 在数组中配对偶数和奇数 - Pairing even and odd numbers in an array 分配偶数和奇数的数组 - An array to assign even and odd numbers 编写程序以查找正奇数之和与小于或等于30的正偶数的乘积 - Write a program to find the sum of positive odd numbers and the product of positive even numbers less than or equal to 30 动态数组:使用C将数字分为偶数和奇数 - Dynamic Array: Separating Numbers into Even and Odd Using C
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM