#include<iostream.h>
#include<iomanip.h>
int main()
{
const int maxsize=10;
int matrix[maxsize][maxsize];
int i,j,rows,cols, swap;
bool quit=true;
char mop;
/*************Matrix Input Starts************/
do
{
cout<<"Enter No.of Rows Between 0 To 10: ";
cin>>rows;
if(rows<0 || rows>11)
cout<<"Please Enter in Range."<<endl;
}
while(rows<0 || rows>11);
do
{
cout<<"Enter No.of Cols Between 0 To 10 : ";
cin>>cols;
if(cols<0 || cols>11)
cout<<"Please Enter in Range."<<endl;
}
while(cols<0 || cols>11);
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
cout<<"Enter element at index["<<i<<"]["<<j<<"] ";
cin>>matrix[i][j];
}
}
cout<<endl<<"Orignal Matrix"<<endl;
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
cout<<matrix[i][j]<<"\t";
}
cout<<endl;
}
/************* Matrix Input Ends ************/
cout<<endl<<endl;
/************* Matrix Operational Loop Starts ************/
while(quit)
{
do
{
cout<<endl;
cout<<setw(39)<<"Matrix Operations"<<endl;
cout<<setw(40)<<"Transpose : [T]"<<endl;
cout<<setw(40)<<"Insert : [I]"<<endl;
cout<<setw(40)<<"Delete : [D]"<<endl;
cout<<setw(40)<<"Find : [F]"<<endl;
cout<<setw(40)<<"Swap : [W]"<<endl;
cout<<setw(40)<<"Print : [P]"<<endl;
cout<<setw(40)<<"Quit : [Q]"<<endl;
cout<<setw(35)<<"Enter Option: ";
cin>>mop;
}
while(mop!='T' && mop!='t' && mop!='I' && mop!='i' && mop!='D' && mop!='d' && mop!='F'
&& mop!='f' && mop!='P' && mop!='p' && mop!='W' && mop!='w' && mop!='Q' && mop!='q');
cout<<endl<<endl;
switch(mop)
{
/***** CASE OT TRANSPOSE *****/
case 'T':
case 't':
{
int tmatrix[maxsize][maxsize];
swap=rows;
rows=cols;
cols=swap;
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
tmatrix[i][j]=matrix[j][i];
}
}
for(i=0;i<=rows;i++)
{
for(j=0;j<cols;j++)
{
matrix[i][j]=tmatrix[i][j];
}
}
cout<<endl<<"Transposed Matrix"<<endl;
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
cout<<matrix[i][j]<<"\t";
}
cout<<endl;
}
break;
}
/***** CASE OF INSERTION *****/
case 'I':
case 'i':
{
char ins;
int rowindex,colindex;
do
{
cout<<"Enter R To Insert Row OR C To Insert column: ";
cin>>ins;
if (ins!='R' && ins!='r' && ins!='C' && ins!='c')
{
cout<<endl<<"Enter Valid Input"<<endl<<endl;
}
}
while(ins!='R' && ins!='r' && ins!='C' && ins!='c');
switch(ins)
{
/***** CASE TO INSERT A ROW *****/
case 'R':
case 'r':
{
do
{
cout<<"Enter Index Where To Insert a Row: ";
cin>>rowindex;
}
while(rowindex>=rows+1);
for(i=rows;i>rowindex;i--)
{
for(j=0;j<cols;j++)
{
matrix[i][j]=matrix[i-1][j];
}
}
for(j=0;j<cols;j++)
{
cout<<"Enter Element At Index["<<rowindex<<"]["<<j<<"] ";
cin>>matrix[rowindex][j];
}
cout<<endl<<"Matrix After Inserting A Row"<<endl;
for(i=0;i<=rows;i++)
{
for(j=0;j<cols;j++)
{
cout<<matrix[i][j]<<"\t";
}
cout<<endl;
}
rows++;
break;
}
/***** CASE TO INSERT A COLUMN *****/
case 'C':
case 'c':
{
do
{
cout<<"Enter Index Where To Insert a Col: ";
cin>>colindex;
}
while(colindex>=cols+1);
for(i=0;i<rows;i++)
{
for(j=cols;j>colindex;j--)
{
matrix[i][j]=matrix[i][j-1];
}
}
for(i=0;i<rows;i++)
{
cout<<"Enter Element At Index["<<i<<"]["<<colindex<<"] ";
cin>>matrix[i][colindex];
}
cout<<endl<<"Matrix After Inserting A Column"<<endl;
for(i=0;i<rows;i++)
{
for(j=0;j<=cols;j++)
{
cout<<matrix[i][j]<<"\t";
}
cout<<endl;
}
cols++;
break;
}
}
break;
}
/***** CASE OF DELETION *****/
case 'D':
case 'd':
{;
char del;
int rowindex,colindex;
do
{
cout<<"Enter R To Delete Row OR C TO Delete Column: ";
cin>>del;
if(del!='R' && del!='r' && del!='C' && del!='c')
{
cout<<endl<<"Enter Valid Input"<<endl<<endl;
}
}
while(del!='R' && del!='r' && del!='C' && del!='c');
switch(del)
{
/***** CASE TO DELETE A ROW *****/
case 'R':
case 'r':
{
do
{
cout<<"Enter Row Index that You want to Delete: ";
cin>>rowindex;
}
while(rowindex<0 || rowindex>=rows);
for(i=rowindex;i<rows;i++)
{
for(j=0;j<cols;j++)
{
matrix[i][j]=matrix[i+1][j];
}
}
cout<<endl<<"Matrix After Deleting A Row"<<endl;
for(i=0;i<rows-1;i++)
{
for(j=0;j<cols;j++)
{
cout<<matrix[i][j]<<"\t";
}
cout<<endl;
}
rows--;
break;
}
/***** CASE TO DELETE A COLUMN *****/
case 'C':
case 'c':
{
do
{
cout<<"Enter Column Index that You want to Delete: ";
cin>>colindex;
}
while(colindex<0 || colindex>=cols);
for(i=0;i<rows;i++)
{
for(j=colindex;j<cols;j++)
{
matrix[i][j]=matrix[i][j+1];
}
}
cout<<endl<<"Matrix After Deleting A Column"<<endl;
for(i=0;i<rows;i++)
{
for(j=0;j<cols-1;j++)
{
cout<<matrix[i][j]<<"\t";
}
cout<<endl;
}
cols--;
break;
}
}
break;
}
/***** CASE OF FINDING A VALUE *****/
case 'F':
case 'f':
{
int value;
bool flag=false;
cout<<"Enter Value To Find In MAtrix: ";
cin>>value;
cout<<endl;
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
if(matrix[i][j]==value)
{
cout<<"Value Placed At Index ["<<i<<"] ["<<j<<"]"<<endl;
flag=true;
}
}
}
if(!flag)
{
cout<<"Value Doesn't Found"<<endl;
}
break;
}
/***** CASE OF SWAPING *****/
case 'W':
case 'w':
{
char swap;
int temp;
do
{
cout<<"Enter R to Swap Row or C to Swap Column :";
cin>>swap;
if(swap!='r' && swap!='R' && swap!='c' && swap!='C')
cout<<"Invalid Option"<<endl;
}
while(swap!='r' && swap!='R' && swap!='c' && swap!='C');
switch(swap)
{
/***** CASE TO SWAP A ROW *****/
case 'R':
case 'r':
{
int index1,index2;
do
{
cout<<"Enter Index Of 1st Row To Swap Between 0-"<<rows-1<<": ";
cin>>index1;
if(index1<0 || index1>rows-1)
cout<<endl<<"Enter Valid Input"<<endl;
}
while(index1<0 || index1>rows-1);
do
{
cout<<"Enter Index Of 2nd Row To Swap Between 0-"<<rows-1<<": ";
cin>>index2;
if(index2<0 || index2>rows-1)
cout<<endl<<"Enter Valid Input"<<endl;
}
while(index2<0 || index2>rows-1);
for(i=index1;i==index1;i++)
{
for(j=0;j<cols;j++)
{
temp=matrix[i][j];
matrix[i][j]=matrix[index2][j];
matrix[index2][j]=temp;
}
}
cout<<endl<<"Matrix After Swapping Rows"<<endl;
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
cout<<matrix[i][j]<<"\t";
}
cout<<endl;
}
break;
}
/***** CASE TO SWAP A COLUMN *****/
case 'C':
case 'c':
{
int index1,index2;
do
{
cout<<"Enter Index Of 1st Col To Swap Between 0-"<<cols-1<<": ";
cin>>index1;
if(index1<0 || index1>cols-1)
cout<<endl<<"Enter Valid Input"<<endl;
}
while(index1<0 || index1>cols-1);
do
{
cout<<"Enter Index Of 2nd Col To Swap Between 0-"<<cols-1<<": ";
cin>>index2;
if(index2<0 || index2>cols-1)
cout<<endl<<"Enter Valid Input"<<endl;
}
while(index2<0 || index2>cols-1);
for(j=index1;j==index1;j++)
{
for(i=0;i<cols;i++)
{
temp=matrix[i][j];
matrix[i][j]=matrix[i][index2];
matrix[i][index2]=temp;
}
}
cout<<endl<<"Matrix After Swapping Columns "<<endl;
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
cout<<matrix[i][j]<<"\t";
}
cout<<endl;
}
break;
}
}
break;
}
/***** CASE OF PRINT *****/
case 'P':
case 'p':
{
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
cout<<matrix[i][j]<<"\t";
}
cout<<endl;
}
break;
}
/***** CASE OF QUIT *****/
case 'Q':
case 'q':
{
cout<<"Quiting Program"<<endl;
quit=false;
break;
}
}
}
/************ Matrix Operational Loop Ends ***********/
return 0;
}
Easy Programming Code ,free download software with keys,c++ toturial,c++ codes and much more....
Thursday, August 30, 2012
Matrix Operation in C++
Sunday, August 26, 2012
Classic calculator code in c++
#include<iostream.h>
void main()
{
int a,b,c=0,i,r=0;
char ch;
cout<<"enter the first number\n";
cin>>a;
cout<<"enter the second number\n";
cin>>b;
cout<<"Enter (+) For Sum \n\n Enter(-) For Subtracion \n\nEnter(*) For Multiplaction\n\n Enter(/) For Division \n\n Enter(%) For Mod\n\nEnter (i) For Inverse of First Number";
cin>>ch;
// if(ch=='+' || ch=='-' || ch=='*' || ch=='/' || ch=='%' || ch=='i')
{
switch (ch){
case '+':
for (i=1 ; i<=b ;i++)
{
a+=1;
}
cout<<a<<endl;
break;
case '-':
for (i=1;i<=b;i++)
{
a-=1;
}
cout<<a<<endl;
break;
case '*':
for(i=1;i<=b;i++)
{
r+=a;
}
cout<<r<<endl;
break;
case '/':
while(a>=b)
{
for(i=1;i<=b;i++)
{
a=--a;
}
c++;
}
cout<<c<<endl;
case '%':
while(a>=b)
{
for(i=1;i<=b;i++)
{
a=--a;
}
}
cout<<a<<endl;
case 'i':
while(a)
{
--a;
--c;
}
cout<<c<<endl;
default:
cout<<"plese enter the correct character[+,-,*,/,%,i]"<<endl;
}
}
}
void main()
{
int a,b,c=0,i,r=0;
char ch;
cout<<"enter the first number\n";
cin>>a;
cout<<"enter the second number\n";
cin>>b;
cout<<"Enter (+) For Sum \n\n Enter(-) For Subtracion \n\nEnter(*) For Multiplaction\n\n Enter(/) For Division \n\n Enter(%) For Mod\n\nEnter (i) For Inverse of First Number";
cin>>ch;
// if(ch=='+' || ch=='-' || ch=='*' || ch=='/' || ch=='%' || ch=='i')
{
switch (ch){
case '+':
for (i=1 ; i<=b ;i++)
{
a+=1;
}
cout<<a<<endl;
break;
case '-':
for (i=1;i<=b;i++)
{
a-=1;
}
cout<<a<<endl;
break;
case '*':
for(i=1;i<=b;i++)
{
r+=a;
}
cout<<r<<endl;
break;
case '/':
while(a>=b)
{
for(i=1;i<=b;i++)
{
a=--a;
}
c++;
}
cout<<c<<endl;
case '%':
while(a>=b)
{
for(i=1;i<=b;i++)
{
a=--a;
}
}
cout<<a<<endl;
case 'i':
while(a)
{
--a;
--c;
}
cout<<c<<endl;
default:
cout<<"plese enter the correct character[+,-,*,/,%,i]"<<endl;
}
}
}
leep year code in javascript
<html>
<body>
<head>
<script type="text/javascript">
function leap(year)
{
if(year % 4==0)
document.write(year+" is a leap year");
else
document.write(year+" is not a leap year");
}
no=prompt("Enter an year to find wheather it is prime of not","2000");
leap(no);
</script>
</head>
</body>
</html>
Comparing weight with short code in javascript
<html>
<body>
<head>
<script type="text/javascript">
function odd(st,end)
{
while(st<=end)
{
if(st % 2 == 1)
document.write("The number "+st+ " is a odd number<br/>");
st=st+1;
}
}
a=parseInt(prompt("Enter starting number","2"));
b=parseInt(prompt("Enter ending number","12"));
odd(a,b);
</script>
</head>
</body>
</html>
comparing four person weight in javascript
<html>
<body>
<script type="text/javascript">
p1weight=prompt("Enter weight of person 1");
p2weight=prompt("Enter weight of person 2");
p3weight=prompt("Enter weight of person 3");
p4weight=prompt("Enter weight of person 4");
if (p1weight >p2weight && p1weight > p3weight && p1weight > p4weight)
document.write("weight of Person 1 is greatest<br/>")
else if(p1weight >p2weight && p1weight >p3weight ||p1weight >p3weight && p1weight >p4weight||p1weight >p2weight && p1weight >p4weight )
document.write("weight of Person 1 is at second mid<br/>")
else if(p1weight >p2weight || p1weight >p3weight ||p1weight >p4weight )
document.write("weight of Person 1 is at first mid<br/>")
else
document.write("weight of Person 1 is least<br/>")
if (p2weight >p1weight && p2weight > p3weight && p2weight > p4weight)
document.write("weight of Person 2 is greatest<br/>")
else if(p2weight >p1weight && p2weight >p3weight ||p2weight >p3weight && p2weight >p4weight||p2weight >p1weight && p2weight >p4weight )
document.write("weight of Person 2 is at second mid<br/>")
else if(p2weight >p1weight || p2weight >p3weight ||p2weight >p4weight )
document.write("weight of Person 2 is at first mid<br/>")
else
document.write("weight of Person 2 is least<br/>")
if (p3weight >p1weight && p3weight > p2weight && p3weight > p4weight)
document.write("weight of Person 3 is greatest<br/>")
else if(p3weight >p1weight && p3weight >p2weight ||p3weight >p2weight && p3weight >p4weight||p3weight >p1weight && p3weight >p4weight )
document.write("weight of Person 3 is at second mid<br/>")
else if(p3weight >p1weight || p3weight >p2weight ||p3weight >p4weight )
document.write("weight of Person 3 is at first mid<br/>")
else
document.write("weight of Person 3 is least<br/>")
if (p4weight >p1weight && p4weight > p2weight && p4weight > p3weight)
document.write("weight of Person 4 is greatest<br/>")
else if(p4weight >p1weight && p4weight >p2weight ||p4weight >p2weight && p4weight >p3weight||p4weight >p1weight && p4weight >p3weight )
document.write("weight of Person 4 is at second mid<br/>")
else if(p4weight >p1weight || p4weight >p2weight ||p4weight >p3weight )
document.write("weight of Person 4 is at first mid<br/>")
else
document.write("weight of Person 4 is least<br/>")
</script>
</body>
</html>
Tuesday, August 21, 2012
Guess game code in c++
#include<iostream.h>
#include<time.h>
#include <stdlib.h>
#include<conio.h>
void main()
{
srand (time(NULL));
int guss,ans,count=1;
ans=rand() % 10+1;
cout<<"Guss the number in the range of 1 to 10\n";
do
{
cin>>guss;
if (guss>ans){
cout<<"Too big number try smaller number\n";
count++;
}
else if(guss<ans){
cout<<"Too smaller number try biger number\n";
count++;
}
else if (guss==ans){
cout<<"congragulation you guss right number in "<<count<<" attempt\n";
}
}
while(guss!=ans && count !=4);
getch();
}
#include<time.h>
#include <stdlib.h>
#include<conio.h>
void main()
{
srand (time(NULL));
int guss,ans,count=1;
ans=rand() % 10+1;
cout<<"Guss the number in the range of 1 to 10\n";
do
{
cin>>guss;
if (guss>ans){
cout<<"Too big number try smaller number\n";
count++;
}
else if(guss<ans){
cout<<"Too smaller number try biger number\n";
count++;
}
else if (guss==ans){
cout<<"congragulation you guss right number in "<<count<<" attempt\n";
}
}
while(guss!=ans && count !=4);
getch();
}
Sunday, August 12, 2012
simple file handeling code in PHP
<?php
$h=fopen("text1.txt",'w');
fwrite($h,"first line of the text1 file (1)\n second line of text1 file (2)\n 3rd line of text1 file.(3)\n");
fclose($h);
echo "your file 1 is created and write successfully"."<br>";
?>
<?php
$h1=fopen("text2.txt",'w');
fwrite($h1,"4th line which is 1st line of text2.(4)\n5th line which is 2nd of text2 (5)\n 6th which is 3rd of text2.(6)\n");
fclose($h1);
echo "your file 2 is created and write successfully"."<br>";
?>
<?php
$h2=fopen("text3.txt",'a');
$h=fopen("text1.txt",'r');
$r=fread($h,filesize("text1.
txt"));$h=fopen("text1.txt",'w');
fwrite($h,"first line of the text1 file (1)\n second line of text1 file (2)\n 3rd line of text1 file.(3)\n");
fclose($h);
echo "your file 1 is created and write successfully"."<br>";
?>
<?php
$h1=fopen("text2.txt",'w');
fwrite($h1,"4th line which is 1st line of text2.(4)\n5th line which is 2nd of text2 (5)\n 6th which is 3rd of text2.(6)\n");
fclose($h1);
echo "your file 2 is created and write successfully"."<br>";
?>
<?php
$h2=fopen("text3.txt",'a');
$h=fopen("text1.txt",'r');
$r=fread($h,filesize("text1.
fwrite($h2,$r);
fclose($h2);
echo "your text 1 file mearge on text 3 file successfully"."<br>";
?>
<?php
$h2=fopen("text3.txt",'a');
$h1=fopen("text2.txt",'r');
$r1=fread($h1,filesize("text2.
txt"));
fwrite($h2,$r1);
fclose($h2);
echo "your text 2 file mearge on text 3 file successfully"."<br>";
?>
<?php
$h3=fopen("text4.txt",'w');
$h2=fopen("text3.txt",'r');
$count=0;
while(!feof($h2))
{
if($count%2==0){
$r2=fgets($h2);
}
else
{
$r2=fgets($h2);
fwrite($h3,$r2);
}
$count++;
}
?>
Numbers table code in c++
#include<iostream.h>
void main()
{
int num,lim,n,p;
cout<<"Enter number ypu want to calculate table\n";
cin>>num;
cout<<"Enter tlhe limit where you want to calculate the table\n";
cin>>lim;
for(n=1;n<=lim;n++)
{
p=num*n;
cout<<num<<" X "<<n<<" = "<<p<<endl;
}
}
void main()
{
int num,lim,n,p;
cout<<"Enter number ypu want to calculate table\n";
cin>>num;
cout<<"Enter tlhe limit where you want to calculate the table\n";
cin>>lim;
for(n=1;n<=lim;n++)
{
p=num*n;
cout<<num<<" X "<<n<<" = "<<p<<endl;
}
}
Display ASCII value of charactor in C++
#include<iostream.h>
void main()
{
char value;
cout<<"Enter the charactor\n";
cin>>value;
int num=value;
cout<<num;
}
void main()
{
char value;
cout<<"Enter the charactor\n";
cin>>value;
int num=value;
cout<<num;
}
palindrome program in c++ code
#include<iostream.h>
void main()
{
int num,rev=0,rem,dev;
cout<<"Enter a number you want to cheack it is palndrom\n";
cin>>num;
dev=num;
while(num!=0)
{
rem=num%10;
num=num/10;
rev=rev*10+rem;
}
cout<<"The reverse of number is"<<rev<<endl;
if(dev==rev)
cout<<"The number is plandrom\n";
else
cout<<"The number is not a plandrom number\n";
}
void main()
{
int num,rev=0,rem,dev;
cout<<"Enter a number you want to cheack it is palndrom\n";
cin>>num;
dev=num;
while(num!=0)
{
rem=num%10;
num=num/10;
rev=rev*10+rem;
}
cout<<"The reverse of number is"<<rev<<endl;
if(dev==rev)
cout<<"The number is plandrom\n";
else
cout<<"The number is not a plandrom number\n";
}
tic tac toe game code in c++
#include<iostream.h>
void main()
{
char arr[3][3];
int i,j,r1,c1,r2,c2,count=0;
//forming game structre
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
arr[i][j]='V';
cout<<arr[i][j]<<"\t";
}
cout<<endl<<endl;
}
while(count<8)
{
cout<<"enter player 1 num\n";
cin>>r1>>c1;
//input validatioin for player 1
while(r1>2 || c1>2 || r1<0 || c1<0 || arr[r1][c1]=='X' || arr[r1][c1]=='O')
{
cout<<"Enter the value in the range of 0 to 2\n\tOR\ntry another value";
cin>>r1>>c1;
}
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
if(i==r1 && j==c1)
{
arr[i][j]='X';
cout<<arr[i][j]<<"\t";
}
else
{
cout<<arr[i][j]<<"\t";
}
}
cout<<endl<<endl;
}
cout<<"enter player 2 num\n";
cin>>r2>>c2;
//input validation for player 2
while(r2>2 || c2>2 || r2<0 || c2<0 || arr[r2][c2]=='X' || arr[r2][c2]=='O')
{
cout<<"Enter the value in the range of 0 to 2\n\tOR\ntry another value";
cin>>r2>>c2;
}
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
if(i==r2 && j==c2)
{
arr[i][j]='O';
cout<<arr[i][j]<<"\t";
}
else
{
cout<<arr[i][j]<<"\t";
}
}
cout<<endl<<endl;
}
if(arr[0][0]=='X' && arr[0][1]=='X' && arr[0][2]=='X'){
cout<<"player 1 wins\n";
break;
}
else if(arr[0][0]=='X' && arr[1][0]=='X' && arr[2][0]=='X'){
cout<<"player 1 wins\n";
break;
}
else if(arr[0][0]=='X' && arr[1][1]=='X' && arr[2][2]=='X'){
cout<<"player 1 wins\n";
break;
}
else if(arr[0][2]=='X' && arr[1][1]=='X' && arr[2][0]=='X'){
cout<<"player 1 wins\n";
break;
}
else if(arr[0][1]=='X' && arr[1][1]=='X' && arr[2][1]=='X'){
cout<<"player 1 wins\n";
break;
}
else if(arr[0][2]=='X' && arr[1][2]=='X' && arr[2][2]=='X'){
cout<<"player 1 wins\n";
break;
}
else if(arr[1][0]=='X' && arr[1][1]=='X' && arr[1][2]=='X'){
cout<<"player 1 wins\n";
break;
}
else if(arr[0][0]=='O' && arr[1][1]=='O' && arr[2][2]=='O'){
cout<<"player 1 wins\n";
break;
}
else if(arr[0][2]=='O' && arr[1][1]=='O' && arr[2][0]=='O'){
cout<<"player 1 wins\n";
break;
}
else if(arr[2][0]=='X' && arr[2][1]=='X' && arr[2][2]=='X'){
cout<<"player 1 wins\n";
break;
}
else if(arr[0][0]=='O' && arr[0][1]=='O' && arr[0][2]=='O'){
cout<<"player 2 wins\n";
break;
}
else if(arr[0][0]=='O' && arr[1][0]=='O' && arr[2][0]=='O'){
cout<<"player 2 wins\n";
break;
}
else if(arr[0][1]=='O' && arr[1][1]=='O' && arr[2][1]=='O'){
cout<<"player 2 wins\n";
break;
}
else if(arr[0][2]=='O' && arr[1][2]=='O' && arr[2][2]=='O'){
cout<<"player 2 wins\n";
break;
}
else if(arr[1][0]=='O' && arr[1][1]=='O' && arr[1][2]=='O'){
cout<<"player 2 wins\n";
break;
}
else if(arr[2][0]=='O' && arr[2][1]=='O' && arr[2][2]=='O'){
cout<<"player 2 wins\n";
break;
}
else if(arr[0][0]=='O' && arr[1][1]=='O' && arr[2][2]=='O'){
cout<<"player 2 wins\n";
break;
}
else if(arr[0][2]=='O' && arr[1][1]=='O' && arr[2][0]=='O'){
cout<<"player 2 wins\n";
break;
}
else if(arr[0][0]=='O' && arr[1][1]=='O' && arr[2][2]=='O'){
cout<<"player 2 wins\n";
break;
}
else if(arr[0][2]=='O' && arr[1][1]=='O' && arr[2][0]=='O'){
cout<<"player 2 wins\n";
break;
}
count++;
if(count==8){
cout<<"no one wine tie\n";
}
count++;
}
}
void main()
{
char arr[3][3];
int i,j,r1,c1,r2,c2,count=0;
//forming game structre
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
arr[i][j]='V';
cout<<arr[i][j]<<"\t";
}
cout<<endl<<endl;
}
while(count<8)
{
cout<<"enter player 1 num\n";
cin>>r1>>c1;
//input validatioin for player 1
while(r1>2 || c1>2 || r1<0 || c1<0 || arr[r1][c1]=='X' || arr[r1][c1]=='O')
{
cout<<"Enter the value in the range of 0 to 2\n\tOR\ntry another value";
cin>>r1>>c1;
}
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
if(i==r1 && j==c1)
{
arr[i][j]='X';
cout<<arr[i][j]<<"\t";
}
else
{
cout<<arr[i][j]<<"\t";
}
}
cout<<endl<<endl;
}
cout<<"enter player 2 num\n";
cin>>r2>>c2;
//input validation for player 2
while(r2>2 || c2>2 || r2<0 || c2<0 || arr[r2][c2]=='X' || arr[r2][c2]=='O')
{
cout<<"Enter the value in the range of 0 to 2\n\tOR\ntry another value";
cin>>r2>>c2;
}
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
if(i==r2 && j==c2)
{
arr[i][j]='O';
cout<<arr[i][j]<<"\t";
}
else
{
cout<<arr[i][j]<<"\t";
}
}
cout<<endl<<endl;
}
if(arr[0][0]=='X' && arr[0][1]=='X' && arr[0][2]=='X'){
cout<<"player 1 wins\n";
break;
}
else if(arr[0][0]=='X' && arr[1][0]=='X' && arr[2][0]=='X'){
cout<<"player 1 wins\n";
break;
}
else if(arr[0][0]=='X' && arr[1][1]=='X' && arr[2][2]=='X'){
cout<<"player 1 wins\n";
break;
}
else if(arr[0][2]=='X' && arr[1][1]=='X' && arr[2][0]=='X'){
cout<<"player 1 wins\n";
break;
}
else if(arr[0][1]=='X' && arr[1][1]=='X' && arr[2][1]=='X'){
cout<<"player 1 wins\n";
break;
}
else if(arr[0][2]=='X' && arr[1][2]=='X' && arr[2][2]=='X'){
cout<<"player 1 wins\n";
break;
}
else if(arr[1][0]=='X' && arr[1][1]=='X' && arr[1][2]=='X'){
cout<<"player 1 wins\n";
break;
}
else if(arr[0][0]=='O' && arr[1][1]=='O' && arr[2][2]=='O'){
cout<<"player 1 wins\n";
break;
}
else if(arr[0][2]=='O' && arr[1][1]=='O' && arr[2][0]=='O'){
cout<<"player 1 wins\n";
break;
}
else if(arr[2][0]=='X' && arr[2][1]=='X' && arr[2][2]=='X'){
cout<<"player 1 wins\n";
break;
}
else if(arr[0][0]=='O' && arr[0][1]=='O' && arr[0][2]=='O'){
cout<<"player 2 wins\n";
break;
}
else if(arr[0][0]=='O' && arr[1][0]=='O' && arr[2][0]=='O'){
cout<<"player 2 wins\n";
break;
}
else if(arr[0][1]=='O' && arr[1][1]=='O' && arr[2][1]=='O'){
cout<<"player 2 wins\n";
break;
}
else if(arr[0][2]=='O' && arr[1][2]=='O' && arr[2][2]=='O'){
cout<<"player 2 wins\n";
break;
}
else if(arr[1][0]=='O' && arr[1][1]=='O' && arr[1][2]=='O'){
cout<<"player 2 wins\n";
break;
}
else if(arr[2][0]=='O' && arr[2][1]=='O' && arr[2][2]=='O'){
cout<<"player 2 wins\n";
break;
}
else if(arr[0][0]=='O' && arr[1][1]=='O' && arr[2][2]=='O'){
cout<<"player 2 wins\n";
break;
}
else if(arr[0][2]=='O' && arr[1][1]=='O' && arr[2][0]=='O'){
cout<<"player 2 wins\n";
break;
}
else if(arr[0][0]=='O' && arr[1][1]=='O' && arr[2][2]=='O'){
cout<<"player 2 wins\n";
break;
}
else if(arr[0][2]=='O' && arr[1][1]=='O' && arr[2][0]=='O'){
cout<<"player 2 wins\n";
break;
}
count++;
if(count==8){
cout<<"no one wine tie\n";
}
count++;
}
}
Subscribe to:
Comments (Atom)