0% found this document useful (0 votes)
24 views4 pages

PHD Screening Test Lab

C-PhD Screening Test Lab

Uploaded by

RAMU
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views4 pages

PHD Screening Test Lab

C-PhD Screening Test Lab

Uploaded by

RAMU
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

PhD Screening Test

Part I: Laboratory

Q1. Take a 2D array as an input and print the number of rows which have exactly 3 zeros and 3 ones in them.

The input will be provided by first giving the number of rows (n) followed by the number of columns (m) in a single
line. The next m lines will contain n integers separated by a space.

The output should be a single number only, that prints the answer.

The maximum number of rows or columns can be 100.

Sample Input
58
20110012
22111000
21112111
21010102
11100002

Sample Output
3

Explanation
The following rows contain exactly 3 zeros and exactly 3 ones:
20110012
22111000
21010102

Sample Program to take input and print output


/*
// Sample code to perform I/O:*/
#include <stdio.h>

int main(){
int m; //number of rows
int n; //number of columns
int X[100][100]; //2D array declaration
scanf("%d %d", &m,&n); // Reading m and n from STDIN
int i,j; // To iterate over the 2D array to take inputs
for(i=0;i<m;i++) // Iterate over rows
{
for(j=0;j<n;j++) // Iterate over columns
scanf("%d",&X[i][j]); // Reading input for a cell
}
int answer; //store the answer here

/* Write your code to calculate the answer here without doing input or output because that is already done over
here. You may change any segment of the code above or below. */

printf("%d\n", answer); // Writing output 'sum' to STDOUT


}

// Warning: Printing unwanted or ill-formatted data to output will cause the test cases to fail
Q2. Take a 1D array as an input. Print the distance between the index of the smallest element and the index
of the largest element.

The input will be provided by first giving the number of elements in the array (n). The next line contains n integers
separated by a space.

The output should be a single number only, that prints the answer.

The maximum number of elements can be 100.

Sample Input
5
21354

Sample Output
2

Explanation
Largest element 5 has an index 3
Smallest element 1 has an index 1
Distance between smallest and largest element index = abs(3-1)=2, where abs() is the absolute value function.

Sample Program to perform Input and Output


/*
// Sample code to perform I/O:*/
#include <stdio.h>

int main(){
int n; //number of elements
int X[100]; //1D array declaration
scanf("%d", &n); // Reading m and n from STDIN
int i; // To iterate over the 1D array to take inputs
for(i=0;i<n;i++) // Iterate over array
{
scanf("%d",&X[i]); // Reading input for a cell
}
int answer; //store the answer here

/* Write your code to calculate the answer here without doing input or output because that is already done over
here. You may change any segment of the code above or below. */

printf("%d\n", answer); // Writing output 'sum' to STDOUT


}
Q3. Let “i j” denote the grid at row i (counting from 0) and column j (counting from 0) in a 2D array. The ‘a’
value of a grid is the number of consecutive 0s immediately above the grid. The ‘b’ value of a grid is the
number of consecutive 0s immediately below the grid. The ‘c’ value of a grid is the number of consecutive 0s
immediately on the left of the grid. The ‘d’ value of a grid is the number of consecutive 0s immediately on the
right of the grid.

Consider the 2D array as follows and the grid “2 5” which is highlighted. There are 2 zeros above it, so the a value is
2. There are no zeros below it, so the b value is 0. There are 2 zeros of the left, so the c value is 2 (note that the third
zero at “2 1” is not considered as it is not consecutive and occurs after a gap of 1). There is one zero on the right, so
the d value is 1. All zeros used in counting are highlighted.

1 1 1 1 1 0 1
1 1 1 0 1 0 1
1 0 1 0 0 0 0
1 0 1 0 0 1 1
1 1 1 1 1 1 1

Given a 2D array and the a-value, b-value, c-value and d-value; find the grid “i j” whose a, b, c and d values match
with the supplied input.

The input will be provided by first giving the number of rows (n) followed by the number of columns (m) in a single
line. The next m lines will contain n integers separated by a space. The next line prints 4 integers, the desired a-value,
b-value, c-value and d-value.

The output should be two numbers only “i j” of the selected grid.

The maximum number of rows or columns can be 100.

Sample Input
57
1111101
1110101
1010000
1010011
1111111
2021

Sample Output
25
Sample Program to take input and print output
/*
// Sample code to perform I/O:*/
#include <stdio.h>

int main(){
int m; //number of rows
int n; //number of columns
int X[100][100]; //2D array declaration
scanf("%d %d", &m,&n); // Reading m and n from STDIN
int i,j; // To iterate over the 2D array to take inputs
for(i=0;i<m;i++) // Iterate over rows
{
for(j=0;j<n;j++) // Iterate over columns
scanf("%d",&X[i][j]); // Reading input for a cell
}
int a,b,c,d; //desired values
scanf("%d %d %d %d", &a,&b,&c,&d); // Reading m and n from STDIN
int answerX, answerY; //store the answer here

/* Write your code to calculate the answer here without doing input or output because that is already done over
here. You may change any segment of the code above or below. */

printf("%d %d\n", answerX, answerY); // Writing output 'sum' to STDOUT


}

// Warning: Printing unwanted or ill-formatted data to output will cause the test cases to fail

You might also like