Showing posts with label matrix. Show all posts
Showing posts with label matrix. Show all posts

Write a program to sort elements of array using Insert Sorting

Size of array is taken from user.. Elements are entered by user in any order... Sorting is performed..

Select To use this code as it is.. select and copy paste this code into code.cpp file :)
  1. #include<stdio.h> 

  2. main() 


  3. int a[100],n,k,i,j,temp; 

  4. printf("How many elements: \n"); 

  5. scanf("%d",&n); 

  6. printf("Enter the element of array: \n"); 

  7. for(i=0;i<=n-1;i++) 

  8. scanf("%d",&a[i]); 

  9. for(k=1;k<=n-1;k++) 


  10. temp=a[k]; 

  11. j=k-1; 

  12. //Coding by: Snehil Khanor 

  13. //http://WapCPP.blogspot.com 

  14. while((temp<a[j])&&(j>=0)) 


  15. a[j+1]=a[j]; 

  16. j=j-1; 


  17. a[j+1]=temp; 


  18. printf("Elements of array after sorting: \n"); 

  19. for(i=0;i<=n-1;i++) 

  20. printf("%d\n",a[i]); 

  21. getch();


Write a program to find transpose of a matrix

A matrix is taken from user.. and its Transpose is displayed as output..


Select To use this code as it is.. select and copy paste this code into code.cpp file :)



  1. #include<iostream.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. clrscr();
  6. int a[10][10],m,n,i,j;
  7. cout<<"Enter number of rows: ";
  8. cin>>m;
  9. cout<<"Enter number of coloumns: ";
  10. cin>>n;
  11. //Coding by: Snehil Khanor
  12. //http://WapCPP.blogspot.com
  13. if(m!=n)
  14. {
  15. cout<<"Matrix not square so Transpose not possible :(";
  16. }
  17. else
  18. {
  19. cout<<endl<<"Enter elements of matrix: "<<endl;
  20. for(i=0;i<m;i++)
  21. {
  22. for(j=0;j<n;j++)
  23. {
  24. cout<<"Enter element a"<<i+1<<j+1<<": ";
  25. cin>>a[i][j];
  26. }
  27. }
  28. cout<<endl<<"Displaying Matrix: "<<endl<<endl;
  29. for(i=0;i<m;i++)
  30. {
  31. for(j=0;j<n;j++)
  32. {
  33. cout<<a[i][j]<<" ";
  34. }
  35. cout<<endl<<endl;
  36. }
  37. cout<<endl<<"Displaying Matrix Transpose: "<<endl<<endl;
  38. for(i=0;i<m;i++)
  39. {
  40. for(j=0;j<n;j++)
  41. {
  42. cout<<a[j][i]<<" ";
  43. }
  44. cout<<endl<<endl;
  45. }
  46. }
  47. getch();
  48. }

Write a program for multiplication of two matrices

Two matrices are taken from user..multiplication matrix is displayed as output.


Select To use this code as it is.. select and copy paste this code into code.cpp file :)



  1. #include<iostream.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. clrscr();
  6. int a[10][10],b[10][10],c[10][10],m,n,o,p,i,j;
  7. cout<<"Enter number of rows of A: ";
  8. cin>>m;
  9. cout<<"Enter number of coloumns of A: ";
  10. cin>>n;
  11. cout<<endl<<"Enter elements of matrix A: "<<endl;
  12. //Coding by: Snehil Khanor
  13. //http://WapCPP.blogspot.com
  14. for(i=0;i<m;i++)
  15. {
  16. for(j=0;j<n;j++)
  17. {
  18. cout<<"Enter element a"<<i+1<<j+1<<": ";
  19. cin>>a[i][j];
  20. }
  21. }
  22. cout<<endl<<"Enter number of rows of B: ";
  23. cin>>o;
  24. cout<<"Enter number of coloumns of B: ";
  25. cin>>p;
  26. cout<<endl<<"Enter elements of matrix B: "<<endl;
  27. for(i=0;i<o;i++)
  28. {
  29. for(j=0;j<p;j++)
  30. {
  31. cout<<"Enter element b"<<i+1<<j+1<<": ";
  32. cin>>b[i][j];
  33. }
  34. }

  35. cout<<endl<<"Displaying Matrix A: "<<endl<<endl;
  36. for(i=0;i<m;i++)
  37. {
  38. for(j=0;j<n;j++)
  39. {
  40. cout<<a[i][j]<<" ";
  41. }
  42. cout<<endl<<endl;
  43. }
  44. cout<<endl<<"Displaying Matrix B: "<<endl<<endl;
  45. for(i=0;i<o;i++)
  46. {
  47. for(j=0;j<p;j++)
  48. {
  49. cout<<b[i][j]<<" ";
  50. }
  51. cout<<endl<<endl;
  52. }
  53. if(n==o)
  54. {
  55. for(i=0;i<m;i++)
  56. {
  57. for(j=0;j<p;j++)
  58. {
  59. c[i][j]=0;
  60. for(int k=0;k<n;k++)
  61. {
  62. c[i][j]=c[i][j]+a[i][k]*b[k][j];
  63. }
  64. }
  65. }
  66. cout<<endl<<"Matrix A * Matrix B = Matrix C: "<<endl<<endl;
  67. for(i=0;i<m;i++)
  68. {
  69. for(j=0;j<p;j++)
  70. {
  71. cout<<c[i][j]<<" ";
  72. }
  73. cout<<endl<<endl;
  74. }
  75. }
  76. else
  77. cout<<"Multiplication not possible :(";
  78. getch();
  79. }

Write a program to find Difference of two matrix

Two matrix are taken from user.. Substraction matrix is displayed as output..


Select To use this code as it is.. select and copy paste this code into code.cpp file :)



  1. #include<iostream.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. clrscr();
  6. int a[10][10],b[10][10],c[10][10],m,n,i,j;
  7. cout<<"Enter number of rows: ";
  8. cin>>m;
  9. cout<<"Enter number of coloumns: ";
  10. cin>>n;
  11. cout<<endl<<"Enter elements of matrix A: "<<endl;
  12. //Coding by: Snehil Khanor
  13. //http://WapCPP.blogspot.com
  14. for(i=0;i<m;i++)
  15. {
  16. for(j=0;j<n;j++)
  17. {
  18. cout<<"Enter element a"<<i+1<<j+1<<": ";
  19. cin>>a[i][j];
  20. }
  21. }
  22. cout<<endl<<"Enter elements of matrix B: "<<endl;
  23. for(i=0;i<m;i++)
  24. {
  25. for(j=0;j<n;j++)
  26. {
  27. cout<<"Enter element b"<<i+1<<j+1<<": ";
  28. cin>>b[i][j];
  29. }
  30. }
  31. //Coding by: Snehil Khanor
  32. //http://WapCPP.blogspot.com
  33. cout<<endl<<"Displaying Matrix A: "<<endl<<endl;
  34. for(i=0;i<m;i++)
  35. {
  36. for(j=0;j<n;j++)
  37. {
  38. cout<<a[i][j]<<" ";
  39. }
  40. cout<<endl<<endl;
  41. }
  42. cout<<endl<<"Displaying Matrix B: "<<endl<<endl;
  43. for(i=0;i<m;i++)
  44. {
  45. for(j=0;j<n;j++)
  46. {
  47. cout<<b[i][j]<<" ";
  48. }
  49. cout<<endl<<endl;
  50. }
  51. cout<<endl<<"Matrix A - Matrix B = Matrix C: "<<endl<<endl;
  52. for(i=0;i<m;i++)
  53. {
  54. for(j=0;j<n;j++)
  55. {
  56. cout<<a[i][j]-b[i][j]<<" ";
  57. }
  58. cout<<endl<<endl;
  59. }
  60. getch();
  61. }

Write a program to find sum matrix of two matrix

Two matrix are taken from user.. Sum matrix is displayed as output..


Select To use this code as it is.. select and copy paste this code into code.cpp file :)



  1. #include<iostream.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. clrscr();
  6. int a[10][10],b[10][10],c[10][10],m,n,i,j;
  7. cout<<"Enter number of rows: ";
  8. cin>>m;
  9. cout<<"Enter number of coloumns: ";
  10. cin>>n;
  11. cout<<endl<<"Enter elements of matrix A: "<<endl;
  12. for(i=0;i<m;i++)
  13. {
  14. for(j=0;j<n;j++)
  15. {
  16. cout<<"Enter element a"<<i+1<<j+1<<": ";
  17. cin>>a[i][j];
  18. }
  19. }
  20. cout<<endl<<"Enter elements of matrix B: "<<endl;
  21. //Coding by: Snehil Khanor
  22. //http://WapCPP.blogspot.com
  23. for(i=0;i<m;i++)
  24. {
  25. for(j=0;j<n;j++)
  26. {
  27. cout<<"Enter element b"<<i+1<<j+1<<": ";
  28. cin>>b[i][j];
  29. }
  30. }

  31. cout<<endl<<"Displaying Matrix A: "<<endl<<endl;
  32. for(i=0;i<m;i++)
  33. {
  34. for(j=0;j<n;j++)
  35. {
  36. cout<<a[i][j]<<" ";
  37. }
  38. cout<<endl<<endl;
  39. }
  40. cout<<endl<<"Displaying Matrix B: "<<endl<<endl;
  41. for(i=0;i<m;i++)
  42. {
  43. for(j=0;j<n;j++)
  44. {
  45. cout<<b[i][j]<<" ";
  46. }
  47. cout<<endl<<endl;
  48. }
  49. cout<<endl<<"Matrix A + Matrix B = Matrix C: "<<endl<<endl;
  50. for(i=0;i<m;i++)
  51. {
  52. for(j=0;j<n;j++)
  53. {
  54. cout<<a[i][j]+b[i][j]<<" ";
  55. }
  56. cout<<endl<<endl;
  57. }

  58. getch();
  59. }

Write a program to store elements in m x n 2D array (Matrix)

m(row) and n(coloumns) are taken from user.. Elements are taken from user.. matrix is displayed in tabular form..
This should be your first program for Matrix.


Select To use this code as it is.. select and copy paste this code into code.cpp file :)



  1. #include<iostream.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. clrscr();
  6. int a[10][10],m,n,i,j;
  7. cout<<"Enter number of rows: ";
  8. cin>>m;
  9. cout<<"Enter number of coloumns: ";
  10. cin>>n;
  11. cout<<endl<<"Enter elements of matrix: "<<endl;
  12. //Coding by: Snehil Khanor
  13. //http://WapCPP.blogspot.com
  14. for(i=0;i<m;i++)
  15. {
  16. for(j=0;j<n;j++)
  17. {
  18. cout<<"Enter element a"<<i+1<<j+1<<": ";
  19. cin>>a[i][j];
  20. }
  21. }
  22. cout<<endl<<"Displaying Matrix: "<<endl<<endl;
  23. for(i=0;i<m;i++)
  24. {
  25. for(j=0;j<n;j++)
  26. {
  27. cout<<a[i][j]<<" ";
  28. }
  29. cout<<endl<<endl;
  30. }
  31. getch();
  32. }

Check out this stream