Reputation:
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]);
}
Upvotes: 0
Views: 1764
Reputation: 882676
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
).row
and col
for the constants.input
function has an extraneous semi-colon.result
correctly.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:
Upvotes: 14
Reputation: 23237
First thing that pops out now that the code is formatted:
#define ROW=3
#define COL=3
should be
#define ROW 3
#define COL 3
Upvotes: 1
Reputation: 33197
#define ROW=3
#define COL=3
is incorrect for the C preprocessor. Try:
#define ROW 3
#define COL 3
See this page for more information on how to use the preprocessor.
Upvotes: 3