#include <iostream>
using namespace std;
void sortedarr(int *,int);
int bst(int,int, int, int *);
int main(){
int size;
int a[100];
int data;
cout<<"Enter the size of the array : ";
cin>>size;
cout<<"Enter array elements ";
for(int i = 0; i<size; i++){
cin>>a[i];
}
sortedarr(a,size);
cout<<"Enter the Data : ";
cin>>data;
int b;
b = bst(data,0,size-1,a);
if(b == -1){
cout<<"Not found"<<endl;
}
else{
cout<<"Fount at index no : "<<b+1<<" place"<<endl;
}
}
void sortedarr(int a[],int m){
int k = 1;
int temp,i;
while(k<m){
for(i=0; i<k && k<m; i++ ){
if(i<m){
if(a[i]>a[k]){
temp = a[i];
a[i] = a[k];
a[k] = temp;
}
}
}
k=k+1;
i=0;
}
}
int bst(int data,int lb,int ub,int a[]){
int mid;
if((ub+lb)%2 ==0){
mid=(ub+lb)/2;
}
else{
mid = (ub+lb+1)/2;
}
while(lb<=ub){
if(a[mid]==data){
return mid;
}
else if(a[mid]<data){
lb = mid+1;
return bst(data,lb,ub,a);
}
else if(a[mid]>data){
ub = mid-1;
return bst(data,lb,ub,a);
}
}
return -1;
}
Showing posts with label Code. Show all posts
Showing posts with label Code. Show all posts
Tuesday, April 17, 2012
Monday, April 16, 2012
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int p;
int s;
int z;
int x = (p%100)*(s%100);
if (x>8000){
z = x/100;
}
else if(x>5000 && x<=8000){
z = x/90;
}
else if(x>1000 && x<=5000)
{
z = x/75;
}
else if(x>100 && x<=1000){
z = x/35;
}
else {
z = x/10;
}
int opt;
int n;
cout<<"My Program generates a random number."<<endl;
cout<<" Can you guess what it is?"<<endl;
cout<<"You may take some help but remember these are limited.";
cout<<endl;
cout<<endl;
int count=0,count1=0,count2=0,count3 = 0;
while(1){
cout<<"1.Check the range in which the number can be"<<endl;
cout<<"2.Check if the number is odd or even"<<endl;
cout<<"3.check if the number is prime or not"<<endl;
cout<<"4.Guess Your number (Only two time)"<<endl;
cout<<"5.The number is ?"<<endl;
cout<<"6.exit"<<endl;
cout<<"Choose your option : "<<endl;
cin>>opt;
switch(opt){
case 1 :
{
if(count==0){
if (z<=50){
if(z <= 25){
if(z<=10){
cout<<"In range of 0 to 10"<<endl;
}
else{
cout<<"In range of 11 to 25"<<endl;
}
}
else if (z <=40){
cout<<"In range of 26 to 40";
}
else {
cout<<"in range of 41 to 50";
}
}
else if(z<=60){
cout <<"In range of 51 to 60";
}
else if (z <= 70){
cout <<"In range of 61 to 70";}
else if (z <= 80){
cout <<"In range of 71 to 80";}
else if (z <= 90){
cout <<"In range of 81 to 90";}
else{
cout <<"In range of 91 to 99";
}
count = count+1;
cout<<endl;
}
else {
cout<<"You already had checked the range";
cout<<endl;
}
}
break;
case 2:{
if(count1 == 0){
if(z % 2 == 0){cout<<"Number is even"<<endl;}
else{cout<<"Number is odd"<<endl;}
count1 = count1+1;}
else{
cout<<"You already checked it"<<endl;
}}
break;
case 3:{
int f = (int)sqrt((double)z);
int flag=1;
for(int i=3; i<=f; i++ ){
if(z == 1 || z==0 ){
cout <<"Neither prime nor not prime";
}
else if(z == 2){
cout<<"Prime number";
}
if(z%i==0){
cout<<"Not prime";
flag = 0;
break;
}
}
if(flag == 1){cout<<"Prime number";}
cout<<endl;
}
break;
case 4:{
if(count2 <= 2){
cout<<"Enter Number : "<<endl;
cin>>n;
if(n==z){
cout<<"You Won!!!!! Game exits!"<<endl;
count2 = count2 + 1;
return 0;
}
else{
count2 = count2 + 1;
cout<<"Incorrect guess. "<<"You have "<<(2-count2)<<" option left";}
if(count2 == 2){
cout<<"Game Over !!";
return 0;
}
cout<<endl;
}
}
break;
case 5: {
cout<<endl<<"The number is "<<z<<" !"<<endl;
break;
}
case 6:{
cout<<"Thank you for playing my game :) "<<endl;
break;
return 0;
}
default :{
cout<<"Please maintain the game rules and choose right option"<<endl;
cout<<endl;
break;
}}
}
Subscribe to:
Posts (Atom)