2d阵列相加 [英] 2d array addition

查看:277
本文介绍了2d阵列相加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个程式加入两个矩阵,但是提供太多错误,我无法解决。



错误:


$ b b


ARRAY BOUNDS MISSING



表达语法


任何想法?

 #include< stdio.h> 
#include< conio.h>

#define ROW = 3
#define COL = 3

int result [] [COL];
void input(int arr [] [COL]);
void add(int mat1 [] [COL],mat2 [] [COL]);
void print(int result [] [COL]);

void main(void)
{
int mat1 [ROW] [COL],mat2 [ROW]
input(mat1);
input(mat2);
add(mat1,mat2);
print(result);
getch();
}

void input(int arr [] [COL]);
{
for(int i = 0; i for(int j = 0; j {
printf Enter element);
scanf(%d,& arr [i] [j]);
}
}

void add(int mat1 [] [COL],int mat2 [] [COL])
{
for = 0; i for(int j = 0; j {
result [i] [j] = mat1 [i] + mat2 [i] [j];
}
}

void print(int result [] [COL])
{
for(int i = 0; i< row; i ++ )
for(int j = 0; j< col; j ++)
printf(%d,result [i] [j]
}


解决方案

。至少你试过之前你要求帮助,这是比一些在这里。您有以下问题:




  • 您的定义格式错误。

  • c

  • 您使用小写字母 row code> col 。

  • 您的输入函数有一个额外的分号。

  • 您不能正确定义 result

  • int



更详细:



(1)您的定义格式有误



您不使用 = 定义,因为它们应该是相对简单的文本替换。所以行 #define ROW = 3 不是你想要的,因为那基本上试图定义符号 ROW = 3



获取一个值 3 ROW / code>,您需要使用 #define ROW 3



代码,你会使用 static const int ROW = 3; ,因为这将给你一个一流的编译器符号,而不只是一个文本替换。实际上没有任何需要使用预处理器定义常量(使用 const )或函数(使用 inline



(2)您使用非标准内容( conio )。



ISO C(标准)不包含 conio.h 头文件。我知道你使用它,以便你可以使用 getch 函数,但ISO C提供了一个完全充分的 getchar

这是 使用扩展的语言,只是意识到,他们通常使你的代码更少的便携式



(3)使用小写 row col ROW 和<$ p>

c $ c> COL
为您的行和列常数,您应该在中使用大写。语句。 C是区分大小写的语言,并且使用 row col 会导致编译器抱怨它们不存在。



(4)您的输入函数有一个额外的分号。 / p>

这只是一个错误的; 在该函数的第一行,

p>

result 变量应该具有确定的大小。 int result [] [10]; 是所谓的不完整类型,因为大小无法识别。



(6)主函数应始终返回 int



是C中主要函数的两种标准形式。这些是:

  int main(void); 
int main(int c,char * v []);

其他允许由实现提供,但如果你想要的代码,






这会消除所有的语法错误,现在你可以使用界面较丑陋: - )

 #include< stdio.h> 

#define ROW 3
#define COL 3

int result [ROW] [COL];
void input(int arr [] [COL]);
void add(int mat1 [] [COL],int mat2 [] [COL]);
void print(int result [] [COL]);

int main(void)
{
int mat1 [ROW] [COL],mat2 [ROW] [COL];
input(mat1);
input(mat2);
add(mat1,mat2);
print(result);
getchar();
return 0;
}
void input(int arr [] [COL])
{
for(int i = 0; i< ROW; i ++)
for = 0; j {
printf(Enter element);
scanf(%d,& arr [i] [j]);
}
}
void add(int mat1 [] [COL],int mat2 [] [COL])
{
for(int i = 0; i< ; ROW; i ++)
for(int j = 0; j result [i] [j] = mat1 [i] [j] + mat2 [i] [j]
}
void print(int result [] [COL])
{
for(int i = 0; i< ROW; i ++)
for = 0; j printf(%d,result [i] [j]);
}

有些建议让它更好:




  • 通过显示矩阵号和坐标,使您输入的值更加明显。

  • 输出数字。

  • 在输出行之间打印换行符。

  • 使数字固定宽度打印li>
  • 评论您的代码(我不能强调这一点)。


This program adds two matrices but it's giving too many errors and I can't solve them.

Errors:

ARRAY BOUNDS MISSING

and

EXPRESSION SYNTAX

Any ideas?

#include<stdio.h>
#include<conio.h>

#define ROW=3
#define COL=3

int result[][COL];
void input(int arr[][COL]);
void add(int mat1[][COL],mat2[][COL]);
void print(int result[][COL]);

void main(void)
{
    int mat1[ROW][COL],mat2[ROW][COL];
    input(mat1);
    input(mat2);
    add(mat1,mat2);
    print(result);
    getch();
}

void input(int arr[][COL]);
{
    for(int i=0;i<row;i++)
        for(int j=0;j<col;j++)
        {
            printf("Enter element");
            scanf("%d",&arr[i][j]);
        }
}

void add(int mat1[][COL],int mat2[][COL])
{
    for(int i=0;i<row;i++)
        for(int j=0;j<col;j++)
        {
            result[i][j]=mat1[i][j]+mat2[i][j];
        }
}

void print(int result[][COL])
{
    for(int i=0;i<row;i++)
        for(int j=0;j<col;j++)
            printf("%d",result[i][j]);
}

解决方案

That's not a bad first attempt. At least you tried before you asked for help, which is more than some do here. You have the following problems:

  • Your defines are of the wrong format.
  • You use non-standard stuff (conio).
  • You use lowercase row and col for the constants.
  • Your input function has an extraneous semi-colon.
  • You don't define result correctly.
  • The main function should always return an int.

In more detail:

(1) Your defines are of the wrong format.

You don't use = in defines since they're supposed to be relatively simple text substitutions. So the line #define ROW=3 is not want you want since that's basically trying to define the symbol ROW=3 to have an empty value.

To get a symbol ROW with a value 3, you need to use #define ROW 3.

Of course, in more modern code you would use static const int ROW = 3; since that would give you a first-class compiler symbol rather than just a text substitution. There really isn't any need to use pre-processor definitions for constants (use const) or functions (use inline) nowadays.

(2) You use non-standard stuff (conio).

ISO C (the standard) does not include a conio.h header file. I understand that you're using it so that you can use the getch function but ISO C provides a perfectly adequate getchar function which serves the same purpose here.

It's okay to use extensions to the language, just be aware that they generally make your code less portable.

(3) You use lowercase row and col for the constants.

Since you've used uppercase ROW and COL for your row and column constants, you should use uppercase in your for statements. C is a case-sensitive language and using row and col will cause the compiler to complain that they don't exist.

(4) Your input function has an extraneous semi-colon.

This is just a misplaced ; in the first line of that function, at the end of the line containing the function devclaration.

(5) You don't define result correctly.

The result variable should have a definite size. The definition int result[][10]; is what's known as an incomplete type since the size cannot be known.

(6) The main function should always return an int.

There are two standard forms of the main function in C. Those are:

int main(void);
int main (int c, char *v[]);

Others are allowed to be provided by the implementation but, if you want code that will run anywhere, you should limit yourself to one of those.


This gets rid of all your syntax errors, now you can work on making the interface less ugly :-)

#include<stdio.h>

#define ROW 3
#define COL 3

int result[ROW][COL];
void input(int arr[][COL]);
void add(int mat1[][COL],int mat2[][COL]);
void print(int result[][COL]);

int main(void)
{
    int mat1[ROW][COL],mat2[ROW][COL];
    input(mat1);
    input(mat2);
    add(mat1,mat2);
    print(result);
    getchar();
    return 0;
}
void input(int arr[][COL])
{
    for(int i=0;i<ROW;i++)
        for(int j=0;j<COL;j++)
        {
            printf("Enter element");
            scanf("%d",&arr[i][j]);
        }
}
void add(int mat1[][COL],int mat2[][COL])
{
    for(int i=0;i<ROW;i++)
        for(int j=0;j<COL;j++)
            result[i][j]=mat1[i][j]+mat2[i][j];
}
void print(int result[][COL])
{
    for(int i=0;i<ROW;i++)
        for(int j=0;j<COL;j++)
            printf("%d",result[i][j]);
}

Some suggestions for making it nicer:

  • Make it more obvious which value you're entering by showing the matrix number and co-ordinates.
  • Make it print spaces between the output numbers.
  • Make it print newlines between the output lines.
  • Make it print the numbers fixed width (to line up nicely).
  • Comment your code (I can't stress this enough).

这篇关于2d阵列相加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆