计算给定数组中的偶数和字符
count even numbers and characters in a given array
#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.
我在编译指向 int result_1
、2
和 3
之后指向 count_even
的代码时遇到错误。
如何获得预期的输出?
还有一种方法可以让我计算数组中的任何字符,无论其类型如何(不知道其大小且不使用 strlen)?
例如:
data_array[]="hello";
=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'}, '[=12=]'};
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 的职位必须填补时,我得到的唯一答案是“1”。
将方法更改为
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.the return 您方法的类型是无效的,但您正在将值分配给一个 int。所以需要更改 return 类型。也不要增加全局变量,而是使用局部变量来计算数量。
2.Mismatch 开闭大括号:
int data_array_2[] = (2, -4, 6, -8, 10, -12, 14, -16};
------^
一种方法是return调用者的计数,例如
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);
请注意,我没有尝试过这个。这或多或少是伪代码。
int result_1= count_even(data_array_1, 6);
count_even 是一个 return 什么都没有的函数,但是在上面的语句中试图将函数 count_even 的 return 值存储到 result_1 是 int 类型。这是不正确的说法。
即使偶数的逻辑似乎是正确的,但它不是return从函数中提取的。
#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.
我在编译指向 int result_1
、2
和 3
之后指向 count_even
的代码时遇到错误。
如何获得预期的输出?
还有一种方法可以让我计算数组中的任何字符,无论其类型如何(不知道其大小且不使用 strlen)?
例如: data_array[]="hello"; =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'}, '[=12=]'};
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 的职位必须填补时,我得到的唯一答案是“1”。
将方法更改为
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.the return 您方法的类型是无效的,但您正在将值分配给一个 int。所以需要更改 return 类型。也不要增加全局变量,而是使用局部变量来计算数量。
2.Mismatch 开闭大括号:
int data_array_2[] = (2, -4, 6, -8, 10, -12, 14, -16};
------^
一种方法是return调用者的计数,例如
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);
请注意,我没有尝试过这个。这或多或少是伪代码。
int result_1= count_even(data_array_1, 6);
count_even 是一个 return 什么都没有的函数,但是在上面的语句中试图将函数 count_even 的 return 值存储到 result_1 是 int 类型。这是不正确的说法。
即使偶数的逻辑似乎是正确的,但它不是return从函数中提取的。