Anda di halaman 1dari 3

Q2: (Part-1 )

Procedure: Frequency
Input: An array A[0..N-1] and an element k
Output: Returns the number of occurrences of the element k in the array A using sequential search.
Begin
Freq = 0
For i = 0 to N-1 do
If(A[i] = k) then
Freq = Freq + 1
EndIf
EndFor
Return Freq
End

Q2: (Part-2 )

Procedure: Frequency
Input: Asorted array A[0..N-1] and an element k
Output: Returns the number of occurrences of the element k in the array A using binary search.
Begin
Freq = 0
Left = 0, Right = N-1
While (Left <= Right)
Mid = (Left + Right)/2
If (A[Mid] = k) then
J = Mid
While(J >= 0 and A[J] = k)
Freq = Freq + 1
J=J-1
EndWhile
J = Mid + 1
While(J < N and A[J] = k)
Freq = Freq + 1
J=J+1
EndWhile
Else if (A[Mid] < k) then
Left = Mid + 1
Else
Right = Mid - 1
EndIf
EndWhile
Return Freq
End
Q3: Procedure: CheckDiagonal
Input: A two dimensional array of binary values, row index I and column index J of a cell.
Output: Returns whether Queen at the cell (I, J) is attached by any other Queen in the diagonal.
Begin //Checking top left diagonal
I1 = I – 1
J1 = J – 1
While (I1 >= 0 and J1 >= 0)
If(Q[I1][J1] = 1) then
Return 0
EndIf
I1 = I1 - 1
J1 = J1 – 1
EndWhile
//Checking bottom right diagonal
I1 = I + 1
J1 = J + 1
While (I1 <= 7 and J1 <= 7)
If(Q[I1][J1] = 1) then
Return 0
EndIf
I1 = I1 + 1
J1 = J1 + 1
EndWhile
//Checking top right diagonal
I1 = I – 1
J1 = J + 1
While (I1 >= 0 and J1 <= 7)
If(Q[I1][J1] = 1) then
Return 0
EndIf
I1 = I1 - 1
J1 = J1 + 1
EndWhile
//Checking bottom left diagonal
I1 = I + 1
J1 = J – 1
While (I1 <= 7 and J1 >= 0)
If(Q[I1][J1] = 1) then
Return 0
EndIf
I1 = I1 + 1
J1 = J1 – 1
EndWhile
Return 1
End
Procedure: CheckRow
Input: A two dimensional array of binary values, row index I and column index J of a cell.
Output: Returns whether Queen at the cell (I, J) is attached by any other Queen in the same row.
Begin
For K = 0 to 7
If(K != J and Q[I][K] = 1) then
Return 0
EndIf
End For
Return 1
End

Procedure: CheckColumn
Input: A two dimensional array of binary values, row index I and column index J of a cell.
Output: Returns whether Queen at the cell (I, J) is attached by any other Queen in the same column.
Begin
For K = 0 to 7
If(K != I and Q[K][J] = 1) then
Return 0
EndIf
End For
Return 1
End

Q4:
void printAllStrings(char s[], int f)
{
int l = strlen(s);
if(f == l)
{
printf("%s\n", s);
return;
}
if(s[f] == 'x')
{
s[f] = '0';
printAllStrings(s, f + 1);
s[f] = '1';
printAllStrings(s, f + 1);
s[f] = 'x';
}
else
{
printAllStrings(s, f + 1);
}
}

Anda mungkin juga menyukai