| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1060 人关注过本帖
标题:这是在一串数字中找偶数的,麻烦大家看一下,求指正
只看楼主 加入收藏
zxjbc
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2010-11-29
结帖率:90.91%
收藏
 问题点数:0 回复次数:6 
这是在一串数字中找偶数的,麻烦大家看一下,求指正
#include<stdio.h>
#define  ARR_SIZE 10
Select(int num[],int n)
{
   int i;
   for(i=0;i<n;i++)
   {
     if(num[i]%2==0)
     {
      printf("%d",num[i]);

     }
   }

}
main()
{
    int num[ARR_SIZE];
    int n,i;
    printf("please enter the total number:");
    scanf("%d",&n);
    printf("please enter the integers:");

    for(i=0;i<n;i++)
    {
      scanf("%d",&num[i]);
    }
    Select(num,n);

    printf("%2d",num[i]);

    getch();
}
搜索更多相关主题的帖子: 偶数 数字 麻烦 
2010-12-18 16:08
我菜119
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
帖 子:938
专家分:1756
注 册:2009-10-17
收藏
得分:0 
类型声明Select(int num[],int n)
void Select(int num[],int n)

愿用余生致力编程
2010-12-18 16:27
A13433758072
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:广东潮州
等 级:小飞侠
威 望:1
帖 子:1182
专家分:2784
注 册:2010-7-22
收藏
得分:0 
缺少头文件#include<conio.h> ︶︿︶ for(i=0;i<n;i++)
    {
      scanf("%d",&num[i]);
    }当n是11时你看怎么办

一步一个脚印...............................默默地前进.....
诚邀乐于解答c菜鸟问题,的热心网友加入,  QQ群38490319
2010-12-18 16:40
xzy199999
Rank: 4
等 级:业余侠客
帖 子:326
专家分:286
注 册:2010-11-19
收藏
得分:0 
select函数没返回值是怎么回事?

我是初学者
2010-12-18 18:33
roomoor
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2010-12-18
收藏
得分:0 
偶数的二进制表示中最后一位为0,利用这一点,我们可以写出更高效的程序,如下:
#include <stdio.h>
#include <stdlib.h>

#define NUM 10

void printf_even(int *arr, int count) {
    int i;

    for(i = 0; i < count; i++) {
        if(!(arr[i] & 0x01))
            printf("%d\n", arr[i]);
    }
}

int main(void) {
    int arr[NUM];
    int count = 0;

    printf("Input your integers:(Ctrl+z ended)\n");

    while(scanf("%d", arr+count) != EOF) {
        count++;
    }

    printf_even(arr, count);
    return EXIT_SUCCESS;
}
2010-12-18 18:52
roomoor
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2010-12-18
收藏
得分:0 
或者写得更加通用一点,将判断偶数的函数做成一个谓词函数,如下:
#include <stdio.h>
#include <stdlib.h>

#define NUM 10

void print(int *arr, int count,
           int (*pred)(int testee)) {
    int i;

    for(i = 0; i < count; i++) {
        if( pred(arr[i]) )
            printf("%d\n", arr[i]);
    }
}

int even(int num) {
    return (num & 0x01) == 0;
}

int main(void) {
    int arr[NUM];
    int count = 0;

    printf("Input your integers:(Ctrl+z ended)\n");

    while(count < NUM && scanf("%d", arr+count) != EOF) {
        count++;
    }

    print(arr, count, even);
    return EXIT_SUCCESS;
}
2010-12-18 18:53
roomoor
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2010-12-18
收藏
得分:0 
回复 4楼 xzy199999
在C语言中,函数返回值省缺为整型。
2010-12-18 18:57
快速回复:这是在一串数字中找偶数的,麻烦大家看一下,求指正
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015243 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved