ISC Practical Paper Computer Science

ISC Practical Paper 2000 2001 2002 2003 2004

ISC Practical Paper COMPUTER SCIENCE
(Reading Time : 15 minutes) (Planning Session AND Examination Session : Three Hours)
—– —– —– —– —–
The total time to be spent on the Planning Session and the Examination Session is Three Hours. After completing the Planning Session, the candidates may begin with the Examination Session. A maximum of 90 minutes is permitted for the Planning Session. However, if candidates finish earlier, they are to be permitted to begin the Examination Session.
(Maximum Marks : 80)
—– —– —– —– —–

INSTRUCTIONS
As it is a practical examination the candidate is expected to do the following:
(a) Write an algorithm for the selected problem.
(b) Write a program in C++ or Java. Document your program by using mnemonic names and comments.
(c) Test run the program on the computer using the given test data and get a print out (hard copy) in the format specified in the problem along with the program listing.

Solve any one of the following problems

 

2000

Question 1.
Write a program to print a rectangle with diamond shape gap exactly at the center of the rectangle using a n input string with odd numbers of character not exceeding 20.
Example:
Input: HAPPY-HAPPY
Output:
HAPPY-HAPPY
HAPPY HAPPY
HAPP     APPY
HAP         PPY
HA             PY
A                 Y
HA             PY
HAP         PPY
HAPP     APPY
HAPPY HAPPY
HAPPY-HAPPY
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
int i,j;
clrscr();
char a[20];
cin>>a;
clrscr();
int l=strlen(a);
int n=l/2;
cout<<a;
for(i=1;i<=l/2;i++)
{
cout<<endl;
for(j=0;j<=n-i;j++)
cout<<a[j];
for(j=0;j<(i*2-1);j++)
cout<<” “;
for(j=n+i;j<l;j++)
cout<<a[j];
}
for(i=l/2-1;i>0;i–)
{
cout<<endl;
for(j=0;j<=n-i;j++)
cout<<a[j];
for(j=0;j<(i*2-1);j++)
cout<<” “;
for(j=n+i;j<l;j++)
cout<<a[j];
}
cout<<endl<<a;
getch();
}
public class ISC00Q1
{
public static void main()
{
int i,j;
String a=”HAPPY-HAPPY”;
int l=a.length();
int n=l/2;
System.out.print(a);
for(i=1;i<=l/2;i++)
{
System.out.println();
for(j=0;j<=n-i;j++)
System.out.print(a.charAt(j));
for(j=0;j<(i*2-1);j++)
System.out.print(” “);
for(j=n+i;j<l;j++)
System.out.print(a.charAt(j));
}
for(i=l/2-1;i>0;i–)
{
System.out.println();
for(j=0;j<=n-i;j++)
System.out.print(a.charAt(j));
for(j=0;j<(i*2-1);j++)
System.out.print(” “);
for(j=n+i;j<l;j++)
System.out.print(a.charAt(j));
}
System.out.println(“\n”+a);
}
}
Question 2.
Write a program to input two valid dates, each comprising of day (2 digit) month (2 digit) and year (4 digit) and calculate the day elapsed between the two dates.
Test your program with following data values:
Example:
First date: Day: 24
Month : 09
Year : 1960
Second Date: Day: 10
Month: 12
Year: 1852First date: Day: 10
Month : 01
Year : 1952
Second Date: Day: 16
Month: 10
Year: 1952
#include <conio.h>
void main()
{
clrscr();
int dd1,mm1,yy1;
int dd2,mm2,yy2;
int temp;
cout<<“First Date (Smaller):”<<endl;
cout<<“Day : “;cin>>dd1;
cout<<“Month : “;cin>>mm1;
cout<<“Year : “;cin>>yy1;
cout<<“Second Date (Bigger):”<<endl;
cout<<“Day : “;cin>>dd2;
cout<<“Month : “;cin>>mm2;
cout<<“Year : “;cin>>yy2;
if( (yy2*365+mm2*30+dd2) < (yy1*365+mm1*30+dd1) )
{
temp=dd1; dd1=dd2; dd2=temp;
temp=mm1; mm1=mm2; mm2=temp;
temp=yy1; yy1=yy2; yy2=temp;
}
int dn=0;
int m[]={0,31,28,31,30, 31,30,31,31,30,31,30,31};
while(dd1!=dd2 || mm1!=mm2 || yy1!=yy2)
{
dd1++;
dn++;
m[2] = (yy1%4==0) ? 29 : 28;
if(dd1>m[mm1])
{
mm1++;
dd1=1;
}
if(mm1>12)
{
mm1=1;
yy1++;
}
}
cout<<“No of days : “<<dn;
getch();
}
import java.io.*;
class ISC00Q2
{
public static void main()throws IOException
{
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(isr);
int temp,dd1,mm1,yy1,dd2,mm2,yy2;
System.out.println(“First Date:”);
System.out.print(“Day : “);
dd1=Integer.parseInt(in.readLine());
System.out.print(“Month : “);
mm1=Integer.parseInt(in.readLine());
System.out.print(“Year : “);
yy1=Integer.parseInt(in.readLine());
System.out.println(“Second Date:”);
System.out.print(“Day : “);
dd2=Integer.parseInt(in.readLine());
System.out.print(“Month : “);
mm2=Integer.parseInt(in.readLine());
System.out.print(“Year : “);
yy2=Integer.parseInt(in.readLine());
if( (yy2*365+mm2*30+dd2) < (yy1*365+mm1*30+dd1))
{
temp=dd1; dd1=dd2; dd2=temp;
temp=mm1; mm1=mm2; mm2=temp;
temp=yy1; yy1=yy2; yy2=temp;
}
int dn=0;
int m[]={0,31,28,31,30,31, 30,31,31,30,31,30,31};
while(dd1!=dd2 || mm1!=mm2 || yy1!=yy2)
{
dd1++;
dn++;
m[2] = (yy1%4==0) ? 29 : 28;
if(dd1>m[mm1])
{
mm1++;
dd1=1;
}
if(mm1>12)
{
mm1=1;
yy1++;
}
}
System.out.print(“No of days : “+dn);
}
}
Question 3.
A File contains a list of names and telephone numbers in the following form:
AJIT 281050
ANIL 462890
HRISHIKESH 524358
—-
—-
—-
The names contains only one word and the telephone numbers are separated by white spaces.
Write an interactive programs to:
(i) Create the above file.
(ii) Display the contents in two columns.
(iii) Search a telephone number for a given name.
(iv) Exit the program.
#include<fstream.h>
#include<stdlib.h>
#include<conio.h>
#include<iomanip.h>
#include<string.h>
char Name[30], Phone[10], ans=’y’;
void NewEntry()
{
clrscr();
ofstream outfile(“Telephone.txt”, ios::app);
if(!outfile)
{
cerr<<“file could not be found”;
exit(1);
}
while(ans==’y’ || ans==’Y’)
{
clrscr();
gotoxy(10,1);
cout<<“ENTER DATA IN PHONE BOOK”<<endl;
gotoxy(10,2);
cout<<“============ ============”<<endl;
gotoxy(1,4);
cout<<“ENTER NAME : “; cin>>Name;
gotoxy(1,6);
cout<<“ENTER PHONE No. : “; cin>>Phone;
outfile<<Name<<” “<<Phone<<endl;
gotoxy(1,9);
cout<<“DO YOU WANT TO ADD MORE RECORDS (y/n): “; cin>>ans;
}
}void Display()
{
clrscr();
ifstream infile(“Telephone.txt”,ios::in);
if(!infile)
{
cerr<<“file could not be found”;
exit(1);
}
gotoxy(7,1);
cout<<“PHONE BOOK DETAILS”<<endl;
gotoxy(7,2);
cout<<“==================”<<endl<<endl;
cout<<“============ ==================”<<endl;
cout<<setiosflags(ios::left) <<setw(20)<<“NAME” <<setw(35)<<“PHONE No.” <<endl;
cout<<“================ ==============”<<endl;
while(infile>> Name>>Phone)
{
cout<<setiosflags(ios::left) <<setw(20)<<Name<<setw(35) <<Phone<<endl;
}
cout<<“============= =================”<<endl;
getche();
}
void Search()
{
char ser[30];
char found=’f’;
ifstream infile(“Telephone.txt”, ios::in);
if(!infile)
{
cerr<<“file could not be found”;
exit(1);
}
clrscr();
cout<<“\nEnter Name : “;cin>>ser;
while(!infile.eof())
{
infile>>Name>>Phone;
if(strcmp(Name,ser)==0)
{
cout<<“Phone No. : “<<Phone<<endl;
found=’t’;
}
}
if(found==’f’)
{
cout<<“\n####################”;
cout<<“\nRECORD DOESNOT EXIST”;
cout<<“\n####################”;
}
getche();
}void main()
{
clrscr();
int x,ch;
while(1)
{
cout<<“Main Menu\n”;
cout<<“1> New Phone No. Entry”<<endl;
cout<<“2> Display Phone Book”<<endl;
cout<<“3> Search a Phone No.”<<endl;
cout<<“4> Exit\n”;
cout<<“Enter choice?”;
cin>>ch;
switch(ch)
{
case 1: NewEntry();break;
case 2: Display();break;
case 3: Search();break;
case 4: exit(0);break;
default: cout<<“Invalid Option”;getche();
}
clrscr();
}
}
import java.io.*;
public class ISC00Q3
{
static InputStreamReader isr=new InputStreamReader(System.in);
static BufferedReader stdin=new BufferedReader(isr);
static String Name;
static int Phone;
private static void NewEntry() throws IOException
{
FileWriter fw=new FileWriter(“PHONE.txt”,true);
BufferedWriter bw=new BufferedWriter(fw);
PrintWriter outFile=new PrintWriter(bw);
for(int i=1;i<=5;i++)
{
System.out.print(“Enter Name : “);
Name=stdin.readLine();
System.out.print(“Enter Marks : “);
Phone=Integer.parseInt(stdin.readLine());
outFile.println(Name+” “+Phone);
}
outFile.close();
}
private static void Display() throws IOException
{
FileReader fr=new FileReader(“PHONE.txt”);
BufferedReader br=new BufferedReader(fr);
System.out.println(“Names Phone No.”);
while(true)
{
Name=br.readLine();
if(Name==null)
break;
System.out.println(Name);
}
br.close();
}
private static void Search() throws IOException
{
FileReader fr=new FileReader(“PHONE.txt”);
BufferedReader br=new BufferedReader(fr);
String N;
boolean found=false;
System.out.print(“Enter Names : “);
N=stdin.readLine();
N=N.toUpperCase();
while(true)
{
Name=br.readLine();
if(Name==null)
break;
String SN=Name.substring(0,Name.indexOf(‘ ‘));
if(SN.equals(N))
{
System.out.println (“Phone No : ” +Name.substring( Name.indexOf(‘ ‘)));
found=true;
}
}
if(found==false)
System.out.println (“Name not present in Phone Book”);
br.close();
}
public static void main() throws IOException
{
int x,ch=0;
do
{
System.out.println(” -: Main Menu :-“);
System.out.println(“[1] New Phone No. Entry”);
System.out.println(“[2] Display Phone Book”);
System.out.println(“[3] Search a Phone No.”);
System.out.println(“[4] Exit”);
System.out.print(“Enter choice? “);
ch=Integer.parseInt(stdin.readLine());
switch(ch)
{
case 1: NewEntry();break;
case 2: Display();break;
case 3: Search();break;
case 4: break;
default: System.out.println (“Invalid Option”);
}
}while(ch<4);
}
}

2001

Question 1.
Consider the sequence of natural numbers
1, 2, 3, 4, 5, 6, 7……Removing every second number produces the sequence
1, 3, 5, 7, 9, 11, 13, 15, 17, ……….Removing every third number produces the sequence
1, 3, 7, 9, 13, 15, 19, 21, 25, 27, ………This process continues indefinitely by removing fourth, fifth …. and so on, till after a fixed number of steps certain numbers remains indefinitely. These are known as lucky numbers. Write a program to generate and print lucky numbers less then a given natural number N where N<=50.
#include <iostream.h>
#include <conio.h>
// 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// 1 3 5 7 9 11 13 15 17 19
// 1 3 7 9 13 15 19
// 1 3 7 13 15 19
// 1 3 7 13 19
void main()
{
int n;
clrscr();
cout<<“Enter the value of N : “;
cin>>n;
int a[100];
for(int i=0;i<n;i++)
a[i]=i+1;
int k,j,ctr=0,step=1;
clrscr();
for(i=0;i<n-ctr;i++)
{
for(j=i+1;j<n-ctr;j+=step)
{
for(k=j;k<n;k++)
{
a[k]=a[k+1];
}
ctr++;
}
step++;
}
cout<<“Lucky Numbers are : “;
for(i=0;i<n-ctr;i++)
cout<<a[i]<<” “;
getch();
}
public class ISC01Q1
{
// 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// 1 3 5 7 9 11 13 15 17 19
// 1 3 7 9 13 15 19
// 1 3 7 13 15 19
// 1 3 7 13 19
public static void main(int n)
{
int i;
int a[]=new int[100];
// array filling
for(i=0;i<n;i++)
a[i]=i+1;
int k,j,ctr=0,step=1;
for(i=0;i<n-ctr;i++)
{
for(j=i+1;j<n-ctr;j+=step)
{
for(k=j;k<n;k++)
{
a[k]=a[k+1];
}
ctr++;
}
step++;
}
System.out.print(“Lucky Numbers are : “);
for(i=0;i<n-ctr;i++)
System.out.print(a[i]+” “);
}
}
Question 2.
Input consist of month number (MM), the day of the month (DD) and the year (YYYY). Write a program to calculate and print the corresponding day of the year ( in the range 1 to 366).

Example:

Input: Month 05
Day 03
Year 1996
Output: CORRESPONDING DAY OF THE YEAR IS : 124
( 31 + 29 + 31 + 30 + 3 = 124 )
Test your program with the following sample data.
MONTH DAY YEAR
09    07 2000
05    03 1954
12    13 1960
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int dd1=1,mm1=1;
int dd2,mm2,yy2;
cout<<“First Date (Smaller):”<<endl;
cout<<“Day : “;cin>>dd2;
cout<<“Month : “;cin>>mm2;
cout<<“Year : “;cin>>yy2;
int dn=0;
int m[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
m[2] = (yy2%4==0) ? 29 : 28;
for(int i=1;i<mm2;i++)
dn+=m[i];
dn+=dd2;
cout<<“CORRESPONDING DAY OF THE YEAR IS = “<<dn;
getch();
}
import java.io.*;
public class ISC01Q2
{
public static void main() throws IOException
{
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader stdin=new BufferedReader(isr);
int dd1=1,mm1=1;
int dd2,mm2,yy2;
System.out.println(“INPUT”);
System.out.print(“Month : “);
mm2=Integer.parseInt(stdin.readLine());
System.out.print(“Day : “);
dd2=Integer.parseInt(stdin.readLine());
System.out.print(“Year : “);
yy2=Integer.parseInt(stdin.readLine());
int dn=0;
int m[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
m[2] = (yy2%4==0) ? 29 : 28;
for(int i=1;i<mm2;i++)
dn+=m[i];
dn+=dd2;
System.out.println(“CORRESPONDING DAY OF THE YEAR IS = “+dn);
}
}
Question 3.
Write a program which inputs Natural numbers N and M followed by integer arrays A[] and B[], each consisting of N and M numbers of elements respectively. Sort the arrays A[] and B[] in descending order of magnitude. Use the sorted arrays to generate a merged array C[]. Arrays C[] should be generated in descending order.
Assume the input arrays to comprise maximum 20 elements each, with no duplicates. Common elements should be should be included in the merged array only once. The output format is as follows:
Sorted Array
A[] B[]
xxx xxx
— —
— —
— —Merged Array
C[]
xxx


—Test your program with the following data samples:
N=5 M=8
A[] 4 16 2 9 26
B[] 24 3 2 12 5 9 1 16
import java.io.*;
public class ISC01Q3
{
public static void main() throws IOException
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader stdin=new BufferedReader(isr);
int N,M;
System.out.print(“Enter First Array Size N : “);
N=Integer.parseInt(stdin.readLine());
System.out.print(“Enter Second Array Size M : “);
M=Integer.parseInt(stdin.readLine());
int A[]=new int[N];
int B[]=new int[M];
int C[]=new int[M+N];
int i,j,t,k=0;
for(i=0;i<N;i++)
{
System.out.print(“Enter elements in First array : “);
A[i]=Integer.parseInt(stdin.readLine());
}
for(i=0;i<M;i++)
{
System.out.print(“Enter elements in Second array : “);
B[i]=Integer.parseInt (stdin.readLine());
}
//sorting the array A and B
for(i=0;i<N-1;i++)
{
for(j=0;j<N-i-1;j++)
{
if(A[j]<A[j+1])
{
t=A[j];
A[j]=A[j+1];
A[j+1]=t;
}
}
}
for(i=0;i<M-1;i++)
{
for(j=0;j<M-i-1;j++)
{
if(B[j]<B[j+1])
{
t=B[j];
B[j]=B[j+1];
B[j+1]=t;
}
}
}
// Diaplying the array A and B
System.out.print(“\nSorted Array A : “);
for(i=0;i<N;i++)
System.out.print(A[i]+ ” “);
System.out.print(“\nSorted Array B : “);
for(i=0;i<M;i++)
System.out.print(B[i]+ ” “);
// Performing MERGE Sort
i=0;j=0;k=0;
while(k<10)
{
if(A[i]>B[j])
{
C[k]=A[i];
k++;
i++;
if(i>N-1)
{
while(k<N+M)
{
C[k]=B[j];
k++;
j++;
}
break;
}
}
else
{
C[k]=B[j];
k++;
j++;
if(j>M-1)
{
while(k<N+M)
{
C[k]=A[i];
k++;
i++;
}
break;
}
}
}
System.out.print (“\nSorted Array C : “);
for(i=0;i<k;i++)
System.out.print(C[i]+ ” “);
}
}

2002

Question 1.
Twin primes are consecutive prime numbers whose difference is 2. For example, (3,5), (11,13), (17,19) are all twin primes. We define the distance of any twin prime pair from a positive integer as follows:If (p1,p2) is a twin prime pair and n is a positive integer then the distance of the twin prime pair from n is:
minimum(abs(n-p1), abs(n-p2)) where abs returns the absolute value of its argument, and minimum returns the smaller of its two arguments.Write a program that reads in a positive integer n and prints out the twin prime pair that has the least distance from n.
For example if n is 30 the pair is (29,31), if n is 13 it is (11,13), if n is 49 it is (41,43), if n is 54 it is (59,61).Sample data:
Input:
Give the number:34
Output:
Number read in is:34
p1=29 p2=31Input:
Give the number:60
Output:
Number read in is:60
p1=59 p2=61
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,k=30000;
int n,p2,p1,l;
int minimum(int,int);
int prime(int);
int abs(int);
cout<<“Enter a number : “;
cin>>n;
for(i=0;i<2*n;i++)
{
p1=prime(i);
p2=prime(i+2);
if (p1==0 || p2==0)
{
continue;
}
if(minimum(abs(n-p1), abs(n-p2))<k)
{
k=minimum(abs(n-p1),abs(n-p2));
j=p1;l=p2;
}
}
cout<<“\n twin prime nearest to ” <<n<<“=”<<j<<“,”<<l;
getch();
}
int abs(int n)
{
if(n<0)
return(-n);
else
return(n);
}
int minimum(int a,int b)
{
int n=(a<b)?a:b;
return(n);
}
int prime(int n)
{
int f=n;
for(int i=2;i<n-1;i++)
{
if(n%i==0)
{
f=0;
break;
}
}
return(f);
}
public class ISC02Q1
{
private int prime(int n)
{
int f=n;
for(int i=2;i<n-1;i++)
{
if(n%i==0)
{
f=0;
break;
}
}
return(f);
}
private int abs(int n)
{
if(n<0)
return(-n);
else
return(n);
}
private int minimum(int a,int b)
{
int n=(a<b)?a:b;
return(n);
}
public void main(int n)
{
int i,j=0,k=30000;
int p2=0,p1=0,l=0;
for(i=0;i<2*n;i++)
{
p1=prime(i);
p2=prime(i+2);
if(p1==0 || p2==0)
{
continue;
}
if(minimum(abs(n-p1),abs(n-p2))<k)
{
k=minimum(abs(n-p1),abs(n-p2));
j=p1;
l=p2;
}
}
System.out.println(“Twin prime nearest to ”
+n+”=”+j+”,”+l);
}
}
Question 2.
Write the program to do the following:Read in an integer n (which can be at most 50). Then read in n integers one by one and store them in an array data from index 0 to n-1. Now we want to rearrange the integers in data in the following way:Find the maximum value in data and put it in the center of the array (that is at (n/2)), find the next largest value and put it to its right, then the next largest and place it to its left and so on alternating right and left until all integers in data are done. For example, if the array is initially:7,3,1,6,4,2,5 then after rearranging it becomes 1,3,5,7,6,4,2However, since we have very little memory, you are not allowed to use any other array apart from data.
Sample data:
Input:
Give the number of integers: 5
Give integer 1: 7
Give integer 2: 9
Give integer 3: 2
Give integer 4: 5
Give integer 5: 6Output:
Original array
7 9 2 5 6
Rearranged array
2 6 9 7 5Input:
Give the number of integers:6
Give integer 1: 5
Give integer 2: 1
Give integer 3: 9
Give integer 4: 8
Give integer 5: 2
Give integer 6: 4Output:
Original array
5 1 9 8 2 4
Rearranged array
2 5 9 8 4 1
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int a[9]={3,7,8,1,9, 6,5,2,4};
int n=9;
int i,j,t,high,pos,hp;
int left=(n%2==1) ?(n/2):(n/2)-1,right=left+1;
for(j=0;j<n;j++)
cout<<a[j]<<” “;
for(i=0;i<n;i++)
{
// find out the position of the next highest number
high=-32767;
for(j=0;j<n;j++)
{
if(a[j]>high && (j<=left || j>=right))
{
high=a[j];
hp=j;
}
}
// find out the location where to put the number
if(i%2==0)
{
pos=left;
left–;
}
else
{
pos=right;
right++;
}
// swap the number
t=a[pos];
a[pos]=a[hp];
a[hp]=t;
}
cout<<endl;
for(j=0;j<n;j++)
cout<<a[j]<<” “;
getche();
}
public class ISC02Q2
{
public static void main()
{
int a[]={8,1,9,6,5,2,4};
int n=7;
int i,j,t,high,pos,hp=0;
int left=(n%2==1) ?(n/2):(n/2)-1;
int right=left+1;
for(j=0;j<n;j++)
System.out.print(a[j]+” “);
for(i=0;i<n;i++)
{
// find out the position of the next highest number
high=-32767;
for(j=0;j<n;j++)
{
if(a[j]>high && (j<=left || j>=right))
{
high=a[j];
hp=j;
}
}
// find out the location where to put the number
if(i%2==0)
{
pos=left;
left–;
}
else
{
pos=right;
right++;
}
// swap the number
t=a[pos];
a[pos]=a[hp];
a[hp]=t;
}
System.out.println();
for(j=0;j<n;j++)
System.out.print(a[j]+” “);
}
}
Question 3.
A mathematician who is working with extremely large integers them using a doubly linked list. For example, the number 7328 will be represented as the doubly linked list with four nodes.7 3 2 8The predecessor of the first node and the successor of the last node are null.Write a program that reads in two numbers (assume they are integers for testing), converts them to the above representation, prints them out with each digit separated by commas and checks whether the two numbers are equal or not. The equality check should work on the representation and not on the integers. Here are sample outputs.
Sample data:
Input:
Give the first number:34563
Give the second number:34562
Output:
First number:3,4,5,6,3
Second number:3,4,5,6,2
UnequalInput:
Give the first number:56432
Give the second number:56432
Output:
First number:5,6,4,3,2
Second number:5,6,4,3,2
Equal
#include<iostream.h>
#include<conio.h>
#include<process.h>
struct node
{
int val;
node *pre,*next;
}*head1=NULL, *head2=NULL;
void add( int x1, int x2)
{
node *head;
int x,c=0;
while( c<=1)
{
if( c==0 )
{
head=head1;
x=x1;
}
else if(c==1 )
{
head=head2;
x=x2;
}
node *p,*temp;
temp=head;
p=new node;
p->val=x;
if( head==NULL)
{
p->pre=NULL;
p->next=NULL;
head=p;
if( c==0 )
head1=p;
else if(c==1 )
head2=p;
}
else
{
while( temp->next!=NULL )
temp=temp->next;
p->pre=temp;
temp->next=p;
p->next=NULL;
}
c++;
}
}
int check()
{
int f=1;
node *p1,*p2;
p1=head1;p2=head2;
while( p1->next!=NULL)
{
if( (p1->val)!=(p2->val) )
{
f=0; break;
}
p1=p1->next;
p2=p2->next;
}
return(f);
}
void main()
{
clrscr();
long int x,y;
cout<<“Enter two numbers( <of the order pow(10,10)?”;
cin>>x>>y;
long int r1,r2,q1=x,q2=y;
while( q1>0 && q2>0)
{
r1=q1%10;r2=q2%10;
q1/=10;
q2/=10;
if( (q1==0 || q2==0) && (q1!=0 || q2!=0))
{
cout<<“Yours no.s can never be equal as they are of unequal digits”;
getch();
exit(0);
}
add(r1,r2);
}
if(check()==1)
cout<<“No.s are equal”;
else if(check()==0)
cout<<“numbers unequal”;
getch();
}

2003

Question 1.
A sample encryption system uses a shifting process to hide a number. The value of the shift can be in the range 1 to 26. For example a shift of 7 means that A = U, B = V, C = W, etc. i.e.Text: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Code: U V W X Y Z A B C D E F G H I J K L M N O P Q R S TFirst an extra space is added to the end of the string. To make things a little more difficult, spaces within the original text are replaced with QQ before the text is encrypted. Double Q (QQ) was selected because no English word ends in Q or contains QQ.Additionally the coded message is printed in blocks of six characters separated by spaces. The first block might not contain six characters. Write a program that takes the coded text (less than 100 characters), the shift value and prints the decoded original text. Your program must reject any non-valid value for shift and display an error message “INVALID SHIFT VALUE”. Assume all characters are upper case. Test your program for the following data and some data that you have coded, using the rules given above:
SAMPLE DATA:
INPUT:
CODED TEXT : “UHINBY LKKQCHHYLKK”
SHIFT : 7
OUTPUT:
DECODED TEXT : ANOTHER WINNERINPUT:
CODED TEXT : “RUIJGGEVGGBKSAGG”
SHIFT : 11
OUTPUT:
DECODED TEXT : BEST OF LUCKINPUT:
CODED TEXT : “DKSMMW NAMMUK QMM”
SHIFT : 29
OUTPUT:
INVALID SHIFT VALUE.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
#include<string.h>
int abs(int x)
{
if(x>=0)
return(x);
else
return(-x);
}
void main()
{
clrscr();
char a[100],b[100];
int l,k=0,i,s;
cout<<“enter string:”;
gets(a);
cout<<“\nenter shift:”;
cin>>s;
l=strlen(a);
if((s<0 || s>26) || (abs(a[l-1]-81)!=abs(s-1)))
{
cout<<“invalid shift”;getch();exit(0);
}
for(i=0;a[i]!=NULL;i++)
{
if( a[i]>=65 && a[i]<=90 )
{
if( a[i]>(91-s))
b[k]=a[i]+s-27;
else
b[k]=a[i]+s-1;
k++;
}
}
b[k]=NULL;
cout<<endl<<“Output:”;
for(i=0;b[i]!=NULL;i++)
{
if(b[i]==’Q’&& b[i+1]==’Q’)
{
cout<<” “;
i++;
}
else
cout<<b[i];
}
getch();
}
import java.io.*;
public class ISC03Q1
{
public static void main() throws IOException
{
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(isr);
String a,b=””;
int l,k=0,i,s;
System.out.print(“Enter string : “);
a=in.readLine();
System.out.print(“Enter shift : “);
s=Integer.parseInt(in.readLine());
a=a.toUpperCase();
l=a.length();
if((s<0 || s>26))
{
System.out.print(“invalid shift”);
return;
}
for(i=0;i<l;i++)
{
if(a.charAt(i)>=65 && a.charAt(i)<=90)
{
if(a.charAt(i)>(91-s))
b=b+(char)(a.charAt(i)+s-27);
else
b=b+(char)(a.charAt(i)+s-1);
}
}
System.out.print(“Output : “);
for(i=0;i<b.length();i++)
{
if(b.charAt(i)==’Q’ && b.charAt(i+1)==’Q’)
{
System.out.print(” “);
i++;
}
else
System.out.print(b.charAt(i));
}
}
}
Question 2.
Give a time in numbers we can convert it into words. For example5:00 five o’ clock
5:10 ten minutes past five
5:15 QUARTER PAST FIVE
5:30 HALF PAST FIVE
5:40 twenty minutes to six
5:45 quarter to six
5:47 thirteen minutes to sixWrite a program which first inputs two integers, the first between 1 and 12 (both inclusive) and second between 0 and 59 (both inclusive) and then prints out the time they represent, in words. Your program should follow the format of the examples above.
SAMPLE DATA:
INPUT:
TIME: 3,0
OUTPUT: 3:00 three o’ clockINPUT:
TIME: 7,29
OUTPUT: 7:29 twenty nine minutes past sevenINPUT:
TIME: 6,34
OUTPUT: 6:34 twenty six minutes to sevenINPUT:
TIME: 12,1
OUTPUT: 12:01 one minute past twelveINPUT:
TIME: 12,45
OUTPUT: 12:45 quarter to oneINPUT:
TIME: 10,59
OUTPUT: 10:59 one minute to elevenINPUT:
TIME: 14:60
OUTPUT: incorrect inputTest your program for the data values given in the examples above and some random data.
#include<iostream.h>
#include<conio.h>
#include<process.h>
char a[19][10] ={ “one”, “two”,”three”, “four”, “five”,”six”,”seven”,
“eight”,”nine”,”ten”, “eleven”,”twelve”,”thirteen”
,”fourteen”,”fifteen”, “sixteen”,”seventeen”,”eightneen”,
“nineteen”};
void clock( int x ,int y, char b[6])
{
if( (y>=1 && y<=14) || ( y>15 && y<=19))
cout<<a[y-1]<<” minutes “<<b<<” “<<a[x-1];
else if (y==15)
cout<<” quarter “<<b<<” “<<a[x-1];
else if( y==20)
cout<<“twenty minutes “<<b<<” “<<a[x-1];
else if( y>20 && y<=29)
cout<<“twenty “<<a[y-21]<<” minutes “<<b<<” “<<a[x-1];
}
void main()
{
clrscr();
int x,y;
cout<<“Enter Time?”;
cin>>x>>y;
if(x>=13 || x<1)
{
cout<<“Invalid Input”;
getch();
exit(0);
}
if(y==0)
cout<<a[x-1]<<” o’clock”;
else if( y==30 )
cout<<“half past “<<a[x-1];
if( y>=0 && y<=30)
clock(x,y,”past”);
else if( y>30 && y<60)
{
if( x==12 )
x=0;
clock( x+1,60-y,”to”);
}
getch();
}
import java.io.*;
public class ISC03Q2
{
static String a[]={“one”,”two”, “three”,”four”,”five”,”six”,
“seven”,”eight”,”nine”, “ten”,”eleven”,”twelve”,
“thirteen”,”fourteen”,”fifteen”, “sixteen”,”seventeen”,
“eightneen”,”nineteen”};
public static void main() throws IOException
{
InputStreamReader isr=new InputStreamReader (System.in);
BufferedReader in=new BufferedReader(isr);
int x,y;
System.out.print(“Enter Time (HH,MM) “);
x=Integer.parseInt(in.readLine());
y=Integer.parseInt(in.readLine());
if(x>=13 || x<1)
{
System.out.print(“Invalid Input”);
return;
}
if(y==0)
System.out.print(a[x-1]+” o’clock”);
else if( y==30 )
System.out.print(“half past “+a[x-1]);
if( y>=0 && y<=30)
clock(x,y,”past”);
else if( y>30 && y<60)
{
if(x==12)
x=0;
clock(x+1,60-y,”to”);
}
}
private static void clock(int x ,int y, String b)
{
if( (y>=1 && y<=14) || ( y>15 && y<=19))
System.out.print (a[y-1]+” minutes “+b+” “+a[x-1]);
else if (y==15)
System.out.print (” quarter “+b+” “+a[x-1]);
else if(y==20)
System.out.print (“twenty minutes “+b+” “+a[x-1]);
else if( y>20 && y<=29)
System.out.print (“twenty “+a[y-21]+ ” minutes “+b+” “+a[x-1]);
}
}
Question 3.
Write a program to declare a square matrix A[][] of order N (N<20). Allow the user to input positive integers into this matrix. Perform the following tasks on the matrix:(i) Output the original matrix.
(ii) Find the SADDLE POINT for the matrix. A saddle point is an element of the matrix such that it is the minimum element for the row to which it belongs and the maximum element for the column to which it belongs. Saddle point for a given matrix is always unique. If the matrix has no saddle point, output the message “NO SADDLE POINT”.
(iii) Sort the elements along principal diagonal in ascending order using insertion sort technique. All other elements should remain unchanged.Test your program for the following data and some random data:
SAMPLE DATA:
INPUT: N = 4
MATRIX A [][] =
2  5  6  9
8  4  12 3
6  7  3  1
12 24 2 11 OUTPUT:
2  5  6  9
8  4 12  3
6  7  3  1
12 24 2 11
NO SADDLE POINTMATRIX AFTER SORTING THE PRINCIPAL DIAGONAL2  5  6  9
8  3  12 3
6  7  4  1
12 24 2 11INPUT: N=3MATRIX A[][]=
4 16 12
2 8  14
1 3  6

OUTPUT:

4 16 12
2 8  14
1 3  6
SADDLE POINT=4MATRIX AFTER SORTING THE PRINCIPAL DIAGONAL
4 16 12
2 6  14
1 3  8
#include <iostream.h>
#include <conio.h>
// display no saddle point but there is two saddle point in the array
// note: In this program we assign array data as a constant. So you can change that part according to your question.
void main()
{
clrscr();
int a[5][8]={3,8,7,6,5,4,3,7,
9,1,2,5,7,8,9,3,
2,4,5,4,2,6,4,5,
1,2,1,2,2,3,1,3,
5,8,9,6,4,7,6,8};
int i,j,k,flag,high,pos;
// To print the matrix //
for(i=0;i<5;i++)
{
for(j=0;j<8;j++)
cout<<a[i][j]<<” “;
cout<<endl;
}
cout<<endl;
// ****************** *********************
for(i=0;i<5;i++)
{
high=a[i][0]; flag=0;
for(j=0;j<8;j++)
{
if(a[i][j]>high)
{
high=a[i][j];
pos=j;
}
}
// cout<<“High : “<<high <<” at “<<pos<<endl;
for(j=0;j<5;j++)
{
if(a[j][pos]<high)
{
flag=1;
}
}
if(flag==0)
cout<< “Saddle Point at Row ” <<(i+1)<<” Column ” <<(pos+1)<<endl;
}
getche();
}
public class ISC03Q3
{
// display no saddle point but there is two saddle point in the array
public static void main()
{
int a[][]={{3,8,7,6,5,4,3,7},
{9,1,2,5,7,8,9,6},
{2,7,5,4,2,6,4,5},
{1,2,1,2,2,3,1,3},
{5,8,9,6,4,7,6,8}};
int i,j,k,flag,high,pos=0;
// To print the matrix //
for(i=0;i<5;i++)
{
for(j=0;j<8;j++)
System.out.print(a[i][j]+” “);
System.out.println();
}
System.out.println();
// ******************* ********************
for(i=0;i<5;i++)
{
high=a[i][0]; flag=0;
for(j=0;j<8;j++)
{
if(a[i][j]>high)
{
high=a[i][j];
pos=j;
}
}
for(j=0;j<5;j++)
{
if(a[j][pos]<high)
{
flag=1;
}
}
if(flag==0)
System.out.println (“Saddle Point at Row “+ (i+1)+” Column “+ (pos+1));
}
}
}

2004

Question 1.
Numbers have different representations depending on the bases on which they are expressed. For example in base 3, the number 12 is written as 110 (1×32 + 1×31 + 0x30), but in base 8 it is written as 14 (1×81 + 4×80).Consider, for example, the integers 12 and 5. Certainly these are not equal if base 10 is used for each. But suppose 12 was a base 3 number and 5 was a base 6 number then what happens, 12 base 3=1×31 + 2×30, or 5 base 6 or 5 base 10(5 in any base is equal to 5 base 10). So 12 and 5 can be equal if you select the right bases for each of them.Write a program to input two integers, X and Y, and calculate the smallest base for X and smallest base for Y (likely different from X) so that X and Y represent the same value. The base associated with X and Y will be between 1 and 20 (both inclusive). In representing these numbers the digits 0 to 0 have their usual decimal interpretations. The upper case alphabetic characters A through J represent digits 10 through 19 respectively.
Test your program for the following data and some random data:
SAMPLE DATA:
INPUT:
X=12, Y=5
OUTPUT:
12(base 3)=5(base 6)INPUT:
X=10, Y=A
OUTPUT:
10(base 10)=A(base 11)INPUT:
X=12, Y=34
OUTPUT:
12(base 17)=34(base 5)INPUT:
X=123, Y=456
OUTPUT:
1 2 3 is not equal to 456 in any base between 2 to 20INPUT:
X=42, Y=36
OUTPUT:
42(base 7)=36(base 8).
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
#include<math.h>
#include<ctype.h>
int check( char a[30], int b)
{
int t,i;
for( i=0;a[i]!=NULL;i++)
{
if (isdigit(a[i]))
t=a[i]-48;
else if( isalpha(a[i]))
t=a[i]-55;
if( t>=b )
return(0);
}
return(1);
}
int baseconv( char a[30], int b )
{
int i,t,l,c=0,s=0;
l=strlen(a);
for( i=l-1;i>=0;i–)
{
if (isdigit(a[i]))
t=a[i]-48;
else if( isalpha(a[i]))
t=a[i]-55;
s+=(t*pow(b,c));
c++;
}
return(s);
}
void main()
{
clrscr();
char a[30],b[30];
int i,j,f=0;
cout<<“Enter 1st code?”;
gets(a);
cout<<“Enter 2nd code?”;
gets(b);
for( i=1;i<=20;i++)
{
if( check(a,i)==1 )
{
for( j=1;j<=20;j++)
{
if( check(b,j)==1 )
{
if( baseconv( a,i)==baseconv(b,j))
{
cout<<a<< “(base “<<i<<“)=” <<b<<“(base “<<j<<“)”;
f=1;
break;
}
}
}
}
if( f==1)
break;
}
if( f==0 )
cout<<“No answer”;
getch();
}
import java.io.*;
public class ISC04Q1
{
private static int check(String a,int b)
{
int t=0,i;
for(i=0;i<a.length();i++)
{
if(a.charAt(i)>=48 && a.charAt(i)<=57)
t=a.charAt(i)-48;
else if(a.charAt(i)>=65 && a.charAt(i)<=90)
t=a.charAt(i)-55;
if(t>=b)
return(0);
}
return(1);
}
private static int baseconv(String a, int b )
{
int i,t=0,l,c=0,s=0;
l=a.length();
for(i=l-1;i>=0;i–)
{
if(a.charAt(i)>=48 && a.charAt(i)<=57)
t=a.charAt(i)-48;
else if(a.charAt(i)>=65 && a.charAt(i)<=90)
t=a.charAt(i)-55;
s+=(t*(int)Math.pow(b,c));
c++;
}
return(s);
}
public static void main() throws IOException
{
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(isr);
String a,b;
int i,j,f=0;
System.out.print(“Enter 1st code?”);
a=in.readLine();
System.out.print(“Enter 2nd code?”);
b=in.readLine();
for(i=1;i<=20;i++)
{
if(check(a,i)==1 )
{
for(j=1;j<=20;j++)
{
if( check(b,j)==1 )
{
if( baseconv(a,i)==baseconv(b,j))
{
System.out.print (a+”(base “+i+”)=” +b+”(base “+j+”)”);
f=1;
break;
}
}
}
}
if( f==1)
break;
}
if( f==0 )
System.out.print(“No answer”);
}
}
Question 2.
The computer department of the Agency of International Espionage is trying to decode intercepted messages. The agency’s spies have determined that the enemy encodes messages by first converting all characters to their ASCII values and then reversing the string.For example, consider A_z(the underscope is just to highlight the space). The ASCII values of A, <space>, z are 65, 32, 122 respectively. Concatenate them to get 6532122, then reverse this to get 2212356 as the coded message.Write a program which reads a coded message and decodes it. The coded message will not exceed 200 characters. It will contain only alphabets (A….Z, and a…..z) and spaces. ASCII values of A….Z are 65…..90 and those of a…..z are 97…122. Test your program for the following data and some random data.
SAMPLE DATA:
INPUT:
Encoded Message:
231217986231019950187 2379231018117927
OUTPUT:
THE DECODED MESSAGE: Have a Nice Day
INPUT:
Encoded Message
2351101150178235111217 9911801562340161171141148
OUTPUT:
THE DECODED MESSAGE: Truth Always Wins
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
clrscr();
int convert(char);
cout<<“Enter Code : “;
int l,a=0,i=0;
char code[200], m[100],c[200],x[50];
gets(code);
strcpy(c,strrev(code));
l=strlen(c);
for(i=0;i<l;i++)
{
if(c[i]!=’1′)
{
x[a]=(convert(c[i])*10 +convert(c[i+1]));
i++;
a++;
}
else
{
x[a]=convert(c[i])*100 +convert(c[i+1])*10 +convert(c[i+2]);
a++;
i+=2;
}
}
for(i=0;i<a;i++)
{
cout<<(char)(x[i]);
}
}
int convert(char d)
{
return((int)(d)-48);
}
public class ISC04Q2
{
private static int convert(char d)
{
return((int)(d)-48);
}
public static void main()
{
int l,a=0,i=0;
String code = “23121798623101995 01872379231018117927″ ;
String c=””,x=””;
for(i=0;i<code.length();i++)
c=code.charAt(i)+c;
l=c.length();
for(i=0;i<l;i++)
{
if(c.charAt(i)!=’1′)
{
x+=(char) (convert(c.charAt(i))*10 +convert(c.charAt(i+1)));
i++;
}
else
{
x+=(char) (convert(c.charAt(i))*100 + convert(c.charAt(i+1))*10 + convert(c.charAt(i+2)));
i+=2;
}
}
System.out.println(x);
}
}
Questions 3.
The manager of a company wants to analyse the machine usage from the records to find the utilization of the machine. He wants to know how long each user used the machine. When the user wants to use the machine he must login to the machine and after finishing the work he must log off the machine.
Each log record consists of.
User identification number.
Login time and date.
Time consists of. Hours Minutes
Date consists of. Day MonthYou may assume all logins and logouts are in the same year and there are 100 users at the most. The time format is 24 hour machine hours and minutes.Design a program:
(a) To find the duration for which each user has logged. Output all records along with the duration in hours (format hours: minutes).
(b) Output the record of the user who logged for the longest duration. You may assume that no user will login for more than 48 hours.
Test your program for the following data values and some more random data.
SAMPLE DATA:
INPUT:
Number of users:3USER               LOGIN            LOGOUT
IDENTIFICATION       TIME & DATE      TIME & DATE
149             20:10 20-12       2:50 21-12
173             12:30 20-12      12:30 21-12
142             16:20 20-12      16:30 20-12

OUTPUT:

USER               LOGIN            LOGOUT       DURATION
IDENTIFICATION       TIME & DATE      TIME & DATE    HOURS:MINS
149             20:10 20-12       2:50 21-12       6:40
173             12:30 20-12      12:30 21-12      24:00
142             16:20 20-12      16:30 20-12      00:10THE USER WHO LOGGED IN FOR THE LONGEST DURATION:
173             12:30 20-12      12:30 21-12      24:00
#include<iostream.h>
#include<conio.h>
struct cafe
{
int usid;
int dd1,mm1,dd2,mm2;
int h1,m1,h2,m2;
int dur;
};
void main()
{
clrscr();
cafe c[100],temp;
int i,j,n,nd;
int mon[]={0,31,28,31,30,31, 30,31,31,30,31,30,31};
cout<<“Number of users : “;
cin>>n;
// accepting user details
for(i=0;i<n;i++)
{
cout<<“Enter user ID : “;
cin>>c[i].usid;
cout<<“Enter Login Time (HH MM) : “;
cin>>c[i].h1>>c[i].m1;
cout<<“Enter Date (DD MM) : “;
cin>>c[i].dd1>>c[i].mm1;
cout<<“enter Logout Time (HH MM) : “;
cin>>c[i].h2>>c[i].m2;
cout<<“Enter Date (DD MM) : “;
cin>>c[i].dd2>>c[i].mm2;
}
// calculating duration for each users
int wh=0,wm=0;
int time1,time2;
for(i=0;i<n;i++)
{
nd=0;
while(c[i].dd1!=c[i].dd2 || c[i].mm1!=c[i].mm2)
{
c[i].dd1++;
nd++;
if(c[i].dd1>mon[c[i].mm1])
{
c[i].dd1=1;
c[i].mm1++;
}
}
time1=c[i].h1*60+c[i].m1;
time2=((nd*24)+c[i].h2)*60+c[i].m2;
c[i].dur=time2-time1;
}
// display users details with duration
cout<<” USER “<<“\t”<<” LOGIN “<<“\t”<<” LOGOUT “<<“\t”
<<“DURATION”<<endl;
cout<<“Identification”<<“\t” <<“Time & Date”<<“\t” <<“Time & Date”<<“\t” <<“Hours:Min”<<endl;
for(i=0;i<n;i++)
{
wh=c[i].dur/60;
wm=c[i].dur%60;
cout<<c[i].usid<<“\t\t”<<c[i].h1 <<“:”<<c[i].m1<<“\t”
<<c[i].dd1<<“-“<<c[i].mm1;
cout<<“\t”<<c[i].h2<<“:” <<c[i].m2<<“\t” <<c[i].dd2 <<“-“<<c[i].mm2;
cout<<“\t”<<wh<<“:”<<wm<<endl;
}
// sorting structure duration wise
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(c[j].dur<c[j+1].dur)
{
temp=c[j];
c[j]=c[j+1];
c[j+1]=temp;
}
}
}
// display longest duration user
cout<<“\n\nTHE USER WHO LOGIN FOR THE LONGEST DURATION”<<endl;
wh=c[0].dur/60;
wm=c[0].dur%60;
cout<<c[0].usid<<“\t\t” <<c[0].h1<<“:” <<c[0].m1<<“\t”
<<c[0].dd1<<“-“<<c[0].mm1;
cout<<“\t”<<c[0].h2<<“:” <<c[0].m2<<“\t”<<c[0].dd2 <<“-“<<c[0].mm2;
cout<<“\t”<<wh<<“:”<<wm<<endl;
getch();
}

 

Leave a Comment