im working on a program for my exams and its not really working.....cant really figure out why........i ran my program in both borland and turbo.....and both are giving the same error......DECLARATION SYNTAX ERROR.

void mul(int a[][3],int b[][3])
{int k=0;
  for (i=0;i<3;++i)
   {
	  for(j=0;j<3;++j)
		{  c[i][j]=0;
         for(k=0;k<3;++k)
	  		c[i][j]=c[i][j]+(a[i][k]*b[k][j]); //Multiplication of two matrices
      }
	}
 cout<<"THE RESULTANT MATRIX IS:"<<"\n";
  for(i=0;i<3;i++)
	{
		cout<<"\n";
		for(j=0;j<3;j++)
			cout<<"  "<<c[i][j];
	}

}

its a really simple program but still i cant figure out the problem..... :( !!!

would really appreciate the help.....!

the compiler points to the line after the function name......

For one, i & j aren't declared, unless they are global variables.... but my C++ teacher would kill you for doing that. Take a look yourself:

for (i=0;i<3;++i)
   {
	  for(j=0;j<3;++j)

Should be: for (int i = 0 .....)

Also, where is c[][] coming from? It just appears in the middle of the function out of nowhere.

I got this to compile just fine by declaring the variables that I mentioned above.

yeah i actually had a lot of functions requiring i,j and c........so i declared them all as global variables....

im calling these functions from the main and passing arrays as the parameters to the functions.......heres the entire program....

#include<iostream.h>
#include<conio.h>
#include<process.h>
int i,j;
int c[3][3];
void add(int a[3][3],int b[3][3])
{
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
c[i][j]=(a[i][j] + b[i][j]); //Addition of two matrices.
}
cout<<"THE RESULTANT MATRIX IS:"<<"\n";
for(i=0;i<3;i++)
{
cout<<"\n";
for(j=0;j<3;j++)
cout<<" "<<c[i][j];
}
}

void subt(int a[3][3],int b[3][3])
{
for(i=0;i<3;++i)
{
for(j=0;j<3;++j)
c[i][j]=a[i][j]-b[i][j]; //Subtraction of two matrices
}
cout<<"THE RESULTANT MATRIX IS:"<<"\n";
for(i=0;i<3;i++)
{
cout<<"\n";
for(j=0;j<3;j++)
cout<<" "<<c[i][j];
}

void mul(int a[3][3],int b[3][3])
{int k;
for (i=0;i<3;++i)
{
for(j=0;j<3;++j)
{ c[i][j]=0;
for(k=0;k<3;++k)
c[i][j]=c[i][j]+(a[i][k]*b[k][j]); //Multiplication of two matrices
}
}
cout<<"THE RESULTANT MATRIX IS:"<<"\n";
for(i=0;i<3;i++)
{
cout<<"\n";
for(j=0;j<3;j++)
cout<<" "<<c[i][j];
}

}
void diag(int a[3][3])
{ int sum=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i==j)
sum=sum + a[i][j];
}
}
cout<<"THE SUM OF DIAGONAL ELEMENTS IS: "<<sum<<"\n";
}


void main()
{char ch;
int n,x[3][3],y[3][3];
do
{
cout<<"\t \t MENU FOR 3X3 MATRICES \t \t"<<"\n \n";
cout<<'\t'<<" 1.ADD THE TWO MATRICES "<<"\n"<<'\t'<<"2.SUBTRACT THE TWO MATRICES"<<"\n"<<'\t'<<"3.MULTIPLY THE TWO MATRICES"<<"\n"<<'\t'<<"4.SUM THE DIAGONAL ELEMENTS"<<"\n"<<'\t'<<"5.EXIT"<<"\n \n";
cout<<"ENTER YOUR CHOICE";
cin>>n;
switch(n)
{ case 1 :cout<<"ENTER THE TWO MATRICES"<<"\n"<<"MATIRX 1";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>x[i][j];
}
}

cout<<"\n"<<"MATRIX 2";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>y[i][j];
}
}
add(x,y);
break;

case 2: cout<<"ENTER THE TWO MATRICES"<<"\n"<<"MATIRX 1";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>x[i][j];
}
}

cout<<"\n"<<"MATRIX 2";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>y[i][j];
}
}
subt(x,y);
break;

case 3: cout<<"ENTER THE TWO MATRICES"<<"\n"<<"MATIRX 1";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>x[i][j];
}
}

cout<<"\n"<<"MATRIX 2";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>y[i][j];
}
}
mul(x,y);
break;

case 4: cout<<"ENTER THE MATRIX"<<"\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>x[i][j];
}
}
diag(x);
break;

case 5:cout<<"THANKYOU FOR USING THIS PROGRAM!";
exit(0);
break;

default: cout<<"WRONG OPTION ENTERED!";
BREAK;
}
cout<<"\n"<<"DO YOU WANT TO CONTINUE? (Y/N)";
cin>>ch;
}while(ch=='Y'|| ch=='y');

}

hope it was helpful..

You had several problems with your code. I got it to compile though.

A) void main() ... in C++ it's ALWAYS int main()

B) Check your opening & closing braces {}, that's where your mistake is. You have an unbalanced number of opening and closing braces so I believe that one of your functions is not ending where it should. To avoid this problem always use opening and closing braces like this:

for ()
{
   for ()
   {
      for ()
      {
          ....
      }
   }
}

C) BREAK; should be break;

D) exit() requires <cstdlib>. I changed your includes a bit to:
#include <iostream>
#include <cstdlib>

That's all you seem to need in this program.

Here's the code that compiles.... Note that you will want to double check it. I put braces where I "thought" you wanted to put them, ... aka verify that everything works right.

#include <iostream>
#include <cstdlib>

using namespace std;

int i, j;
int c[3][3];

void add(int a[3][3], int b[3][3]) {
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++)
            c[i][j] = (a[i][j] + b[i][j]); //Addition of two matrices.
    }
    cout << "THE RESULTANT MATRIX IS:" << "\n";
    for (i = 0; i < 3; i++) {
        cout << "\n";
        for (j = 0; j < 3; j++)
            cout << " " << c[i][j];
    }
}

void subt(int a[3][3], int b[3][3]) {
    for (i = 0; i < 3; ++i) {
        for (j = 0; j < 3; ++j)
            c[i][j] = a[i][j] - b[i][j]; //Subtraction of two matrices
    }
    cout << "THE RESULTANT MATRIX IS:" << "\n";
    for (i = 0; i < 3; i++) {
        cout << "\n";
        for (j = 0; j < 3; j++) {
            cout << " " << c[i][j];
        }
    }
}

void mul(int a[3][3], int b[3][3]) {
    int k;
    for (i = 0; i < 3; ++i) {
        for (j = 0; j < 3; ++j) {
            c[i][j] = 0;
            for (k = 0; k < 3; ++k) {
                c[i][j] = c[i][j]+(a[i][k] * b[k][j]); //Multiplication of two matrices
            }
        }
    }
    cout << "THE RESULTANT MATRIX IS:" << "\n";
    for (i = 0; i < 3; i++) {
        cout << "\n";
        for (j = 0; j < 3; j++)
            cout << " " << c[i][j];
    }

}

void diag(int a[3][3]) {
    int sum = 0;
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            if (i == j)
                sum = sum + a[i][j];
        }
    }
    cout << "THE SUM OF DIAGONAL ELEMENTS IS: " << sum << "\n";
}

int main() {
    char ch;
    int n, x[3][3], y[3][3];
    do {
        cout << "\t \t MENU FOR 3X3 MATRICES \t \t" << "\n \n";
        cout << '\t' << " 1.ADD THE TWO MATRICES " << "\n" << '\t' << "2.SUBTRACT THE TWO MATRICES" << "\n" << '\t' << "3.MULTIPLY THE TWO MATRICES" << "\n" << '\t' << "4.SUM THE DIAGONAL ELEMENTS" << "\n" << '\t' << "5.EXIT" << "\n \n";
        cout << "ENTER YOUR CHOICE";
        cin >> n;
        switch (n) {
                case 1:cout << "ENTER THE TWO MATRICES" << "\n" << "MATIRX 1";
                for (i = 0; i < 3; i++) {
                    for (j = 0; j < 3; j++) {
                        cin >> x[i][j];
                    }
                }

                cout << "\n" << "MATRIX 2";
                for (i = 0; i < 3; i++) {
                    for (j = 0; j < 3; j++) {
                        cin >> y[i][j];
                    }
                }
                add(x, y);
                break;

            case 2: cout << "ENTER THE TWO MATRICES" << "\n" << "MATIRX 1";
                for (i = 0; i < 3; i++) {
                    for (j = 0; j < 3; j++) {
                        cin >> x[i][j];
                    }
                }

                cout << "\n" << "MATRIX 2";
                for (i = 0; i < 3; i++) {
                    for (j = 0; j < 3; j++) {
                        cin >> y[i][j];
                    }
                }
                subt(x, y);
                break;

            case 3: cout << "ENTER THE TWO MATRICES" << "\n" << "MATIRX 1";
                for (i = 0; i < 3; i++) {
                    for (j = 0; j < 3; j++) {
                        cin >> x[i][j];
                    }
                }

                cout << "\n" << "MATRIX 2";
                for (i = 0; i < 3; i++) {
                    for (j = 0; j < 3; j++) {
                        cin >> y[i][j];
                    }
                }
                mul(x, y);
                break;

            case 4: cout << "ENTER THE MATRIX" << "\n";
                for (i = 0; i < 3; i++) {
                    for (j = 0; j < 3; j++) {
                        cin >> x[i][j];
                    }
                }
                diag(x);
                break;

            case 5:cout << "THANKYOU FOR USING THIS PROGRAM!";
                exit(0);
                break;

            default: cout << "WRONG OPTION ENTERED!";
                break;
        }
        cout << "\n" << "DO YOU WANT TO CONTINUE? (Y/N)";
        cin >> ch;
    } while (ch == 'Y' || ch == 'y');

}

You had several problems with your code. I got it to compile though.

A) void main() ... in C++ it's ALWAYS int main()

B) Check your opening & closing braces {}, that's where your mistake is. You have an unbalanced number of opening and closing braces so I believe that one of your functions is not ending where it should. To avoid this problem always use opening and closing braces like this:

for ()
{
   for ()
   {
      for ()
      {
          ....
      }
   }
}

C) BREAK; should be break;

D) exit() requires <cstdlib>. I changed your includes a bit to:
#include <iostream>
#include <cstdlib>

That's all you seem to need in this program.

Here's the code that compiles.... Note that you will want to double check it. I put braces where I "thought" you wanted to put them, ... aka verify that everything works right.

#include <iostream>
#include <cstdlib>

using namespace std;

int i, j;
int c[3][3];

void add(int a[3][3], int b[3][3]) {
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++)
            c[i][j] = (a[i][j] + b[i][j]); //Addition of two matrices.
    }
    cout << "THE RESULTANT MATRIX IS:" << "\n";
    for (i = 0; i < 3; i++) {
        cout << "\n";
        for (j = 0; j < 3; j++)
            cout << " " << c[i][j];
    }
}

void subt(int a[3][3], int b[3][3]) {
    for (i = 0; i < 3; ++i) {
        for (j = 0; j < 3; ++j)
            c[i][j] = a[i][j] - b[i][j]; //Subtraction of two matrices
    }
    cout << "THE RESULTANT MATRIX IS:" << "\n";
    for (i = 0; i < 3; i++) {
        cout << "\n";
        for (j = 0; j < 3; j++) {
            cout << " " << c[i][j];
        }
    }
}

void mul(int a[3][3], int b[3][3]) {
    int k;
    for (i = 0; i < 3; ++i) {
        for (j = 0; j < 3; ++j) {
            c[i][j] = 0;
            for (k = 0; k < 3; ++k) {
                c[i][j] = c[i][j]+(a[i][k] * b[k][j]); //Multiplication of two matrices
            }
        }
    }
    cout << "THE RESULTANT MATRIX IS:" << "\n";
    for (i = 0; i < 3; i++) {
        cout << "\n";
        for (j = 0; j < 3; j++)
            cout << " " << c[i][j];
    }

}

void diag(int a[3][3]) {
    int sum = 0;
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            if (i == j)
                sum = sum + a[i][j];
        }
    }
    cout << "THE SUM OF DIAGONAL ELEMENTS IS: " << sum << "\n";
}

int main() {
    char ch;
    int n, x[3][3], y[3][3];
    do {
        cout << "\t \t MENU FOR 3X3 MATRICES \t \t" << "\n \n";
        cout << '\t' << " 1.ADD THE TWO MATRICES " << "\n" << '\t' << "2.SUBTRACT THE TWO MATRICES" << "\n" << '\t' << "3.MULTIPLY THE TWO MATRICES" << "\n" << '\t' << "4.SUM THE DIAGONAL ELEMENTS" << "\n" << '\t' << "5.EXIT" << "\n \n";
        cout << "ENTER YOUR CHOICE";
        cin >> n;
        switch (n) {
                case 1:cout << "ENTER THE TWO MATRICES" << "\n" << "MATIRX 1";
                for (i = 0; i < 3; i++) {
                    for (j = 0; j < 3; j++) {
                        cin >> x[i][j];
                    }
                }

                cout << "\n" << "MATRIX 2";
                for (i = 0; i < 3; i++) {
                    for (j = 0; j < 3; j++) {
                        cin >> y[i][j];
                    }
                }
                add(x, y);
                break;

            case 2: cout << "ENTER THE TWO MATRICES" << "\n" << "MATIRX 1";
                for (i = 0; i < 3; i++) {
                    for (j = 0; j < 3; j++) {
                        cin >> x[i][j];
                    }
                }

                cout << "\n" << "MATRIX 2";
                for (i = 0; i < 3; i++) {
                    for (j = 0; j < 3; j++) {
                        cin >> y[i][j];
                    }
                }
                subt(x, y);
                break;

            case 3: cout << "ENTER THE TWO MATRICES" << "\n" << "MATIRX 1";
                for (i = 0; i < 3; i++) {
                    for (j = 0; j < 3; j++) {
                        cin >> x[i][j];
                    }
                }

                cout << "\n" << "MATRIX 2";
                for (i = 0; i < 3; i++) {
                    for (j = 0; j < 3; j++) {
                        cin >> y[i][j];
                    }
                }
                mul(x, y);
                break;

            case 4: cout << "ENTER THE MATRIX" << "\n";
                for (i = 0; i < 3; i++) {
                    for (j = 0; j < 3; j++) {
                        cin >> x[i][j];
                    }
                }
                diag(x);
                break;

            case 5:cout << "THANKYOU FOR USING THIS PROGRAM!";
                exit(0);
                break;

            default: cout << "WRONG OPTION ENTERED!";
                break;
        }
        cout << "\n" << "DO YOU WANT TO CONTINUE? (Y/N)";
        cin >> ch;
    } while (ch == 'Y' || ch == 'y');

}

hey thanks a lot for the help!!! the program didnt work at first though,but thats because my compiler doesnt recognize cstdlib ...and for me my exit function is in process.h header file...
neways i ran the program and it works beautifully!!! thanks again!!!!

ps:i got another 20 programs to go so ull be hearing from me again pretty soon!! ;) thanks again..take care..!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.