【问题标题】:How do I find the even numbers on the line into C program如何在C程序中找到行中的偶数
【发布时间】:2018-08-25 18:30:52
【问题描述】:

我有一个练习要做 我需要在矩阵中找到我有偶数的哪一行 一行中的所有数字都应该是双数

例如:在矩阵中matrix[R][C]={{8,1,2},{3,7,5},{6,2,14},{13,8,15},{8,0,2,},{4,50,26},{2,84,11},{12,36,9}};
示例输出:2,4,5

这就是我所做的,我必须用指针来做,我做错了什么? 在我得到的输入上:0,1,2

#define R 8
#define C 3   
void *ckeckme(int **m)
{
    int **p,j,i;

    for(p=m;p-m<C;p++){
    if(isEven(&p)==1)
    printf("%d",p-m);
    }
}

int isEven(int *v)
{
    int *ptr,conut=0,cc=0;
    int i,j;                                        
    for(j=0;j<R;j++)
    for(i=0; i<C; i++){

        if(*(v + i+ j)%2==0)
                conut++;

    if(conut==3)
    {

    return 1;
    conut=0;
    }
}

}    
int main() {

int x,i,j,matrix[R][C]={    {8,1,2},
                    {3,7,5},
                    {6,2,14},
                    {13,8,15},
                    {8,0,2},
                    {4,50,26},
                    {2,84,11},
                    {12,36,9}};
    *ckeckme(matrix);

}    

【问题讨论】:

  • 我真的不知道这个程序要做什么。
  • 检查第一行是否所有的数字都是即使是打印f行的数字
  • 什么是“线”?这里的输入是什么?输出是什么?
  • 你期望它在这种情况下做什么和输出?
  • 我担心拼写“conut”。并不是说它实际上对行为很重要,但是如果在以后的代码中经常出现类似的错误,那么就会有很多由拼写错误引起的错误。

标签: c pointers void-pointers


【解决方案1】:

首先:我会从阅读所有警告开始,我猜它们有很多,从checkme 开始,尽管声明了类型 (void*),但没有返回值——你可能不明白星号的含义。在函数声明中它意味着它返回 void 指针(没有指定的数据类型)并且在调用中它会被取消引用,但是编译器如何允许你这样做,我不知道。

第二:下次正确格式化您的代码。难怪你不能得到它,这是一场灾难。尤其是任何合理的编辑器都可以自动完成。

checkme 肯定有问题:for(p=m;p-m&lt;C;p++){ — 看起来很可笑。 C 名称具有误导性,因为它是行数而不是相反的。顺便说一句,你为什么省略最后 3 列?

if(isEven(&amp;p)==1) — 我不知道它不会崩溃。 &amp; 返回指针,因此当它打算传递一行时,您将指针传递给矩阵。

然后,即使您似乎再次检查整个矩阵。无论如何,最好回到你理解的东西。哦,值应该是双精度值,而不是整数。

gcc -Wall -pedantic -Werror yourcode.c #compile with strict rules
/tmp/x.c: In function ‘ckeckme’:
/tmp/x.c:8:8: error: implicit declaration of function ‘isEven’ [-Werror=implicit-function-declaration]
     if(isEven(&p)==1)
        ^~~~~~
/tmp/x.c:9:5: error: implicit declaration of function ‘printf’ [-Werror=implicit-function-declaration]
     printf("%d",p-m);
     ^~~~~~
/tmp/x.c:9:5: error: incompatible implicit declaration of built-in function ‘printf’ [-Werror]
/tmp/x.c:9:5: note: include ‘<stdio.h>’ or provide a declaration of ‘printf’
/tmp/x.c:9:14: error: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long int’ [-Werror=format=]
     printf("%d",p-m);
             ~^  ~~~
             %ld
/tmp/x.c:5:15: error: unused variable ‘i’ [-Werror=unused-variable]
     int **p,j,i;
               ^
/tmp/x.c:5:13: error: unused variable ‘j’ [-Werror=unused-variable]
     int **p,j,i;
             ^
/tmp/x.c: In function ‘isEven’:
/tmp/x.c:15:22: error: unused variable ‘cc’ [-Werror=unused-variable]
     int *ptr,conut=0,cc=0;
                      ^~
/tmp/x.c:15:10: error: unused variable ‘ptr’ [-Werror=unused-variable]
     int *ptr,conut=0,cc=0;
          ^~~
/tmp/x.c: In function ‘main’:
/tmp/x.c:42:14: error: passing argument 1 of ‘ckeckme’ from incompatible pointer type [-Werror=incompatible-pointer-types]
     *ckeckme(matrix);
              ^~~~~~
/tmp/x.c:3:7: note: expected ‘int **’ but argument is of type ‘int (*)[3]’
 void *ckeckme(int **m)
       ^~~~~~~
/tmp/x.c:42:5: error: dereferencing ‘void *’ pointer [-Werror]
     *ckeckme(matrix);
     ^~~~~~~~~~~~~~~~
/tmp/x.c:34:9: error: unused variable ‘j’ [-Werror=unused-variable]
 int x,i,j,matrix[R][C]={    {8,1,2},
         ^
/tmp/x.c:34:7: error: unused variable ‘i’ [-Werror=unused-variable]
 int x,i,j,matrix[R][C]={    {8,1,2},
       ^
/tmp/x.c:34:5: error: unused variable ‘x’ [-Werror=unused-variable]
 int x,i,j,matrix[R][C]={    {8,1,2},
     ^
/tmp/x.c: In function ‘ckeckme’:
/tmp/x.c:11:1: error: control reaches end of non-void function [-Werror=return-type]
 }
 ^
/tmp/x.c: In function ‘isEven’:
/tmp/x.c:31:1: error: control reaches end of non-void function [-Werror=return-type]
 }
 ^

我假设您自己无法思考,因此请考虑以下代码:

#include <stdio.h>
#include <math.h>

int main(void) {
    double matrix[8][3]={ {8,1,2},
                    {3,7,5},
                    {6,2,14},
                    {13,8,15},
                    {8,0,2},
                    {4,50,26},
                    {2,84,11},
                    {12,36,9}};

    for( int i = 0; i < 8; i++ ) {
        int j;
        for( j = 0; j < 3; j++ ) {
            if( fmod( matrix[i][j], 2 ) != 0 ) // % operator is only for ints
                break;
        }

        if( j == 3 )
            printf( "row %d is all even\n", i+1 );
    }

    return 0;
}    

你唯一需要做的就是用指针来做。 ;)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-22
    • 2022-10-06
    • 2021-11-17
    • 1970-01-01
    • 2016-03-21
    • 2016-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多