QUIZ –1
[A]
Switch(c){
case ‘0’:case’1’:case’2’: ndigit[c- ‘0’]++;
case ‘ ’:
case ‘\t’:nwhite++;
default: for(int I=0;I<n;I++) a=a+n;
}
suppose for each ‘case’,
probability=p.
so, 1st line = 3p*3.
2nd line=p*1 or p*0
// consider 1 in case of ‘skip’ operation consideration.
3rd line= p*1
4th line=
(1-5p)*(n-2)
so the complexity of the code is
3p*3+p*1(or p*0)+p*1+ (1-5p)*(n-2)
Course ID |
Course Name |
Course Teacher |
234 |
Database |
Mr. A |
|
|
|
Registered – Table
Student ID |
Student Name |
Course ID |
12 |
Mr. B |
234 |
|
|
|
Write appropriate C-code to find out the ‘Course Name’ that Mr. B (Student Name) registered. (Note : A student can register more than 1 course or no course at all. You should consider this while code)
for(I=0;I< n;I++)
{
if(Registered[I].StudentName==”Mr. B”) // or you can use strcmp()
function.
{
for(j=0;j<n;j++)
{
if (Curse[j].CourseID==Registered[i].CourseID)
cout<<Course[j].CourseName;
}
}
}
[B]
1. Calculate the complexity of the following code:
Switch(month)
{
case’1’: case’3’: case’5’: case’7’:case’8’:case’10’:case’12’: days =31;
case’4’: case’6’:case’9’:case’11’: days=30;
case ‘2’: { if (year%4==0) days =29;
else days =28;
}
}
Suppose
for each ‘case’ probability =p.
So, line 1’s cost =7p(1)
Line 2’s cost =4p(1)
Line 4 and Line 3’s cost = (1-11p)(4)
So cost= 7p+4p+(1-11p)*4
2. ‘Course’- table
Course ID |
Course Name |
Course Teacher |
234 |
Database |
Mr. A |
|
|
|
Registered – Table
Student ID |
Student Name |
Course ID |
12 |
Mr. B |
234 |
|
|
|
Write appropriate C-code to find out the ‘Student Name’ who are registered for 2nd year course(CourseID started with’2**” .(note: Astudent can registered more than 1 course or no course at all. You should consider this while writing code)
for(I=0;I<n;I++) {
if(Registered[I].CourseID>200
&& Registered[i].CourseID<300)
cout<<Registered[I].StudentName;
}
[C]
calculate the complexity of the following code:
for(I=1;I<n;I=2*i){
for(k=1;k<n;k=4*k){
for(j=1;j<n;j=5*j){
if(b>0) a=1;
else a=2;
}
}
}
(1+log2(n))*(1+log4(n))*(1+log5(n))*3
Course ID |
Course Name |
Course Teacher |
234 |
Database |
Mr. A |
|
|
|
Registered – Table
Student ID |
Student Name |
Course ID |
12 |
Mr. B |
234 |
|
|
|
Write appropriate C-code to find out the ‘Student Name’ who are registered for 2nd year course(CourseID started with’2**” .(note: Astudent can registered more than 1 course or no course at all. You should consider this while writing code)
for(I=0;I<n;I++) {
if(Registered[I].CourseID>200
&& Registered[i].CourseID<300)
cout<<Registered[I].StudentName;
}
[D]
1. Calculate the complexity of the following code:
for(I=1;I<n;I=2*I){
for(k=1;k<n;k=4*k){
if (b%3>0) a=1;
else a=2;
}
}
(1+log2(n))*(1+log4(n))*4
2. ‘Course’- table
Course ID |
Course Name |
Course Teacher |
234 |
Database |
Mr. A |
|
|
|
Registered – Table
Student ID |
Student Name |
Course ID |
12 |
Mr. B |
234 |
|
|
|
Write appropriate C-code to find out Student Name who are registered for CourseID=220.
for(I=0; I <n; I ++){
if(Registered[I ].CourseID==220)
cout << Registered[I ].StudentName;
}