Type mismatch error

Discussion in 'C Programming' started by shan, Nov 20, 2005.

  1. shan

    shan Guest

    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]);
    }
    }
    }
     
    shan, Nov 20, 2005
    #1
    1. Advertising

  2. shan

    Sandeep Guest

    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[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]);
    }
    }
    }
     
    Sandeep, Nov 20, 2005
    #2
    1. Advertising

  3. shan

    pete Guest

    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
     
    pete, Nov 20, 2005
    #3
  4. shan

    Barry Guest

    "pete" <> wrote in message
    news:...
    > 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
     
    Barry, Nov 20, 2005
    #4
  5. shan

    Sandeep Guest

    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.
     
    Sandeep, Nov 20, 2005
    #5
  6. shan

    shan Guest

    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.
     
    shan, Nov 20, 2005
    #6
  7. shan

    Sandeep Guest

    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[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]);
    }
    }
    }
     
    Sandeep, Nov 20, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. data type mismatch error

    , Aug 10, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    656
    Karl Seguin
    Aug 10, 2005
  2. type mismatch error

    , Aug 12, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    3,410
    Kevin Spencer
    Aug 12, 2005
  3. Murrgon
    Replies:
    1
    Views:
    413
    Thomas Jollans
    Jun 10, 2010
  4. Sean

    Long Raw Type Mismatch error

    Sean, Jul 23, 2003, in forum: ASP General
    Replies:
    0
    Views:
    122
  5. programmingzeal
    Replies:
    0
    Views:
    1,170
    programmingzeal
    May 6, 2012
Loading...

Share This Page