1

Here is my code...I have two dimensional matrices A,B. I want to develop the product of the A and B.

#include <stdio.h>
    #include <stdlib.h>

    int main(int argc, char *argv[])
    {

    int scan();
    int multiply();
    int print();

    int metxa[3][4],metxb[4][3],resut[3][3];

    int main();
    {
    scan();
    multiply();
    print();
    return 0;
    }
    int scan();
    {
    int i;
    for(i=0;i<3;i++)
    {
    scanf("%d %d %d %d",&metxa[i][0],&metxa[i][1],&metxa[i][2],&metxa[i][3]);
    }
    for(i=0;i<4;i++)
    {
    scanf("%d %d %d",&metxb[i][0],&metxb[i][1],&metxb[i][2]);
    }
    }
    int multiply();
    {
    int i,j;
    for(i=0;i<3;i++)
    {
    for(j=0;j<3;j++)
    {
    resut[i][j]=metxa[i][0]*metxb[0][j]+metxa[i][1]*metxb[1][j]+metxa[i][2]*metxb[2][j]+metxa[i][3]*metxb[3][j];

    }
    }
    }
    int print();
    {
    int i,j;
    for(i=0;i<3;i++)
    {
    for(j=0;j<3;j++)
    {
    printf("%d\t",resut[i][j]);
    }
    printf("\n");
    }
    }
    system("PAUSE");
    return 0;
    }

but I got linker error-undefined reference to scan and multiply methods... why should I get this error?

desh
  • 33
  • 4
  • 1
    Why should you get this error? More like why shouldn't you! [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Marlon Jan 09 '12 at 20:32
  • 7
    WTH is this? Why have you declared main, inside main? There are too many problems with this code. Answering this particular one is fruitless. [Read a book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Benjamin Lindley Jan 09 '12 at 20:33
  • 2
    I don't think this should get downvoted. Obviously the OP is new to programming, and it seems like a fine question. – Marlon Jan 09 '12 at 20:37
  • 2
    @Marlon: Well it certainly shouldn't get up-voted. Not until that formatting is cleaned up. – Benjamin Lindley Jan 09 '12 at 20:40
  • @desh you might want to indent the code logically so it's ~~more~~ readable. –  Jan 09 '12 at 20:57
  • @marlon thank u... i am new to the programming languages. that is why i asked this kind of little things.... – desh Jan 09 '12 at 21:09

2 Answers2

3

Semi-colon after function name at definition:

int scan();
{
    ...
}

Should be:

int scan()
{
    ...
}

Same for multiply() and print() functions.

Also:

  • Neither scan(), multiply() and print() return a value yet their return value is int.
  • scan(), multiply(), print() and declared and defined within main().

Here is the posted code in a compilable state (I am making no declaration as to is correctness):

#include <stdio.h>
#include <stdlib.h>

void scan();
void multiply();
void print();

int metxa[3][4],metxb[4][3],resut[3][3];

int main(int argc, char *argv[])
{
    scan();
    multiply();
    print();
    system("PAUSE");

    return 0;
}

void scan()
{
    int i;
    for(i=0;i<3;i++)
    {
        scanf("%d %d %d %d",
              &metxa[i][0],
              &metxa[i][1],
              &metxa[i][2],
              &metxa[i][3]);
    }
    for(i=0;i<4;i++)
    {
        scanf("%d %d %d",
              &metxb[i][0],
              &metxb[i][1],
              &metxb[i][2]);
    }
}

void multiply()
{
    int i,j;
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
            resut[i][j] = metxa[i][0]*
                          metxb[0][j]+metxa[i][1]*
                          metxb[1][j]+metxa[i][2]*
                          metxb[2][j]+metxa[i][3]*
                          metxb[3][j];
        }
    }
}

void print()
{
    int i,j;
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
            printf("%d\t",resut[i][j]);
        }
        printf("\n");
    }
}
hmjd
  • 120,187
  • 20
  • 207
  • 252
3

You need to either declare the function before you call it, or at least a forward declarations of it with the implementation somewhere later.

#include <stdio.h>
#include <stdlib.h>

void scan();
int main(int argc, char *argv[])
{
    scan();
}

void scan()
{
  //do stuff
}

or

#include <stdio.h>
#include <stdlib.h>

void scan()
{
  //do stuff
}
int main(int argc, char *argv[])
{
    scan();
}

You also need to declare the matrix outside the body of main(). As with the functions, they will not be availiable if you do.

Kimmeh
  • 991
  • 7
  • 21