Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Type mismatch error (http://www.velocityreviews.com/forums/t440218-type-mismatch-error.html)

shan 11-20-2005 12:26 PM

Type mismatch error
 
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[i][j]=a[i][j]+b[i][j];
for(i=0;i<rows;i++)
{
printf("\n");
for(j=0;j<cols;j++)
printf("%d",mat_add[i][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[i][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[i][j]);
}
}
}


Sandeep 11-20-2005 12:45 PM

Re: Type mismatch error
 

shan wrote:
> 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[i][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[i][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[i][j]=a[i][j]+b[i][j];
for(i=0;i<rows;i++)
{
printf("\n");
for(j=0;j<cols;j++)
printf("%d",mat_add[i][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[i][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[i][j]);
}
}
}


pete 11-20-2005 02:02 PM

Re: Type mismatch error
 
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.

--
pete

Barry 11-20-2005 02:12 PM

Re: Type mismatch error
 

"pete" <pfiland@mindspring.com> wrote in message
news:43808200.4B4D@mindspring.com...
> 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.
>
> --
> pete


LOL!

barry



Sandeep 11-20-2005 03:25 PM

Re: Type mismatch error
 

pete wrote:
> 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.


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.


shan 11-20-2005 04:42 PM

Re: Type mismatch error
 

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[i][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.


Sandeep 11-20-2005 05:33 PM

Re: Type mismatch error
 
shan wrote:
> 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[i][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[i][j])". It should read "scanf("%d",&b[i][j])". Note the
"&". This denotes the address of b[i][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[i][j]=a[i][j]+b[i][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[i][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[i][j]);
}
}
}



All times are GMT. The time now is 11:10 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.