39
Pritish Chakraborty
·2010-03-24 00:54:33
-=BINARY SEARCH=-
Little Intro : This type of searching method is very popular as it minimises the number of comparisons in an array. It works only on ascending-sorted arrays. The array is divided into two segments by a middle element. If the element to be searched is greater than the middle element, the segment before the middle element is eliminated. Similarly, if lesser, the segment after the middle element is eliminated. This process continues till only one element is left, which is the required element.
int Bsearch(int AR[], int size, int item)
{
int beg, mid, last;
beg = 0;
last = size-1;
while(beg <= last)
{
mid = (beg + last)/2;
if(item == AR[mid])
return mid;
if(item > AR[mid])
beg = mid+1;
if(item < AR[mid])
last = mid-1;
}
return -1;
}
void main()
{
int N, AR[ 50 ], item, i, index;
cin >> N;
for(i = 0; i < N; i++)
{
cin >> AR[ i ];
}
cin >> item;
index = Bsearch(AR, N, item);
if(index == -1)
cout << "\nSearch unsuccessful";
else
cout << "\nFound at : " << index + 1;
}
39
Pritish Chakraborty
·2010-03-24 00:56:06
-= DELETION FROM ARRAY(SPECIFIC POSITION) =-
void main()
{
int i, loc, AR[ 50 ], prev, num, N;
cout << "\nWhat size bishes? : ";
cin >> N;
for(i = 0; i < N; i++)
cin >> AR[ i ];
cout << "\nWhich location to eliminate? : ";
cin >> loc;
loc--;
if(loc < N)
{
num = AR[loc];
prev = loc;
}
while(prev < N)
{
AR[prev] = AR[prev+1];
prev = prev+1;
}
N--;
for(i = 0; i < N; i++)
cout << AR[ i ] << "\t";
}
39
Pritish Chakraborty
·2010-03-24 00:59:46
-= EXCHANGE SELECTION SORT(ASCENDING ORDER) =-
Little Intro : This type of sorting method(ascending order) checks the entire array for the smallest element and swaps it with the first element in each pass. Note that number of elements in the array = Number of passes undertaken to sort the array.
void Selsort(int AR[], int size)
{
int small, pos , tmp, i, j;
for(i = 0; i < size; i++)
{
small = AR[ i ];
for(j = i+1; j < size; j++)
{
if(AR[ j ] < small)
{
small = AR[ j ];
pos = j;
}
}
tmp = AR[ i ];
AR[ i ] = AR[pos];
AR[pos] = tmp;
cout << "\nArray after pass - " << i+1 << " - is : ";
for(j = 0; j < size; j++)
cout << AR[ j ] << " ";
}
}
void main()
{
int AR[ 50 ], ITEM, N, index;
cout << "Size? : ";
cin >> N;
cout << "\nEnter array elements : ";
for(int i = 0; i < N; i++)
cin >> AR[ i ];
Selsort(AR, N);
cout << "\nFinally, sorted array : ";
for(i = 0; i < N; i++)
cout << AR[ i ] << " ";
cout << endl;
}
39
Pritish Chakraborty
·2010-03-24 01:11:17
-= STRING COMPARISON =-
Little Intro : And you thought I was going to use string.h, didn't you?
void main()
{
char ch1[BUFSIZ], ch2[BUFSIZ];
int diff;
cout << "\nEnter string 1 : ";
cin.getline(ch1, BUFSIZ);
cout << "\nEnter string 2 : ";
cin.getline(ch2, BUFSIZ);
if(strlen(ch1) == strlen(ch2))
{
for(int i = 0; ch1[ i ] != '\0' && ch2[ i ] != '\0'; i++)
{
diff = ch1[ i ] - ch2[ i ];
if(!diff)
continue;
else
break;
} //for
}// if
if(diff != 0)
cout << "\nNot the same.";
else
cout << "\nStrings are the same.";
}
Edited as missing condition was pointed out by Ankur.
1
Ankur
·2010-03-24 01:43:21
Oopsy Daisy! I only glimpsed last program and guess what?
Try entering string 1 as : Pritish and string 2 as : PritishC
Check the output
leave the rest.
1
Ankur
·2010-03-24 02:00:10
My edition for last program:
http://pastebin.ca/1851256
PS: this text editor was changing few symbols that's why uploaded
39
Pritish Chakraborty
·2010-03-25 11:17:17
-= STRING LENGTH =-
int main()
{
char ch[ 20 ];
int temp;
cout << "Enter the string - ";
gets(ch);
fflush(stdin);
cout << endl;
for(int i = 0; ch[ i ] != '\0'; i++)
{
temp = i;
}
cout << "The length is - " << temp+1;
getch();
return 0;
}
39
Pritish Chakraborty
·2010-03-25 11:17:54
-= PRIME NUMBER CHECKER =-
int main()
{
int flag = 1;
int num, i;
cout << "Enter a positive integer : ";
cin >> num;
if(num <= 0)
cout << "\nYou did not enter a positive integer";
else if(num > 0)
{
for(i = 2; i <= num/2; i++)
{
if(num % i == 0)
{
flag = 0;
break;
}
}
if(flag == 1)
cout << "\nThe number you entered is prime";
else
cout << "\nThe number is not prime";
}
getch();
}
39
Pritish Chakraborty
·2010-03-25 11:18:24
-= PRIME NUMBER LIMIT + SUM =-
Little Intro : This program finds prime numbers upto a specified limit and also finds their sum. You only need to specify the limit, for eg - if the limit is 7 it will show 2 3 5 7 and their sum as well.
int main()
{
int flag = 1;
int num, i, L, sum = 0;
cout << "\nEnter a positive integer limit : ";
cin >> L;
cout << endl;
if(L <= 0)
cout << "\nThe limit you entered is not positive";
else if(L > 0)
{
for(num = 2; num <= L; num++)
{
flag = 1;
for(i = 2; i <= num/2; i++)
{
if(num % i == 0)
{
flag = 0;
break;
}
}
if(flag == 1)
{
cout << num << "\t";
sum += num;
}
}
cout << "\nThe sum is : " << sum;
}
getch();
}
39
Pritish Chakraborty
·2010-03-25 11:20:37
-= NUMBER OF TERMS OF PRIMES + SUM =-
Little Intro : This program will display the prime number series upto n terms and sum them also.
int main()
{
int flag, i, num, n, ctr = 0, sum = 0;
cout << "Enter number of terms : ";
cin >> n;
if(n <= 0)
cout << "\nInvalid number";
else
{
for(num = 2; ctr < n; num++)
{
flag = 1;
for(i = 2; i <= num/2; i++)
{
if(num % i == 0)
{
flag = 0;
break;
}
}
if(flag == 1)
{
cout << num << "\t";
ctr++;
sum += num;
}
}
cout << "\nThe sum is : " << sum;
}
getch();
1
abcd
·2010-03-25 20:54:14
lol!
computer programming is 95% logic and 5% syntax knowledge!