加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

计算字符数组中的1的数量

发布时间:2020-12-16 10:35:45 所属栏目:百科 来源:网络整理
导读:我试图用递归类型程序计算表示二进制数的字符数组中的1的数量.但是,似乎我的程序只是计算数组中的字符数.我不知道我是否只是在比较错误与否,但我似乎无法找到问题 #include stdio.h# include stdlib.h#include string.h#define SIZE 20int determine (char a
我试图用递归类型程序计算表示二进制数的字符数组中的1的数量.但是,似乎我的程序只是计算数组中的字符数.我不知道我是否只是在比较错误与否,但我似乎无法找到问题

#include <stdio.h>
# include <stdlib.h>
#include <string.h>
#define SIZE 20

int determine (char array[SIZE],int count,int track );

int main()
{ 
int answer = 0; 
char input[SIZE]={"1001001"};
int count = 0;
int track = 0;

answer = determine(input,count,track);

 printf("The number of 1's is %d ",answer);

system("PAUSE");
return 0;
 }

 int determine(char array[],int track)
{   

 if (array[track] != '')
 {
    if ( array[track] == '1');
    {
         count++;
    }
    return determine(array,track = track+1);    
 }
 else 
 {
      return count;
 }
}

解决方法

if ( array[track] == '1');

应该

if ( array[track] == '1')

除掉 ;

如果你有;然后无论条件评估结果(TRUE或FALSE)计数都将被执行

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读