C++ coding

CA50

Skilled
I am currently working on a c++ program to do all the martix operations like addition, subtraction, multiplication and transpose.

But i am little weak in that "Function call / declaration etc." , so i am feeling difficulties in coding the program.

I am sending my partially completed code:
Please tell me how to initialize a function to add and display, and also the function itself.

Code:
#include<iostream.h>
#include<conio.h>
#define max_row 10
#define max_col 10 
int main()
{
      //theis program will add two matrix of 3x4
      int mat1[max_row][max_col],mat2[max_row][max_col],mat3[max_row][max_col],row,col,i,j,value,n;
      cout<<"\n\t\t\t  OPERATIONS ON MATRICES\n\t\t    Coded by CA50 on 22/03/10 08:40\n\n";
      cout<<"Enter the number of rows for MATRIX 1 (max is 10):\a ";
      cin>>row;
      cout<<"Enter the number of columns for MATRIX 1 (max is 10):\a ";
      cin>>col;
      if((row<=max_row)&&(col<=max_col))
      {                                
          cout<<"\nYou have sucessfully created MATRIX 1 of ("<<row<<"x"<<col<<").\n";
          for(i=0;i<row;i++)
          {
              for(j=0;j<col;j++)
              {
                  cout<<"Enter value for row-"<<i<<" col-"<<j<<" ";
                  cin>>value;
                  mat1[i][j]=value;
              }
              
          }
      }
      else
      {
          cout<<"\n\n\aMatrix generation failed. You have exceeded the maximum size limitation.\n";
          getch();
          exit(0);
      }
      cout<<"\n";
      cout<<"Enter the number of rows for MATRIX 2 (max is 10):\a ";
      cin>>row;
      cout<<"Enter the number of columns for MATRIX 2 (max is 10):\a ";
      cin>>col;
      if((row<=max_row)&&(col<=max_col))
      {
          cout<<"\nYou have sucessfully created MATRIX 2 of ("<<row<<"x"<<col<<").\n";
          for(i=0;i<row;i++)
          {
              for(j=0;j<col;j++)
              {
                  cout<<"Enter value for row-"<<i<<" col-"<<j<<" ";
                  cin>>value;
                  mat2[i][j]=value;
              }
          }
      }
      else
      {
          cout<<"\n\n\aMatrix generation failed. You have exceeded the maximum size limitation.\n";
          getch();
          exit(0);
      }
      //menu creation
      system("cls");
      cout<<"\n\t\t\t  OPERATIONS ON MATRICES\n\t\t    Coded by CA50 on 22/03/10 08:40\n\n";
      cout<<"----- MATRIX MENU -----\n1. Matrix addtion.\n2. Matrix Subtraction.\n3. Matrix multiplication.\n4. Transpose of a matrix.\nEnter your choice ";
      cin>>n;
      switch(n)
      {
               case 1:
                    cout<<"\nThe result of matrix addition is:\n";
                    //call matrix addition function here                
               case 2:
                    //call matrix subtraction function
               case 3:
                    //call matrix multiplication function
               case 4:
                    //call transpose function
               default :
                       cout<<"\n\nINVALID CHOICE\n\nPress any key to exit...";
                       getch();
                       exit (0);
      }
      return 0;
}
 
well, u need to write the prototype of the functions to Add and Display and then the function can go after the main function.

for ex to add two numbers u would do as follows,
#include<iostream.h>
#include<conio.h>

void Add(int a, int b); //function prototype

void main()
{
int a,b;
cin >>b;
cin >>b;
add(a,b);
}

void add (int a, int b) //function definition
{
cout << a+b;
}
 
Sorry that first reply(oops, im second!) wont be of much help. But I couldnt stop from adding few comments.

Your code doesnt represent any '++' of C++. If its not a homework problem and you have time, try to do the following -

1. Change 'max is 10' so that the values of max_row and max_col are used. In case you change them in future, you dont have to bother about changing it at many places.

2. Your program is a pretty good candidate for learning some parts of '++' of C++. Make the matrices as objects and you can learn about operator overloading.

3. You can make it as a pretty good lib and also a command driven application which uses the library. If you are doing this for any submission/presentation work, ppl will be impressed.

good luck
 
Quick tip: use clrscr () function instead of system("cls") ..

Lets see now..

You have inputted 2 matrices from the user in main() function.

Going along that logic what you are looking for is to create 5 funtions ..

1. addition ()

2. substraction ()

3. division ()

4. multiplication ().

5. print ()

Ok..

So first off..

Q> HOW TO DECLARE A FUNCTION ?? (pardon the caps.. :p)

A> [return type] [function name] ([argument types , , ]);

eg. void addition (int [][],int [][]);

This is called declaring a function. Please note that we have not yet 'defined' the function but just created a reference for future use.

Q> HOW TO DEFINE A FUNCTION ??

A>

[return type] [function name] ([argument variables , , ])

{

< ------- BODY ---------->

}

This is the syntax for defining a function.

Code:
Lets quickly jump into the addition() function.

void addition (int a[10][10],b[10][10], int x, int y)

{

 \\ Here a & b are the matrices passed from the main program.

 \\ x & y are the row n column size of matrix a & b. Assuming the matrix addition 

 \\ rule that both matrix are of same dimension.

int temp[max_row][max_col];

for (int i = 0 ; i < x; i++)

{

 for(int o = 0 ; o < y ; o++)

  {

   temp [i][o]=a[i][o]+b[i][o];

  }

}

print(temp,x,y);

}

void print (int p[10][10],int x, int y)

{

 cout<<"Result"<<endl; 

 for (int i = 0 ; i < x; i++)

  {

  for(int o = 0 ; o < y ; o++)

   {

     cout<<p[i][o]<<" ";

   }

   cout <<endl;

 }

}

int main()

{

      //theis program will add two matrix of 3x4

      int mat1[max_row][max_col],mat2[max_row][max_col],mat3[max_row][max_col],row,col,i,j,value,n;

      cout<<"\n\t\t\t  OPERATIONS ON MATRICES\n\t\t    Coded by CA50 on 22/03/10 08:40\n\n";

      cout<<"Enter the number of rows for MATRIX 1 (max is 10):\a ";

      cin>>row;

      cout<<"Enter the number of columns for MATRIX 1 (max is 10):\a ";

      cin>>col;

      if((row<=max_row)&&(col<=max_col))

      {                                

          cout<<"\nYou have sucessfully created MATRIX 1 of ("<<row<<"x"<<col<<").\n";

          for(i=0;i<row;i++)

          {

              for(j=0;j<col;j++)

              {

                  cout<<"Enter value for row-"<<i<<" col-"<<j<<" ";

                  cin>>value;

                  mat1[i][j]=value;

              }

              

          }

      }

      else

      {

          cout<<"\n\n\aMatrix generation failed. You have exceeded the maximum size limitation.\n";

          getch();

          exit(0);

      }

      cout<<"\n";

      cout<<"Enter the number of rows for MATRIX 2 (max is 10):\a ";

      cin>>row;

      cout<<"Enter the number of columns for MATRIX 2 (max is 10):\a ";

      cin>>col;

      if((row<=max_row)&&(col<=max_col))

      {

          cout<<"\nYou have sucessfully created MATRIX 2 of ("<<row<<"x"<<col<<").\n";

          for(i=0;i<row;i++)

          {

              for(j=0;j<col;j++)

              {

                  cout<<"Enter value for row-"<<i<<" col-"<<j<<" ";

                  cin>>value;

                  mat2[i][j]=value;

              }

          }

      }

      else

      {

          cout<<"\n\n\aMatrix generation failed. You have exceeded the maximum size limitation.\n";

          getch();

          exit(0);

      }

      //menu creation

      system("cls");

      cout<<"\n\t\t\t  OPERATIONS ON MATRICES\n\t\t    Coded by CA50 on 22/03/10 08:40\n\n";

      cout<<"----- MATRIX MENU -----\n1. Matrix addtion.\n2. Matrix Subtraction.\n3. Matrix multiplication.\n4. Transpose of a matrix.\nEnter your choice ";

      cin>>n;

      switch(n)

      {

               case 1:

                    cout<<"\nThe result of matrix addition is:\n";

                    addition (mat1,mat2,row,col);               

               case 2:

                    //call matrix subtraction function

               case 3:

                    //call matrix multiplication function

               case 4:

                    //call transpose function

               default :

                       cout<<"\n\nINVALID CHOICE\n\nPress any key to exit...";

                       getch();

                       exit (0);

      }

     

      return 0;

}

Please compile n check back.
 
Oh! this is awesome, i posted the same question in TDF (few miniutes before TE) and PF(1-2 days back), but here the thing is different. i posted the question and went to market, now i am bombarded with answers. thanks thanks everybody :thanks!:

bhaskarvyas001 said:
well, u need to write the prototype of the functions to Add and Display and then the function can go after the main function.

for ex to add two numbers u would do as follows,
#include<iostream.h>
#include<conio.h>

void Add(int a, int b); //function prototype

void main()
{
int a,b;
cin >>b;
cin >>b;
add(a,b);
}

void add (int a, int b) //function definition
{
cout << a+b;
}

Thanks bhaskar for your quick tutorial on FUNCTIONs.

random2 said:
Sorry that first reply(oops, im second!) wont be of much help. But I couldnt stop from adding few comments.

Your code doesnt represent any '++' of C++. If its not a homework problem and you have time, try to do the following -

1. Change 'max is 10' so that the values of max_row and max_col are used. In case you change them in future, you dont have to bother about changing it at many places.

2. Your program is a pretty good candidate for learning some parts of '++' of C++. Make the matrices as objects and you can learn about operator overloading.

3. You can make it as a pretty good lib and also a command driven application which uses the library. If you are doing this for any submission/presentation work, ppl will be impressed.

good luck

Thanks for your reply, actually till now i havn`t learnt anything on objects and class, currently i am just rying to convert the C programs to c++ ones. Can you please explain you point no. 1.
dexBG said:
Quick tip: use clrscr () function instead of system("cls") ..
Lets see now..

You have inputted 2 matrices from the user in main() function.
Going along that logic what you are looking for is to create 5 funtions ..

1. addition ()
2. substraction ()
3. division ()
4. multiplication ().
5. print ()

Ok..
So first off..

Q> HOW TO DECLARE A FUNCTION ?? (pardon the caps.. :p)
A> [return type] [function name] ([argument types , , ]);

eg. void addition (int [][],int [][]);
This is called declaring a function. Please note that we have not yet 'defined' the function but just created a reference for future use.

Q> HOW TO DEFINE A FUNCTION ??
A>
[return type] [function name] ([argument variables , , ])
{
< ------- BODY ---------->
}

This is the syntax for defining a function.

Code:
Lets quickly jump into the addition() function.

void addition (int a[10][10],b[10][10], int x, int y)
{
 \\ Here a & b are the matrices passed from the main program.
 \\ x & y are the row n column size of matrix a & b. Assuming the matrix addition 
 \\ rule that both matrix are of same dimension.

int temp[max_row][max_col];

for (int i = 0 ; i < x; i++)
{
 for(int o = 0 ; o < y ; o++)
  {
   temp [i][o]=a[i][o]+b[i][o];
  }
}

print(temp,x,y);

}
void print (int p[10][10],int x, int y)
{
 cout<<"Result"<<endl; 
 for (int i = 0 ; i < x; i++)
  {
  for(int o = 0 ; o < y ; o++)
   {
     cout<<p[i][o]<<" ";
   }
   cout <<endl;
 }

}

int main()
{
      //theis program will add two matrix of 3x4
      int mat1[max_row][max_col],mat2[max_row][max_col],mat3[max_row][max_col],row,col,i,j,value,n;
      cout<<"\n\t\t\t  OPERATIONS ON MATRICES\n\t\t    Coded by CA50 on 22/03/10 08:40\n\n";
      cout<<"Enter the number of rows for MATRIX 1 (max is 10):\a ";
      cin>>row;
      cout<<"Enter the number of columns for MATRIX 1 (max is 10):\a ";
      cin>>col;
      if((row<=max_row)&&(col<=max_col))
      {                                
          cout<<"\nYou have sucessfully created MATRIX 1 of ("<<row<<"x"<<col<<").\n";
          for(i=0;i<row;i++)
          {
              for(j=0;j<col;j++)
              {
                  cout<<"Enter value for row-"<<i<<" col-"<<j<<" ";
                  cin>>value;
                  mat1[i][j]=value;
              }
              
          }
      }
      else
      {
          cout<<"\n\n\aMatrix generation failed. You have exceeded the maximum size limitation.\n";
          getch();
          exit(0);
      }
      cout<<"\n";
      cout<<"Enter the number of rows for MATRIX 2 (max is 10):\a ";
      cin>>row;
      cout<<"Enter the number of columns for MATRIX 2 (max is 10):\a ";
      cin>>col;
      if((row<=max_row)&&(col<=max_col))
      {
          cout<<"\nYou have sucessfully created MATRIX 2 of ("<<row<<"x"<<col<<").\n";
          for(i=0;i<row;i++)
          {
              for(j=0;j<col;j++)
              {
                  cout<<"Enter value for row-"<<i<<" col-"<<j<<" ";
                  cin>>value;
                  mat2[i][j]=value;
              }
          }
      }
      else
      {
          cout<<"\n\n\aMatrix generation failed. You have exceeded the maximum size limitation.\n";
          getch();
          exit(0);
      }
      //menu creation
      system("cls");
      cout<<"\n\t\t\t  OPERATIONS ON MATRICES\n\t\t    Coded by CA50 on 22/03/10 08:40\n\n";
      cout<<"----- MATRIX MENU -----\n1. Matrix addtion.\n2. Matrix Subtraction.\n3. Matrix multiplication.\n4. Transpose of a matrix.\nEnter your choice ";
      cin>>n;
      switch(n)
      {
               case 1:
                    cout<<"\nThe result of matrix addition is:\n";
                    addition (mat1,mat2,row,col);               
               case 2:
                    //call matrix subtraction function
               case 3:
                    //call matrix multiplication function
               case 4:
                    //call transpose function
               default :
                       cout<<"\n\nINVALID CHOICE\n\nPress any key to exit...";
                       getch();
                       exit (0);
      }

     
      return 0;
}
Please compile n check back.
Thanks mate, first of all i compile using DevC++, so it doesn`t support clrscr(), so i have to stick with system("cls"). And thanks a lot for your codes, i will work on this right now.

And last of all this code is neither homework nor projects. I am currently doing BCA(2nd sem), i learnt C in 1st sem and i coded the matrix programs seperately, so now i am just trying to combine all the programs into one. thats it friends.:)

thanks mate its working, addition is working i will figure out the rest, i will post the whole code after completion.
Bye-bye time to code
 
CA50 said:
Can you please explain you point no. 1.

you have few lines in the code as below -
cout<<"Enter the number of columns for MATRIX 1 (max is 10):\a ";

Now, if you change the max rows and columns in the code to 12 or 15, you would have to change 10 at more places. Instead, use the constant you have already defined like below.

cout<<"Enter the number of columns for MATRIX 1 (max is " <<max_col <<"):\a" <<endl;

and similarly for max_row. Now, If you want to change the max values sometime, all you have to do is change it in the constant definition and rest will use the same. These are very simple things that one has to take care of.

CA50 said:
And last of all this code is neither homework nor projects. I am currently doing BCA(2nd sem), i learnt C in 1st sem and i coded the matrix programs seperately, so now i am just trying to combine all the programs into one. thats it friends.:)
Good that you are doing this.
 
^^Thanks random2, i will change my code now. I understood your point.

Hi everybody, with your help, i finished the coding. But there is a little problem in the multiplication part.
Here is the code, just compile and try it.
Suggestion are always welcome
I have also attached the exe and the cpp file, for those who are too lazy to compile:mad: and run.:cool2:
 
Hi everybody i removed the previous attachment, here is a new version of multi-matrix

Try this and post your replies.
 
Back
Top