Saturday 9 December 2017

HOW TO FIND INVERSE OF A MATRIX USING C++?

INVERSE OF A MATRIX

This post is for CLASS 12 Students. Are you bored to find Determinant, Cofactor, Adjoint and Finally Inverse ?



What if you type the question and all the above things come directly on your computer screen.

First I will say how did i do this?

  •     Know the normal way
  •     Find the shortcut. Here I used a Shortcut.
  •     Write down Algorithm.
  •     Now Start Coding.
  •     You should be focussed on your output.
  •     Combine Maths and C++
  •     That's it. You did it.

Here is a sample.


Or what if Determinant is ZERO.Check this


What NEXT?..........CODE. Copy and paste it in your Notepad and change it into .cpp. Here is the code.

  1. #include<iostream.h>
  2. #include<conio.h>
  3. void get(int a[3][3])
  4. {
  5.  for(int i=0;i<3;i++)
  6.  {
  7.   for(int j=0;j<3;j++)
  8.   {
  9.    cout<<"Enter the value of...a["<<i+1<<"]["<<j+1<<"]....";
  10.    cin>>a[i][j];
  11.   }
  12.  }
  13. }
  14. void disp(int d[3][3])
  15. {
  16.  cout<<"\n";
  17.  for(int i=0;i<3;i++)
  18.  {
  19.   for(int j=0;j<3;j++)
  20.   {
  21.    cout<<"\t"<<d[i][j];
  22.   }
  23.   cout<<"\n";
  24.  }
  25. }
  26. int det(int a[3][3])
  27. {
  28. int mod;
  29. mod=(a[0][0]*(a[1][1]*a[2][2]-a[2][1]*a[1][2]))-(a[0][1]*(a[1][0]*a[2][2]-a[2][0]*a[1][2]))+(a[0][2]*(a[1][0]*a[2][1]-a[2][0]*a[1][1]));
  30. cout<<"\nDeterminant of A="<<mod;
  31. return mod;
  32. }
  33. void main()
  34. {
  35. clrscr();
  36. int a[3][3],mod,adj[3][3],co[3][3];
  37. get(a);
  38. disp(a);
  39. mod=det(a);
  40. cout<<"\nA Inverse is (1/Determinant of A)*Adjoint A";
  41.  if(mod==0)
  42.  {
  43.  cout<<"\nInverse doesnt exist.";
  44.  }
  45.  else
  46.  {
  47.  co[0][0]=a[1][1]*a[2][2]-a[2][1]*a[1][2];
  48.  co[0][1]=a[1][2]*a[2][0]-a[2][2]*a[1][0];
  49.  co[0][2]=a[1][0]*a[2][1]-a[2][0]*a[1][1];
  50.  co[1][0]=a[2][1]*a[0][2]-a[0][1]*a[2][2];
  51.  co[1][1]=a[2][2]*a[0][0]-a[0][2]*a[2][0];
  52.  co[1][2]=a[2][0]*a[0][1]-a[0][0]*a[2][1];
  53.  co[2][0]=a[0][1]*a[1][2]-a[1][1]*a[0][2];
  54.  co[2][1]=a[0][2]*a[1][0]-a[1][2]*a[0][0];
  55.  co[2][2]=a[0][0]*a[1][1]-a[1][0]*a[0][1];
  56.  cout<<"\nCofactor matrix...";
  57.  disp(co);
  58.  adj[0][0]=a[1][1]*a[2][2]-a[2][1]*a[1][2];
  59.  adj[1][0]=a[1][2]*a[2][0]-a[2][2]*a[1][0];
  60.  adj[2][0]=a[1][0]*a[2][1]-a[2][0]*a[1][1];
  61.  adj[0][1]=a[2][1]*a[0][2]-a[0][1]*a[2][2];
  62.  adj[1][1]=a[2][2]*a[0][0]-a[0][2]*a[2][0];
  63.  adj[2][1]=a[2][0]*a[0][1]-a[0][0]*a[2][1];
  64.  adj[0][2]=a[0][1]*a[1][2]-a[1][1]*a[0][2];
  65.  adj[1][2]=a[0][2]*a[1][0]-a[1][2]*a[0][0];
  66.  adj[2][2]=a[0][0]*a[1][1]-a[1][0]*a[0][1];
  67.  cout<<"\nAdjoint matrix...";
  68.  disp(adj);
  69.  }
  70. getch();
Now its your turn.Try making a code for 2x2 matrix.If you succeed comment below.


YOU can ask any other program like this in comments or you can  mail me.


Wednesday 6 December 2017

A SIMPLE GAME BY ME



A SIMPLE GAME BY ME

Do you remember this game?

Don't expect too much. In the place of T-REX there is a BALL and
obstacles are BLOCKS.I tried a little. Here is my GAMEPLAY

MY GAMEPLAY.



Eager to see the code.Totally 129 lines. R U READY?
  1. #include <graphics.h>
  2. #include<dos.h>
  3. #include<conio.h>
  4. #include<iostream.h>
  5. #include<stdio.h>
  6. #include <ctype.h>
  7. void main()
  8. {
  9. clrscr();
  10. int gd=DETECT,gm;
  11. initgraph(&gd,&gm,"c:\\turboc3\\bgi");
  12. int x=639,y=479;
  13. char a;int score=0;char abc[1];unsigned int speed=5;
  14. outtextxy(200,200,"HELLO! THIS IS MY FIRST GAME");
  15. outtextxy(170,220,"Press any key to continue....");
  16. getch();
  17. cleardevice();
  18. outtextxy(200,200,"PRESS 'SPACE' TO JUMP");
  19. outtextxy(250,220,"PRESS 'e' TO EXIT");
  20. outtextxy(200,240,"Press any key to continue....");
  21. getch();
  22. for(int i=1;i<=x;i++)
  23. {
  24. cleardevice();
  25. line(0,y,x,y);
  26. circle(80,y-25,25);
  27. line(x-30-i,y,x-30-i,y-60);
  28. line(x-50-i,y,x-50-i,y-60);
  29. line(x-30-i,y-60,x-50-i,y-60);delay(speed);
  30. if(i==484)
  31. {
  32. sound(500);
  33. delay(500);
  34. nosound();
  35. window(35,15,40,25);
  36. outtextxy(300,200,"GAME OVER");
  37. printf("Your Score is %d",score);
  38. outtextxy(200,260,"Press any key to exit....");
  39. getch();
  40. break;
  41. }
  42. else if(i<480&&i>350)
  43. {
  44. if(kbhit())
  45. {
  46. a=getch();
  47. int b=a;
  48. if(b==32)
  49. {
  50. cleardevice();
  51. for(int j=1;j<=100;j++)
  52. {
  53. cleardevice();
  54. line(0,y,x,y);
  55. circle(80,y-25-j,25);
  56. line(170-j,y,170-j,y-60);
  57. line(150-j,y,150-j,y-60);
  58. line(170-j,y-60,150-j,y-60);delay(speed);
  59. if(j==100)
  60. {
  61. for(int k=1;k<=100;k++)
  62. {
  63. cleardevice();
  64. line(0,y,x,y);
  65. circle(80,y-125+k,25);delay(speed);
  66. if(k==100)
  67. {
  68. break;
  69. }
  70. }
  71. }
  72. }
  73. ++score;--speed;
  74. }
  75. else if(a=='e')
  76. {
  77. window(35,15,40,25);
  78. outtextxy(200,200,"THANK YOU FOR PLAYING THE GAME");
  79. printf("Your Score is %d",score);
  80. outtextxy(200,260,"Press any key to exit....");
  81. getch();
  82. break;
  83. }

  84. else
  85. {
  86. continue;
  87. }
  88. i=1;
  89. continue;
  90. }
  91. }
  92. else if(kbhit())
  93. {
  94. a=getch();
  95. int b;
  96. if(b==32)
  97. {
  98. for(int j=1;j<=100;j++,i++)
  99. {
  100. cleardevice();
  101. line(0,y,x,y);
  102. circle(80,y-25-j,25);
  103. line(x-30-i,y,x-30-i,y-60);
  104. line(x-50-i,y,x-50-i,y-60);
  105. line(x-30-i,y-60,x-50-i,y-60);delay(speed);
  106. if(j==100)
  107. {
  108. for(int k=1;k<=100;k++,i++)
  109. {
  110. cleardevice();
  111. line(0,y,x,y);
  112. circle(80,y-125+k,25);
  113. line(x-30-i,y,x-30-i,y-60);
  114. line(x-50-i,y,x-50-i,y-60);
  115. line(x-30-i,y-60,x-50-i,y-60);delay(speed);

  116. if(k==100)
  117. {
  118. break;
  119. }
  120. }
  121. }

  122. }
  123. }
  124. }
  125. }
  126. }
If you have any doubts and queries comment below. 




Arranging your name in alphabetical order.

DIFFICULT IN ARRANGING SEQUENCE OF WORDS IN ALPHABETICAL ORDER? PERHAPS YOUR NAME?





TRY THE CODE BELOW.


  1. #include<iostream.h>
  2. #include<conio.h>
  3. #include<string.h>
  4. void main()
  5. {
  6. clrscr();
  7. char name[20],final[20];
  8. int number[20],order[20];
  9. cout<<"\nEnter the name..";
  10. cin>>name;
  11. int length=strlen(name);
  12. for(int i=0;i<length;i++)
  13.  {
  14.   number[i]=name[i];
  15.  }
  16.  int j=1,max=0;
  17.   while(j<=length)
  18.   {
  19.   max=0;
  20.    for(i=0;i<length;i++)
  21.    {
  22.      if(number[i]>max)
  23.      {
  24.      max=number[i];
  25.      order[length-j]=max;
  26.      }
  27.    }
  28.    for(int k=0;k<length;k++)
  29.    {
  30.     if(max==number[k])
  31.     {
  32.     number[k]=0;break;
  33.     }
  34.    }
  35.   j++;
  36.   }
  37. cout<<"\nAlphabetical order...";
  38. for(i=0;i<length;i++)
  39. {
  40. final[i]=order[i];
  41. cout<<final[i];
  42. }

  43. getch();

  44. }
DO YOU NEED YOUR NAME ALSO? COMMENT YOUR NAME AND NUMBER. I WILL SEND IT.


IF YOU HAVE ANY DOUBTS OR ANY OTHER QUERIES YOU ARE FREE TO COMMENT BELOW.

Tuesday 12 September 2017

Decimal to binary

Convert a number to its binary.

Copy the code and paste it in here

#include <iostream>
#include<conio.h>
using namespace std;
void main()
{
int q, r, a, b[16] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
int i = 0;
cout << "\nEnter a number between -32768 and 32767....";
cin >> a;
if (a>0)
{
q = a;
}
else
{
q = -a;
b[15] = 1;
}
cout << "\nBinary in 16-bit format ";
for (i = 0; q != 1; i++)
{
r = q % 2;
b[i] = r;
q /= 2;
}
b[i] = 1;
for (int j = 0; j<16; j++)
{
if (j % 4 == 0)
cout << " ";
cout << b[15 - j];
}
_getch();
}

Comment below.





Monday 11 September 2017

Doraemon

Do you need to see Doraemon? Try the code below.

#include "graphics.h"
#include <iostream>
using namespace std;
void main()
{
initwindow(800, 800, "Doraemon");
int x = 800, y = 800;
circle(x / 2, y / 2 - 20, 5);//nose
line(x / 2, y / 2 - 20 + 5, x / 2, y / 2);//nose
line(x / 2 - 20, y / 2, x / 2 + 20, y / 2);//mouth
arc(x / 2, y / 2, 180, 360, 20);//mouth
arc(x / 2, y / 2 + 20, 30, 150, 10);//toungue
ellipse(x / 2 - 10, y / 2 - 35, 0, 360, 8, 15);//left eye
ellipse(x / 2 - 10, y / 2 - 35, 0, 360, 2, 5);//left eye;//left eye dot
ellipse(x / 2 + 10, y / 2 - 35, 0, 360, 8, 15);//right eye
ellipse(x / 2 + 10, y / 2 - 35, 0, 360, 2, 5);//right eye dot
arc(x / 2, y / 2 - 10, 110, 250, 40);//left curve
arc(x / 2, y / 2 - 10, 290, 70, 40);//right curve
line(x / 2 - 15, y / 2 + 29, x / 2 + 15, y / 2 + 29);//line joining two curve
arc(x / 2, y / 2 - 20, 90, 270, 50);//left head
arc(x / 2, y / 2 - 20, 270, 90, 50);//right head
line(x / 2 - 20, y / 2 - 20, x / 2 - 45, y / 2 - 30);//left moustache
line(x / 2 - 20, y / 2 - 10, x / 2 - 45, y / 2 - 10);//left moustache
line(x / 2 - 20, y / 2, x / 2 - 45, y / 2 + 10);//left moustache
line(x / 2 + 20, y / 2 - 20, x / 2 + 45, y / 2 - 30);//right moustache
line(x / 2 + 20, y / 2 - 10, x / 2 + 45, y / 2 - 10);//right moustache
line(x / 2 + 20, y / 2, x / 2 + 45, y / 2 + 10);//right moustache
//body
arc(x / 2 - 15, y / 2 + 70, 90, 200, 40);//left hand arc
arc(x / 2 + 15, y / 2 + 70, 340, 90, 40);//right hand arc
circle(x / 2 - 42, y / 2 + 80, 10);//left hand palm
circle(x / 2 + 42, y / 2 + 80, 10);//right hand palm
ellipse(x / 2 - 20, y / 2 + 65, 90, 250, 15, 40);//left body
ellipse(x / 2 + 20, y / 2 + 65, 290, 90, 15, 40);//right body
ellipse(x / 2 - 20, y / 2 + 110, 0, 360, 20, 10);//left shoe
ellipse(x / 2 + 20, y / 2 + 110, 0, 360, 20, 10);//right shoe
arc(x / 2, y / 2 + 60, 180, 360, 20);//pocket
line(x / 2 - 20, y / 2 + 60, x / 2 + 20, y / 2 + 60);//pocket
circle(x / 2, y / 2 + 39, 10);//bell
arc(x / 2, y / 2 + 70, 180, 360, 25);//curve between body and pocket
ellipse(x / 2 - 5, y / 2 + 60, 100, 200, 20, 30);//curve between body and pocket
ellipse(x / 2 + 5, y / 2 + 60, 340, 100, 20, 30);//curve between body and pocket

getch();
}



The output will be


 Comment your views below. Ask any other characters.

Sunday 10 September 2017

Calculator

This is my Calculator program. In this program you can enter the entire expression instead of only 2 numbers.Try it and comment below.




#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
int a,b;
char c;
system("cls");
cout << "Enter the expression....";
cout << "\nPress '= 'after entering expression....";
cin >> a;
do
{
cin >> c;
switch (c)
{
case '+':cin >> b; a += b; break;
case '-':cin >> b; a -= b; break;
case '*':cin >> b; a *= b; break;
case '/':cin >> b; a /= b; break;
}
} while (c != '=');
cout << a;
_getch();
}