Tuesday 17 March 2009

Binary Searh+sort+Seq sort

#include
#define size 10

void sort ( int[]);
void seqsearch (int[], int);
void binarysearch (int [], int);

void main ()
{
int list[] = {10,19,18,17,16,15,14,13,12,11};
int target;
int i;

printf("\nEnter the value to search : ");
scanf("%d",&target);

seqsearch(list,target);
sort (list);
binarysearch (list,target);

printf("\n\t\tSORTED LIST\n");
for (i=0; i printf("\nList[%d]= %d\n",i,list[i]);

}

void seqsearch(int list[], int t)
{
int i=0;

printf("\n\t\tSequential Search Technique\n");

while(i ++i;

if( i printf("\nTarget was found in location %d\n",i);
else
printf("\nTarget not in the list\n");

}

void sort(int list[])
{
int i,j,temp;

for(i=0; i for( j=i+1; j if(list[i]>list[j])
{
temp=list[i];
list[i]=list[j];
list[j]=temp;
}
}

void binarysearch(int list[],int t)
{
printf("\n\t\tBinary Search Technique\n");

int mid,first=0, last=size-1;

while(first {
mid=(first+last)/2;

if(t>list[mid])
first=mid+1;
else if(t last=mid-1;
else
break;
}

if(t==list[mid])

printf("\nTarget was found in location %d \n",mid);
else
printf("\nTarget not in the list\n");
}

Combo Seq Sort

#include
#define size 10

void sort ( int[]);
void seqsearch (int[], int);
void binarysearch (int [], int);

void main ()
{
int list[] = {10,19,18,17,15,14,13,12,11};
int target;
int i;

printf("\nEnter the value to search : ");
scanf("%d",&target);

seqsearch(list,target);

sort (list);

printf("\n\t\tSORTED LIST");
for (i=0; i printf("\nList[%d]= %d\n",i,list[i]);

}

void seqsearch(int list[], int t)
{
int i=0;

while(i ++i;

if( i printf("\nTarget was found in location %d\n",i);
else
printf("\nTarget not in the list\n");

}

void sort(int list[])
{
int i,j,temp;

for(i=0; i for( j=i+1; j if(list[i]>list[j])
{
temp=list[i];
list[i]=list[j];
list[j]=temp;
}
}

Seq Search

#include
#define size 10

void sort ( int[]);
void seqsearch (int[], int);
void binarysearch (int [], int);

void main ()
{
int list[] = {10,19,18,17,15,14,13,12,11};
int target;

printf("\nEnter the value to search : ");
scanf("%d",&target);

seqsearch(list,target);

}

void seqsearch(int list[], int t)
{
int i=0;

while(i ++i;

if( i printf("\nTarget was found in location %d\n",i);
else
printf("\nTarget not in the list\n");

}

Calculate Cost X

import java.util.Scanner;
class calculateCost {
public static void main (String args[]) {
Scanner s = new Scanner(System.in);


int weight=0;
String method=null;
double cost=0.0;

System.out.println("Choose the selected shipping method");
System.out.println("A=Air");
System.out.println("T=Truck");
System.out.println("M=Mail");
String choice=s.next();

if(choice.equals("A"))
{
method="Air";
System.out.println("Enter weight :");
weight=s.nextInt();

if (weight <=8)
cost=2.00;
else if (weight<=16)
cost=3.00;
else
cost=4.50;
}
else if(choice.equals("T"))
{
method="Truck";
System.out.println("Enter weight :");
weight=s.nextInt();

if (weight <=8)
cost=1.50;
else if (weight<=16)
cost=2.35;
else
cost=3.25;
}

else if(choice.equals("M"))
{
method="Mail";
System.out.println("Enter weight :");
weight=s.nextInt();

if (weight <=8)
cost=0.50;
else if (weight<=16)
cost=1.50;
else
cost=2.15;
}
else
System.out.println("Thank You");

System.out.println("Method\tWeight\tCost");
System.out.println("=====================");
Package p = new Package(weight,method,cost);
System.out.println( p.display());




}
}

String Compare strcmp

//Program to key in the password 3 times

#include
#include
#define mypassword "abc123"

void main()
{

char password [20] ;
int i ;

for( i=0; i <3; ++i)
{

printf("\nEnter security password : ");
gets(password);




if (strcmp(password,mypassword)==0)
{
printf("\nCorrect\n");
break;
}
}
if (i==3)
printf("\nYou have key in the password 3 time\n");
}

Monday 16 March 2009

C+ Super Array !!!

#include
#include
#define myfruit "grapes"

void main()
{
char name [20];
char string [20] = "Hello";
char string2 [20] = "How are you";
char dummy[20];
char fruit [20];

printf("\nEnter name : ");
scanf ("%s",&name);

gets(dummy); // to read the enter key and store in the string dummy

printf("\nYour name is %s",name);

printf("\nEnter your name again : ");
gets(name);

printf("\nYour nane is %s\n",name);
strcat(string,name);

printf("\nNow in string is %s",string);

printf("\nThe number of characters in name is %d",strlen(name));

strcpy(string,string2);
printf("\nNow in string is %s",string);

printf("\nThe value return is %d",strcmp("Apple","Papaya"));

printf("\nThe value return is %d",strcmp("Papaya","Apple"));

printf("\nThe value return is %d",strcmp("Apple","Apple"));

printf("\nGuess my favourite fruit ?");

gets(fruit);

if (strcmp(fruit,myfruit)==0)
printf("\nWell done! You guess it right!\n");
else
printf("\nSorry! %s is not my favourite fruit\n",fruit);
}

What is life ?