Tuesday, April 6, 2010

For the undergraduates

Not only B.E./B.Tech, but there are several courses out there that teach C/C++ in 1st or 2nd semester. Students who don't have slightest idea of this language at this tender age run helter-skelter to complete their assignments. For them, I'm posting here all the programs that I developed/wrote/coded for assignments when I was in second semester, thats long back in first half of 2007. Copy these, learn these, understand these, throw these, I don't care. I'm upping them here with a hope that they will be of some use to someone in great need indeed.

Disclaimer: These are programs, not software(if you consider IEEE definition of software) so these don't have any documentation, nor do I guarantee their successful run in all test cases. Its up to you to edit, modify, improve the programs. At the time of posting I didn't check the programs, so I won't be liable for any bugs. ANother thing is that these programs should be bug free. If you are getting any error from your compiler then it may be some trivial issues like conio.h or anything like that. In such a case do some research and elliminate the bug yourself, this will help you in your academics. But if you aren't able to do, then post a comment here and tell me which program is giving what error in which compiler and I'll post the solution.

Here I start, from easy to tougher:

1. Program to print the table of a user entered number.

#include <iostream.h>
int a, i;
int main()
{
cout<<"Enter the number whose table u want to print:\n ";
cin>>a;
for(i=1;i<=10;i++)
{
cout<<a<<" * "<<i<<"="<<a*i<<"\n";
}
return (0);
}


2. Program to find factorial of a number using recursion

#include <iostream.h>
long double fact(long int num){
if (num==1){
return (1);
}
else{
return (double)(num*fact(num-1));
}
}
int main(){
long int a;
cout<<"Enter number:\n";
cin>>a;
cout<<fact(a)<<endl;
return (0);
}



3. Program to find sum of digits of a number

#include <iostream.h>
#include <conio.h>
void main(){
int a, s=0, i, r;
cout<<"Enter the number: ";
cin>>a;
for(i=0; a>=9; i++){
r=a%10;
a=(a/10);
s=s+r;
}
cout<<"The sum of digits is: "<<s+a;
getch();
}



4. Program to simplify a rational number to fractions(e.g. 6/8 to 3/4)

#include<iostream.h>
void fract(int a, int b){
int min=a;
if(b<min){
min=b;
}
for(int n=1; n<=min; n++){
if((a % n == 0) && (b % n == 0)){
a=a/n;
b=b/n;
min=min/n;
n=1;

}
}
cout<<"The required fraction is "<<a<<"/"<<b;
}

void main()
{
int x, y;
cout<<"Enter the numerator:";
cin>>x;
cout<<"Enter the denominator:";
cin>>y;
fract(x, y);
}



5. Pogram to find whether a string is a palindrome or not

#include <stdio.h>
//#include <conio.h>
#include <process.h>
void main(){
char input[20];
int length=0;
printf("Enter the word for palindrome test:\n");
gets(input);
//Find the length of entered string
while(input[length]!='\0'){
length++;
}
//Now check for palindrome status
for (int i=0; i<=length--; i++){
if (input[i]!=input[length]){
printf("Entered word isn't a palindrome.");
exit(0);
}
}
printf("Entered word is a palindrome.");
// getch();
}


6. Program to print all prime numbers upto the number input by the user

#include <iostream.h>
#include <math.h>
int main(){
int i=0, n, j=0, flag=0, mid;
cout<<"Enter the number upto where you want prime numbers to be displayed:\n";
cin>>n;
cout<<"Following are the required prime numbers:\n2\n3\n";
for(i=2;i<=n;i++){
mid = floor(sqrt(i));
for(j=2;j<=mid;j++){
if(i%j==0){
flag=0;
break;
}
else{
flag=1;
}
}
if(flag==1){
cout<<i<<"\n";
}
}
return (0);
}

7. Your own strlen function. Program to find the length of a user input string

#include <iostream.h>
int i;
int strlen(char *input){
for(i=0; input[i]!='\0'; i++){
}
return i;
}
int main(){
char *data;
cin>>data;
cout<<strlen(data);
return (0);
}


8. Your own strrev function. Program to reverse a user input string

#include <iostream.h>
#include <stdio.h>
int main(){
char *name;
cout<<"Enter the string:";
gets(name);
int count=0;
for(int i=0; name[i]!='\0'; i++){
count++;
}
for (int i=count; i>=0; i--){
cout<<name[i];
}
return (0);
}

9. Program to reverse a number

#include<iostream.h>
#include<conio.h>
void main()
{
int a,b;
int y=1,revs=0;
cout<<"No daalo";
cin>>a;
while(y>0)
{
b=a%10;
a=a/10;
revs=revs*10+b;
y=a;
}
cout<<revs;
getch();
}


10. Demonstrate a practical use of switch case

#include<iostream.h>
#include<conio.h>
int main()
{
float r,a;
char n;
cout<<"To find area of circle press C and for sphere press S:";
cin>>n;
switch(n)
{
case 'c':
{
cout<<"Enter the radius of the circle: ";
cin>>r;
a=3.1414*r*r;
cout<<"Area of the circle is " <<a;
break;
}
case 's':
{
cout<<"Enter the radius of the sphere: ";
cin>>r;
a=4*3.1414*r*r;
cout<<"Area of the sphere is " <<a;
break;
}
default:
cout<<"invalid entry!!!!!!!";
}
getch();
return(0);
}



11. Program to compute the union of 2 or more sets

#include <iostream.h>
int b;
int howmany(){
cout<<"How many elements you want to enter in this set";
cin>>b;
return b;
}
void main(){
int a, c[100], i=0, j=0, k=0;
cout<<"How many sets do you want to enter?";
cin>>a;
if (a<2){
cout<<"Blah blah! Enter at least two sets moron, for computation of union!";
}

for(int m=k;k<a;m++){
j=j+howmany();

cout<<"Enter the elements of this set one by one\n";
for(int n=i;i<j;n++)
{
cin>>c[i];
i++;
}
k++;
}

int temp=0;
for(a=0;a<i;a++){
for(b=a+1;b<i;b++){
if(c[a]==c[b]){
c[b]=0;
}

}
}
/////////////////////
cout<<"The union of all the sets above is:\n";
for (a=0;a<i;a++){
if (c[a]!=0){
cout<<c[a]<<"\n";
}
}
}



12. Program for matrix addition and multiplication using operator overloading and classes and ADT

#include <iostream.h>
int i, j, k, num;
class matrix{
int element[3][3];
public:
void display(){
for (int i=0;i<=2;i++){
for(int j=0; j<=2; j++){
cout<<element[i][j];
cout<<"\t";
}
cout<<"\n";
}
}
void getdata(){
cout<<"Enter the elements of matrix row wise:\n";
for (int i=0;i<=2;i++){
for(int j=0; j<=2; j++){
cin>>element[i][j];
}
cout<<"\n";
}
}

matrix operator +(matrix target){
matrix temp;
for(int i=0;i<=2;i++){
for(int j=0;j<=2;j++){
temp.element[i][j] = element[i][j]+target.element[i][j];
}
}
return temp;
}
matrix operator *(matrix target){
matrix temp;
int num=0;
for (i=0;i<=2;i++){
for(j=0;j<=2;j++){
for(k=0;k<=2;k++){
num = ((element[i][k])*(target.element[k][j]) + num);
}
temp.element[i][j] = num;
num=0;
}
}
return temp;
}
};
void main(){
matrix a, b,c ,d;
a.getdata();
b.getdata();
c.getdata();
d=a*b+c;
d.display();

}



13. Program to find the age difference between two dates using operator overloading and classes and ADT (Note that, calculating age difference is not as simple as normal subtraction other 3 part data like KM,M and CM trio)

#include <iostream.h>
#include <process.h>
class date{
int day;
int month;
int year;
bool leap;
public:
void getdata(void){
cout<<"Enter year(eg. 1988): \n";
cin>>year;
if((year%100)==0){
if((year%400)==0){
leap=true;
}
}
else if((year%4)==0){
leap=true;
}
else leap=false;
inmonth:
cout<<"Enter month(eg. 11): \n";
cin>>month;
if((month>12) || (month<1)){
cout<<"Invalid month entered. Please re-enter:\n";
goto inmonth;
}
inday:
cout<<"Enter day(eg. 25):\n";
cin>>day;
if((day>31) || (day<1)){
cout<<"Invalid day entered. Please re-enter:\n";
goto inday;
}
else if(((4==month)||(6==month)||(9==month)||(11==month))&&(day>30)){
cout<<"This month can't have more than 30 days. Please re-enter:\n";
goto inday;
}
else if((2==month)&&(leap==true)&&(day>29)){
cout<<"February can't have more than 29 days in a leap year. Please re-enter:\n";
goto inday;
}
else if((2==month)&&(leap==false)&&(day>28)){
cout<<"February can't have more than 28 days in a non-leap year. Please re-enter:\n";
goto inday;
}
}
void disp(){
cout<<year<<" years "<<month<<" months "<<day<<" days\n";
}
date operator-(date target){
int last_day_diff;
date result;
if(target.year>year){
cout<<"Please enter a higher date to subtract.";
exit(0);
}
if(target.day<=day){
result.day=day-target.day;
}
else if(target.day>day){
switch (month){
case 4:
case 6:
case 9:
case 11:
last_day_diff=30-target.day;
break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
last_day_diff=31-target.day;
break;
}
if ((true==leap)&&(2==month)){
last_day_diff=29-target.day;
}
else if((false==leap)&&(2==month)){
last_day_diff=28-target.day;
}
result.day=last_day_diff+day;
month--;
}
if(target.month<=month){
result.month=month-target.month;
}
else if(target.month>=month){
result.month=12-target.month+month;
year--;
}
result.year=year-target.year;
return result;
}
};


int main() {
date a, b, result;
cout<<"Enter higher date:\n";
a.getdata();
cout<<"Enter lower date:\n";
b.getdata();
result=a-b;
result.disp();
return 0;
}



14. Program to add and multiply two complex numbers using class, operator overloading, ADT

#include <iostream.h>

int abs(int input){
if (input>=0){
return input;
}
else{
return (-1*input);
}
}

class complex{
public:
int number[2]; //array to store real and imaginary part
complex(){ //constructor to intialize to zero
number[0]=0;
number[1]=0;
}

void display(){
if (number[1]>0){
cout<<number[0]<<"+"<<number[1]<<"i";
}
if (number[1]<0){
cout<<number[0]<<"-"<<abs(number[1])<<"i";
}
cout<<"\nReal part is: "<<number[0]<<" and imaginary part is: "<<number[1];

}

void getdata(){
cout<<"Enter real part:";
cin>>number[0];
cout<<"Enter imaginary part:";
cin>>number[1];

}

complex operator +(complex target){
complex temp;
temp.number[0] = number[0]+target.number[0];
temp.number[1] = number[1]+target.number[1];
return temp;
}

complex operator *(complex target){
complex temp;
temp.number[0] = (number[0]*target.number[0])-(number[1]*target.number[1]);
temp.number[1] = (number[0]*target.number[1])+(number[1]*target.number[0]);
return temp;
}

};
void main(){
complex a, b, c ,d;
a.getdata();
b.getdata();
c.getdata();
d=a*b+c;
d.display();

}



15. Program to print all ascii printable characters in a file instead of screen

#include <iostream.h>
#include <fstream.h>
int main(){
char i;
char path[255];
cout<<"Enter path:\n";
cin>>path;
ofstream file(path);
for(i=48; i<=122; i++){
file<<i<<"\n";
}
cout<<"Data written. Exiting";

return (0);
}

Saturday, April 3, 2010

For the GATE-2011 Aspirants

I will soon launch a site with all the resources related to GATE CS preparation. First let me come out of the vicious admissions phase. Then I'll post all the resources I used, for the preparation online. Everything will be very well categorized subject wise.

Wednesday, February 17, 2010

GATE 2010 CS Solutions

Gateforum announced that they will release solutions by 7:30PM on Feb15th, but they didn't. None other coaching institute did. All of them released answer keys only and slept, keeping the students in confusion.
So, I decided, I will post the solutions to selected questions.

If you are just preparing for GATE, you should solve more and more questions. And for that I would recommend GKP Publisher's Question Bank. Although there are some mistakes in the book for solutions to some problems, but I recommend this book solely for the huge collection of problems it has.

Disclaimer: These are my developed solutions. I am a student and I can't guarantee the correctness of the solutions. And yeah, don't think that I solved them all correct. I am not a computer scince wizard either.

Note: Click on the images to enlarge.

First, get the question paper here or here(Thanks Sapan for the second version!)

Now, lets start!

Q-1: I fiddled with it and got the correct answer but didn't mark it cuz of fear of getting it wrong!


Q-2, 3,4,5 I skipped because I had skipped large amount of discrete maths portion and I'm not comfortable with serious topics.

Q-6:
The rightmost thing appearing in this image is P. Read it as PR'. I stripped the R' by mistake while cropping, and overwrote the image.


Q-7:
I hit B as explained below.
The refresh operation in a DRAM chip cycles through each row reading the contents of each cell in the row and writing them back.
For the given problem our RAM module will have 32 chips. Pretty easy calculation i.e. (4M*8bits)/(1M*8bits). One refresh cycle refreshes 1 row of each chip being used to construct the module. Thus we require 1K refresh cycles to refresh the wole module by refreshing 1 row of each chip in the module at once.
Hence time required will be 100*(2^10)ns

Q-8:
F 8 7 B is
1111 1000 0111 1011
Multiplying by 8 means multiplying by 2, three times successively. That accounts for 3, 1-bit left shifts i.e. 3-bit left shift.
On performing 3-bit left shift on the number we get
1100 0011 1101 1000
Thats C 3 D 8

Q-9:
Using the equation of multiplexer we have
f=RP'Q'+R'P'Q+R'PQ'+RPQ
=P'(Q'R+R'Q)+P(R'Q'+RQ)
=P'(Q XOR R)+P(Q XNOR R)........(since, A'B+AB' is A XOR B and AB+A'B' is A XNOR B)
=P'(Q XOR R)+P(Q XOR R)'
=P XOR (Q XOR R)
=P XOR Q XOR R..................(since, XOR is associative)

Q-10: I fiddled with it for quite some time and hit C. I believe I was wrong.

Q-11:
I marked B due to some misunderstanding and being in a hurry(dunno why?)!
Proceed to the function call. We have the address of i in p and address of j in q. Now the operation p=q; will transfer j's address in p. The second operation *p=2; will put 2 at j's address. i's address will remain untouched. Hence after returning from the function we get 0 2 as output from the printf function.

Q-12:
I hit D cuz of a calculation mistake.


Q-13B 14C 15D

Q-16: Seems to be an ambiguous question. A and D are the contenders for being the answer. I hit A.

Q-17:


Q-18:
Each node except root in a B+ Tree can hold between T-1 and 2T-1 keys where T is the order of the tree.
In the problem, we have
2T-1=5
=>2T=6
=>T=3
So the min. i.e. T-1 will be
=>T-1=2 hence option B

Q-19 C. Thats pretty obvious

Q-20: I hit A. But thats wrong. 2PL doesn't guarantee freedom from deadlocks. Timestamp ordering does. So B is the right answer.

Q-21: I hit D. I don't have any explanation for it. I had skipped this portion while studying Software Engineering.

Q-22: B is pretty obvious

Q-23: I skipped. I am not comfortable with this thing.

Q-24: 196 is the obvious answer. No need to explain.

Q-25: I hit B but thats wrong. D will be correct. Read Galvin's book.

Q-26: I skipped. I'm not comfortable with probability.

Q-27: Same as above.

Q-28: I drew some example graphs and guessed the answer to be D. And I got it correct!!!!

Q-29: D


Q-30: B
For all x, there exists y, there exists t, not F(x,y,t)
For every person x out there, there is a person y, and a time t such that x can't fool y at time t.
Option B i.e. No one can fool everyone all the time expresses the meaning best.

Q-31:
A is correct. This is a simple problem involving De Morgan's law. No need to explain.

Q-32: I skipped. No idea. I ain't an electronics wizard.

Q-33: B


Q-34:
I took an example sequence and hit A after applying some unexplainable heuristics. :P

Q-35:
Just follow the recursive call, you'll get 12+(7-(13-(4+(11-(6))))) which is 15.

Q-36:
D is the obvious answer. Moving the last element to the first means setting the pointer of second last element to null and setting head to point to the value of last element(now first) and setting its next pointer point to the to the previous first node.

Q-37:
d=a+b(requires 3 registers. 2 for operands, 1 for result)
e=c+d(a is not used as an operand anywhere ahead so we can overwrite it as e. so, still 3 registers)
f=c+e(we don't need d as an operand, so overwrite it as f. still 3 registers)
b=c+e(we don't need c as operand, so overwrite it as b. still 3 registers)
e=b+f(we don't need b as operand, so overwrite it as e. still 3 of them)
d=5+e(we can overwrite e with the new d. still 3)
return d+f
We're done with 3 registers. But I marked 4 registers. Because in the middle of this whole evaluation I toppled on a variable and needed an extra one.

Q-38: I am uncomfortable with compiler design. I skipped.

Q-39: This one seems to be ambiguous. Consider the string 1010011. It has even number of ones. But none of the given regular expressions can generate it. I didn't mark anything.

Q-40: A CFG can keep the count of 1 thing. D seems to be the obvious answer. I hit C. Even I don't know why!!!!

Q-41: I hit B because I thought, to accept a substring we need equal or lesser number of states. Dunno whether I am write or wrong, but everyone says I am wrong. Rest in peace.

Q-42:
I hit C cuz of a confusion. *damn*


Q-43:


Q-44:
Go through the test conditions T1 through T4.
You'll deduce that:
T1->S1
T2->S3
T3->S1
T4->S2,S4

so T1,T2,T4->{S1.S2,S3,S4)

Q-45 and 46 I skipped because I am scared of these things.

Q-47: D is the obvious answer. No need to explain. Just bitwise AND 255.255.255.224 to 10.105.1.113 and 10.105.1.91 and you will get different network addresses.

Q-48 and 49:


Q-50 and 51:
Label the rows and columns of the matrix as 0,1,2,3,4 and draw the graph. Apply Kruskal's algorithm on Q-50 skipping the second 1 weight edge.

Edit: For Q-51 the required path is 1-0-4-2 and that is 8 units.. I made the same mistake(rather blunder) in exam hall as well which will set me back by -2.66 marks andaround 150-250 rank positions.

Q-52: Pretty easy. Ans. C

Q-53: I made some heuristics and guessed the ans. to be C 30. And that seems to be right! Al the coaching institutes converge at that answer!

Q-54 and 55: I think I got both of them wrong. I hit B and D respectively.

Monday, February 15, 2010

GATE - 2010

If you are just preparing for GATE, you should solve more and more questions. And for that I would recommend GKP Publisher's Question Bank. Although there are some mistakes in the book for solutions to some problems, but I recommend this book solely for the huge collection of problems it has.

Graduate Aptitude Test in Engineering - GATE 2010 was conducted all over India yesterday. I too appeared in the CS paper. The various answer keys provided by different coaching institutes are all differing in their answers. Roughly I'll be getting 45 marks. The results will be declared on March 15th 2010. Here I'm posting all the available answer keys for now.

Answer Key From Gateforum
http://gateforum.com/downloads/CSGATE2010Key.pdf

Answer Key From GATECounsellor



Answer Key From Made Easy Group(Click to enlarge)


Answer Key From Ace Engineering Academy(Click to enlarge)



Answer key from Vani Institute

Some questions seem to be pretty disputed. The question numbers are 7,16,33,34,37,39,48,49,50,51,54,55,63

Anyways, the answer key from Made Easy group seems to be the worst with loads of wrong answers.

Lets see what happens. Everything will be clear on March 15th.