Skillrack Practice Problems 8

Solutions for Skillrack Code Test CSE1001 Practice Problems 8

3-Nov-2016 00:00 to  12-Nov-2016 00:00

Total time : 500 mins

Challenges : 5

Question 1 (Identify machines in same local networks)

Numeric addresses for computers on the international network Internet are composed of four parts, separated by periods, of the form   xx.yy.zz.mm    where  xx ,  yy ,  zz , and  mm  are positive integers. Locally, computers are also known by a nicknames.

Sample Data

IP address

111.22.3.44

555.66.7.88

111.22.5.66

0.0.0.0

A pair of computers are said to be in same locality when the first two components of the addresses are same. Given the IP addresses of ‘n’  computers, write an algorithm and the subsequent ‘C’ program to identify the pair of computers from the same locality.

In this example, the message to be displayed will be ‘Machines 1 and 3 are on the same local network’. Index of the machines is printed in the message. Index of the machines start from index 1.

Input Format

Number of computers

IP address of the computer1 as a String

IP address of the computer2 as a String

….

Output Format

For each pair of machines in the same local network, print:

Machines A and B are on the same local network (A and B are index of the machines)

Solution

#include < stdio.h >
#include < string.h >
int main()
{
    int n,p,i,j;
    char s[20][20];
    scanf("%d",&n);
    for(i=0;i < n;i++)
    {
        scanf("%s",s[i]);
        p=0;
        j=0;
        while(p!=2)
        {
            j++;
            if(s[i][j]=='.')
            p++;
        }
        s[i][j]='\0';
    }
    for(i=0;i < n-1;i++)
    for(j=i+1;j < n;j++)
    if(strcmp(s[i],s[j])==0)
    printf("Machines %d and %d are on the same local network",i+1,j+1);
    return(0);
}

Input

INPUT:
n - number of entries
for(i=0;i < n;i++)
    {
        scanf("%s",s[i]);
        p=0;
        j=0;
        while(p!=2)
        {
            j++;
            if(s[i][j]=='.')
            p++;
        }
        s[i][j]='\0';
    }

Output

OUTPUT:

if(strcmp(s[i],s[j])==0)
    printf("Machines %d and %d are on the same local network",i+1,j+1);

Processing

for(i=0;i < n-1;i++)
    for(j=i+1;j < n;j++)
    if(strcmp(s[i],s[j])==0)

Pseudocode

BEGIN

Read n and array s
for(i=0;i < n-1;i++)
    for(j=i+1;j < n;j++)
    if(strcmp(s[i],s[j])==0)
    printf("Machines %d and %d are on the same local network",i+1,j+1);

END

Question 2 (Excess numbers)

Given ‘n’ integers, write an algorithm and the subsequent ‘C’ program to print all Excess numbers in it. A number is said to an ‘Excess number’ if it is greater than the average of the elements in its right. Last number is always considered to be ‘Excess number’. For example, given seven numbers 23, 34, 12, 21, 14, 26, 33, then the numbers 34, 33 are excess numbers.

Input format

First line contains the number of elements, n

Next ‘n’ lines contain the elements

Output format

Print all excess numbers in the collection

Solution

#include < stdio.h >
int main()
{
    int n,i,j;
    int s=0;
    int a[20];
    scanf("%d",&n);
    for(i=0;i < n;i++)
    scanf("%d",&a[i]);
    for(i=0;i < n-1;i++)
    {
        s=0;
        for(j=i+1;j < n;j++)
        s+=a[j];
        s/=(n-i-1);
        if(a[i] > s)
        printf("%d\n",a[i]);
    }
    printf("%d",a[n-1]);
    return(0);
}

Input

INPUT:

n - number of elements
for(i=0;i < n;i++)
    scanf("%d",&a[i]);

Output

OUTPUT:

if(a[i] > s)
        printf("%d\n",a[i]);
printf("%d",a[n-1]);

Processing

for(i=0;i < n-1;i++)
    {
        s=0;
        for(j=i+1;j < n;j++)
        s+=a[j];
        s/=(n-i-1);
    }

Pseudocode

BEGIN

Read n and array a
for(i=0;i < n-1;i++)
    {
        s=0;
        for(j=i+1;j  s)
        printf("%d\n",a[i]);
    }
    printf("%d",a[n-1]);

END

Question 3 (Singular to plural)

A Software is developed for kids to give plural of a given word. Write an algorithm and the Subsequent ‘C’ program to read a noun and print their plurals on the basis of the following rules:

a.   If noun ends in “y”, remove the “y” and add “ies”.

b.   If noun ends in “s”, “ch”, or “sh”, add “es”.

c.   In all other cases, just add “s”.

Print each noun and its plural in lowercase letters with separated by a space.

Input format

Noun – A String

Output format

Plural of the noun

Solution

#include < string.h >
#include < stdio.h >
int main()
{
    char n[20];
    int i,j,l;
    scanf("%s",&n);
    l=strlen(n);
    if(n[l-1]=='y')
    {
        n[l-1]='i';
        n[l]='e';
        n[l+1]='s';
        n[l+2]='\0';
    }
    else if(n[l-1]=='s'||(n[l-2]=='c'&&n[l-1]=='h')||(n[l-2]=='s'&&n[l-1]=='h'))
    {
        n[l]='e';
        n[l+1]='s';
        n[l+2]='\0';
    }
    else
    {
        n[l]='s';
        n[l+1]='\0';
    }
    printf("%s",n);
    return(0);
}

Input

INPUT :

n - given string (word)

Output

OUTPUT:

printf("%s",n);

Processing

if(n[l-1]=='y')
    {
        n[l-1]='i';
        n[l]='e';
        n[l+1]='s';
        n[l+2]='\0';
    }
    else if(n[l-1]=='s'||(n[l-2]=='c'&&n[l-1]=='h')||(n[l-2]=='s'&&n[l-1]=='h'))
    {
        n[l]='e';
        n[l+1]='s';
        n[l+2]='\0';
    }
    else
    {
        n[l]='s';
        n[l+1]='\0';
    }

Pseudocode

BEGIN

Read n
if(n[l-1]=='y')
    {
        n[l-1]='i';
        n[l]='e';
        n[l+1]='s';
        n[l+2]='\0';
    }
    else if(n[l-1]=='s'||(n[l-2]=='c'&&n[l-1]=='h')||(n[l-2]=='s'&&n[l-1]=='h'))
    {
        n[l]='e';
        n[l+1]='s';
        n[l+2]='\0';
    }
    else
    {
        n[l]='s';
        n[l+1]='\0';
    }
printf("%s",n);

END

Question 4 (Count two digit prime numbers)

Given a number ‘n’, write an algorithm and the subsequent ‘C’ program to count the number of two digit prime numbers in it when adjacent digits are taken. For example, if the value of ‘n’ is 114 then the two digit numbers that can be formed by taking adjacent digits are 11 and 14. 11 is prime but 14 is not. Therefore print 1.

Input Format

A number

Output Format

Count of two digit prime numbers that can be formed when adjacent digits are taken

Solution

#include < stdio.h >
#include < string.h >
int prime(int n)
{
    int i;
    int flag=1;
    for(i=n-1;i > 1;i--)
    if(n%i==0)
    {
        flag=0;
        break;
    }
    if(flag==1)
    return(1);
    else
    return(0);
}
int main()
{
    int i,n;
    int c=0;
    char s[20];
    scanf("%s",s);
    for(i=0;i < strlen(s)-1;i++)
    {
        n=(10*(s[i]-'0'))+(s[i+1]-'0');
        c+=prime(n);
    }
    printf("%d",c);
    return(0);
}

Input

INPUT:

s - given number

Output

OUTPUT:

printf("%d",c);

Processing

int prime(int n)
{
    int i;
    int flag=1;
    for(i=n-1;i > 1;i--)
    if(n%i==0)
    {
        flag=0;
        break;
    }
    if(flag==1)
    return(1);
    else
    return(0);
}

Pseudocode

BEGIN

Read s
int prime(int n)
{
    int i;
    int flag=1;
    for(i=n-1;i > 1;i--)
    if(n%i==0)
    {
        flag=0;
        break;
    }
    if(flag==1)
    return(1);
    else
    return(0);
}
printf("%d",c);

END

Question 5 (Verification of ‘L’ shaped arrangement of coins on game board)

Consider a nxn board game with four types of coins red, green, blue and yellow. Given the state of the board with coins in all cells, develop an algorithm and write a C program to check if the same coins are placed in ‘L’ shape on the board. The number of cells in the vertical and horizontal line of ‘L’ shape should be same and should be greater than 1. Red coins are represented by ‘r’, blue coins are represented by ‘b’, green coins are represented by ‘g’ and yellow coins are represented by ‘y’.

For example, given the configuration of a 4 X 4 board with coins as shown below,

b r y r

r r y b

y r b b

b r r r

the program must print ‘Yes’. As there is a ‘L’ shape as shown in the figure below.

Input Format

Number of rows, r

Number of columns, c

Next ‘r x c’ lines contains the elements in the matrix

Elements in the matrix given row by row

Output Format

Print Yes or No

Solution

#include < stdio.h >
int main()
{
    int r,n,i,j;
    int f=0;
    char c[10][10];
    scanf("%d%d",&r,&n);
    for(i=0;i < r;i++)
    for(j=0;j < n;j++)
    scanf("%c",&c[i][j]);
    if(r > 2 && n > 2)
    {
        for(i=0;i < r-1;i++)
        for(j=0;j < n-2;j++)
        {
            if((c[i][j]==c[i+1][j])&&(c[i][j]==c[i+1][j+1])&&(c[i][j]==c[i+1][j+2]))
            {
                f=1;
                printf("Yes");
                break;
            }
        }
        if(f==0)
        printf("No");
    }
    else
    printf("No");
    return(0);
}

Input

INPUT:

r - number of rows
n - number of columns
c - matrix

Output

OUTPUT:

if(n > 2 &&r > 2)
{
  if(f==0)
  printf("No");
  else
  printf("Yes");
}
else
printf("No");

Processing

for(i=0;i < r-1;i++)
for(j=0;j < n-2;j++)
{
        if((c[i][j]==c[i+1][j])&&(c[i][j]==c[i+1][j+1])&&(c[i][j]==c[i+1][j+2]))
        {
            f=1;
            printf("Yes");
            break;
        }
}

Pseudocode

BEGIN

Read r,n and c
if(r > 2 && n > 2)
    {
        for(i=0;i < r-1;i++)
        for(j=0;j < n-2;j++)
        {
            if((c[i][j]==c[i+1][j])&&(c[i][j]==c[i+1][j+1])&&(c[i][j]==c[i+1][j+2]))
            {
                f=1;
                printf("Yes");
                break;
            }
        }
        if(f==0)
        printf("No");
    }
    else
    printf("No");

END

4 thoughts on “Skillrack Practice Problems 8

Leave a comment