Type mismatch error

S

shan

What is the meaning for the error expression syntax and type mismatch
error.I am using turbo c++.can anybody correct the errors in the
folowing program.

Following program is to find matrix addition.

Thanks in advance.

#include<stdio.h>
int rows,cols,a[3][3],b[3][3];
void main()
{
int mat_add[3][3],i,j;
int enter(void);
int getting_mat_1(int a[][3]);
int getting_mat_2(int b[][3]);
enter();
getting_mat_1(a[][3]); /*line 10 expression syntax*/
getting_mat_2(b[][3]); /*line 11 same as above*/
for(i=0;i<rows;i++)
for(j=0;j<cols;j++)
mat_add[j]=a[j]+b[j];
for(i=0;i<rows;i++)
{
printf("\n");
for(j=0;j<cols;j++)
printf("%d",mat_add[j]);
}
}


int enter(void)
{
int i,j;
printf("Enter how many rows to be entered\n");
scanf("%d",&rows);
printf("Enter how many columns to be entered\n");
scanf("%d",&cols);
printf("Enter the matrix in the form\n1 0 1\n9 8 7\n7 5 2\n");
return(rows,cols);
}


void getting_mat_1(int a[][3])
{ /*line 37 type mismatch in
declaration*/
int i,j;
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
if ('\n'==getchar())
break;
else
scanf("%d",a[j]);
}
}
}


int getting_mat_2(int b[][3])
{
int i,j;
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
if ('\n'==getchar())
break;
else
scanf("%d",b[j]);
}
}
}
 
S

Sandeep

shan said:
What is the meaning for the error expression syntax and type mismatch
error.I am using turbo c++.can anybody correct the errors in the
folowing program.

Following program is to find matrix addition.

Thanks in advance.

#include<stdio.h>
int rows,cols,a[3][3],b[3][3];
void main()
{
int mat_add[3][3],i,j;
int enter(void);
int getting_mat_1(int a[][3]);

In the function body you are defining it as "void getting_mat_1()";
Hence the redeclaration error.
int getting_mat_2(int b[][3]);
enter();
getting_mat_1(a[][3]); /*line 10 expression syntax*/
getting_mat_2(b[][3]); /*line 11 same as above*/

The following will suffice.
getting_mat_1(a);
getting_mat_1(b);

void getting_mat_1(int a[][3])
{ /*line 37 type mismatch in
declaration*/
int i,j;
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
if ('\n'==getchar())
break;
else
scanf("%d",a[j]);
}
}
}


int getting_mat_2(int b[][3])
{
int i,j;
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
if ('\n'==getchar())
break;
else
scanf("%d",b[j]);
}
}
}


Here is a code that compiles - Though i have not seen the output.

#include <stdio.h>

int rows,cols,a[3][3],b[3][3];

void main()
{
int mat_add[3][3],i,j;
int enter(void);
int getting_mat_1(int a[][3]);
int getting_mat_2(int b[][3]);
enter();
getting_mat_1(a);
getting_mat_2(b);
for(i=0;i<rows;i++)
for(j=0;j<cols;j++)
mat_add[j]=a[j]+b[j];
for(i=0;i<rows;i++)
{
printf("\n");
for(j=0;j<cols;j++)
printf("%d",mat_add[j]);
}

}

int enter(void)
{
int i,j;
printf("Enter how many rows to be entered\n");
scanf("%d",&rows);
printf("Enter how many columns to be entered\n");
scanf("%d",&cols);
printf("Enter the matrix in the form\n1 0 1\n9 8 7\n7 5 2\n");
return(rows,cols);
}

int getting_mat_1(int a[][3])
{
int i,j;
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
if ('\n'==getchar())
break;
else
scanf("%d",a[j]);
}
}
}


int getting_mat_2(int b[][3])
{
int i,j;
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
if ('\n'==getchar())
break;
else
scanf("%d",b[j]);
}
}
}
 
P

pete

Sandeep wrote:
Here is a code that compiles - Though i have not seen the output.

I don't have the stamina
to explain even a small percentage
of what's wrong with that code.
 
S

Sandeep

pete said:
I don't have the stamina
to explain even a small percentage
of what's wrong with that code.

Then Don't - I have not corrected his code logically. Far from that, I
have not even read what he wrote. Corrections were only made to the
syntax and why it was not compiling.
 
S

shan

Sandeep wrote:
int getting_mat_1(int a[][3]);
int getting_mat_2(int b[][3]);

Array doesn't return anyhting so void should be declared instead of
int.

Here also void.

int getting_mat_1(int a[][3])
{
. ....................
.....................

}
Here also void

int getting_mat_2(int b[][3])
{
int i,j;
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
if ('\n'==getchar())
break;
else
scanf("%d",b[j]);
}
}
}


But an error occured at run time .Each time when scanf is called it
terminates only when enter is pressed.It should be changed so that by
pressing spacebar it should be terminated.
 
S

Sandeep

shan said:
Sandeep wrote:
int getting_mat_1(int a[][3]);
int getting_mat_2(int b[][3]);

Array doesn't return anyhting so void should be declared instead of
int.

getting_mat_1 is a function and it can return any valid datatype that
you want. It is a matter of choice and design. You may want to return a
status code from your function to indicate success or failure. In that
case, use int. If you do not, you can use void.
Here also void.

int getting_mat_1(int a[][3])
{
. ....................
.....................

}
Here also void

int getting_mat_2(int b[][3])
{
int i,j;
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
if ('\n'==getchar())
break;
else
scanf("%d",b[j]);
}
}
}


But an error occured at run time .Each time when scanf is called it
terminates only when enter is pressed.It should be changed so that by
pressing spacebar it should be terminated.


The run time error is happening because of the statement
"scanf("%d",b[j])". It should read "scanf("%d",&b[j])". Note the
"&". This denotes the address of b[j]th element.

I have made some changes to your program. The program is far from being
perfect for what you are trying to do. It reads two 3X3 arrays and adds
them. You should now try to improve it so that it can read a MXN array
and add them. Hope this helps.

#include <stdio.h>

// int enter(void); // No need for this function. A function should be
declared only when needed
// Since you are declaring your matrix "a" and "b" as 3X3,
// I am assuming that is what you needed. Try to improve the program
for MXN

int a[3][3],b[3][3];
void getting_mat(int a[][3]);

// No need to have mat1, mat2 . This is where a function helps
// Just use the same function to read the values

int main()
{
int mat_add[3][3];
int i,j;


printf("\nEnter first 3X3 matrix: \n");
getting_mat(a);
printf("\nEnter second 3X3 matrix: \n");
getting_mat(b);

for(i=0;i<3;i++)
for(j=0;j<3;j++)
mat_add[j]=a[j]+b[j];


printf("\nThe addition of matrix a and b\n");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",mat_add[j]);
}

}

void getting_mat(int a[][3])
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[j]);
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top