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:
conio).rowandcolfor the constants.inputfunction has an extraneous semi-colon.resultcorrectly.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=3is not want you want since that’s basically trying to define the symbolROW=3to have an empty value.To get a symbol
ROWwith a value3, 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 (useconst) or functions (useinline) nowadays.(2) You use non-standard stuff (
conio).ISO C (the standard) does not include a
conio.hheader file. I understand that you’re using it so that you can use thegetchfunction but ISO C provides a perfectly adequategetcharfunction 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
rowandcolfor the constants.Since you’ve used uppercase
ROWandCOLfor your row and column constants, you should use uppercase in yourforstatements. C is a case-sensitive language and usingrowandcolwill cause the compiler to complain that they don’t exist.(4) Your
inputfunction 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
resultcorrectly.The
resultvariable should have a definite size. The definitionint 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:
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 🙂
Some suggestions for making it nicer: