My Programs

“Talk is cheap. Show me the code.”
Linus Torvalds

Some C++ code:

#include<iostream>
#include<cmath>
#include<fstream>
using namespace std;
double square_root(int a);
void compare_Sqrt();
int main()
{
int x=0;
cout<<“Enter a number: “;
cin>>x;
cout<<“The square root is: “<<square_root(x);
compare_Sqrt();
}
double square_root(int a)
{
double sqr=1;
for(int i=0;i<10;++i)
{
sqr=((sqr+a/sqr)/2.0);
}
return sqr;
}
void compare_Sqrt()
{
int x=0; double diff=0;
ifstream ifile;
ofstream ofile;
ifile.open(“input.txt”);
ofile.open(“output.txt”);
while(!ifile.eof())
{
ifile>>x;
diff=square_root(x)-sqrt(x);
ofile<<x<<” “<<square_root(x)<<” “<<sqrt(x)<<” ”
<<diff<<endl;
}
ifile.close();
ofile.close();
}

Now, Program for a game of 23 toothpics :

#include<iostream>
using namespace std;
void errorcheck(int& a);
int tp=23;
int main()
{
int x=0, ct=0, game=0;
cout<<“This is the game of 23\nEnter 1 to begin\nEnter 0 to leave the game\n”;
cin>>game;
while(game!=0)
{
while(tp>0)
{
cout<<“The total toothpics are: “<<tp<<endl;
cout<<“Chose your move: “;
cin>>x;
errorcheck(x);
tp=tp-x;
if(tp>4)
{
ct=4-x;
cout<<“Computers Move is: “<<ct<<endl;
tp=tp-ct;
}
else if(tp>=2&&tp<=4)
{
ct=tp-1;
cout<<“Computers Move is: “<<ct<<endl;
tp=tp-ct;
}
else if(tp==1)
{
–tp;
cout<<“Congratulations You Win”;
}
else
cout<<“Sorry You lose”;
}
cout<<“\n\nWanna play again?\nEnter 1 to begin\nEnter 0 to leave the game\n”;
cin>>game;
}
}
void errorcheck(int& a)
{
while(a<1||a>=4)
{
cout<<“Must chose toothpics between 1&4″<<endl;
cout<<“Chose your move: “;
cin>>a;
}
if(tp<4)
{
while(a>tp||a<1)
{
cout<<“Error only “<<tp<<” remaining toothpics, choose again: “;
cin>>a;
}
}

 

technology1.jpg

Leave a comment

Design a site like this with WordPress.com
Get started
search previous next tag category expand menu location phone mail time cart zoom edit close