计算给定数组中的偶数和字符数

时间:2015-10-01 10:25:17

标签: c arrays count

#include <stdio.h> 

int result=0;
void count_even(int*b, int size){
    for (int a = 0; a < size; a++){
        if(b[a] % 2 == 0){
            result++;
        }
    }
}

int main(int argc, char* argc[]){
    int data_array_1[] = {1, 3, 5, 7, 9, 11);
    int data_array_2[] = (2, -4, 6, -8, 10, -12, 14, -16};
    int data_array_2[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};

    int result_1 = count_even(data_array_1, 6);
    printf("data_array_1 has%d even numbers\n", result_1);
    int result_2 = count_even(data_array_2, 8);
    printf("data_array_2 has %d even numbers\n", result_2);
    int result_3 = count_even(data_array_3, 11);
    printf("data_array_3 has %d even numbers\n", result_3);
    return 0;
}

预期产出:

data_array_1 has 0 even numbers.

data_array_2 has 8 even numbers.

data_array_3 has 6 even numbers.

我在count_evenint result_12之后编译指向3的代码时收到错误。

如何获得预期的输出?

还有一种方法可以计算任何字符而不管其在数组中的类型(不知道它的大小和不使用strlen)吗?

例如: data_array中[] =&#34;你好&#34 ;; = 5

第二个问题的代码是

#include <stdio.h>
#include <stdlib.h>
int get_length(char*buffer))
{
int length=0;
length=sizeof(buffer)/sizeof(int);
return length;
}
int main (int argc, char* argv[])
{
char string1[] = {'h', 'e', 'l', 'l', 'o'}, '\0'};
char string2[]= "Hello";
char string3[] ="How long is this string?";
printf("%s" is %d chars long. \n", string1, get_length(string1));
printf("%s" is %d chars long. \n", string2, get_length(string2));
printf("%s" is %d chars long. \n", string3, get_length(string3));
return 0;
}

当必须填写%d的位置时,我得到的唯一答案是&#34; 1&#34;。

4 个答案:

答案 0 :(得分:4)

将方法更改为

int count_even(int*b, int size)
{
    int result = 0;
    for (int a=0; a<size; a++)
    {
        if(b[a]%2==0)
            result++;
    }
    return result;
}

您之前错误的原因

1.您的方法的返回类型为void,但您将值赋给int。所以需要改变返回类型。而不是递增全局变量使用局部变量来计算数字。

2.开闭式括号的匹配:

 int data_array_2[] = (2, -4, 6, -8, 10, -12, 14, -16};
                ------^

答案 1 :(得分:1)

一种方法是将计数返回给调用者,例如

    int count_even(int*b, int size) {
       int count = 0;
       for (int a=0; a<size; a++) {
          if(b[a]%2==0) {
             count ++;
          }
       }
       return count;
    }

...
int count_1 = count_even(data_array_1, 6);
printf("data_array_1 has%d even numbers\n", count_1);

请注意,我试试这个。这或多或少是伪代码。

答案 2 :(得分:0)

int result_1 = count_even(data_array_1,6);

count_even是一个不返回任何内容的函数,但在上面的语句中,尝试将函数count_even的返回值存储到result_1类型为int。这是不正确的陈述。

即使偶数的逻辑似乎是正确的,但它不是从函数返回的。

答案 3 :(得分:0)

检查此代码

#include <stdio.h>  


int result=0;  
int count_even(int b[], int size)
{
  int a;
  for(a=0; a<size; a++)
    {
     if(b[a]%2==0)
      {
       result++;
      }
    }

  return result;
}

int main()
{

 int data_array_1[]= {1,3,5,7,9,11};
 int data_array_2[]= {2,-4,6,-8,10,-12,14,-16};
 int data_array_3[]= {10,9,8,7,6,5,4,3,2,1,0};
 int result_1,result_2,result_3;
 result_1= count_even(data_array_1, 6);
 printf("data_array_1 has %d even numbers\n", result_1);
 result=0;
 result_2= count_even(data_array_2, 8);
 printf("data_array_2 has %d even numbers\n", result_2);
 result=0;
 result_3= count_even(data_array_3, 11);
 printf("data_array_3 has %d even numbers\n", result_3);

 return 0;
}