C++ coding

Status
Not open for further replies.

CA50

Cyborg Agent
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;
}
 
first i suggest reading Let us C by yashvant Kanetkar

#includes ....

void display(int mat[],int row,int col)
{
i<row
j<col
cout<<*mat+i+j;
cout<<endl;
}

int *add(int mat1[],int mat2[],int r1,int r2,int c1, int c2)
{
adding & storing in sum[][];
return *sum;
}

main()
{
case 1:
add(.......);
case 2:
......
 

Liverpool_fan

Sami Hyypiä, LFC legend
first i suggest reading Let us C by yashvant Kanetkar
:lol: What? Let us C is a C book. WTF will he do with a C book eh? :rolleyes:
And it's a crap C book too. :/

The problem with people is that they think C and C++ have similarites which is absolutely NOT the case. Most people end up with C practices in C++ and vice versa which is FAIL.

Anyway for a Beginner, Robert Lafore's book (the latest one, not the older Turbo C++ one is pretty okay), though thinking in C++ is one of the best (and eBook is free) in spite of the fact it's a bit advanced.

Anyway you CAN begin with C if you wish, with a book like C Programming Language by Kernighan and Ritchie; but be prepared to UNLEARN C concepts A LOT while moving to C++.
 
^^ true ... but for a person with little knowledge of functions, i think basic C concepts should be learnt first...

without this C or C++ cannot be understood properly
 
OP
CA50

CA50

Cyborg Agent
thanks friends for your wise advices, actually i did some C and now just trying to code those previous c programs in C++. Anyway i coded the whole matrix problem and it is really great. I am contented
 
Status
Not open for further replies.
Top Bottom