The Computer Geeks Thread

This thread is for those poor, lost souls who were stupid enough to take Computer Science for classes XI and XII, without knowing that it's not necessary as the syllabus revvs up to a whole new level in college.

You do get the basics of stuff like C++ and SQL though, and
knowledge gained is helpful somewhere or the other in life.

*Note : I myself am an exponent of this elite club of idiots.*

The main purpose of this thread is to help your fellow lost souls by posting programs, tidbits of information, questions and their solutions from different books, etc. You can brainstorm on this thread as well, and request help for your doubts. To inaugurate the proceedings, I'll post some of my programs from arrays and strings.

11 Answers

39
Pritish Chakraborty ·

-=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 ·

-= 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 ·

-= 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 ·

-= 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 ·

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 ·

My edition for last program:

http://pastebin.ca/1851256

PS: this text editor was changing few symbols that's why uploaded

39
Pritish Chakraborty ·

-= 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 ·

-= 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 ·

-= 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 ·

-= 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 ·

lol!

computer programming is 95% logic and 5% syntax knowledge!

Your Answer

Close [X]