PRACTICE PROBLEM 8

PRACTICE PROBLEM 8

Verification of 'L' shaped arrangement of Coins on Game Board (Id-2435)

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
Input:
the number of rows and columns and the 'r x c' board
Processing:
#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;
}

Output:
display yes or no
Program:
#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;
}

Pseudocode:
Step1. get the number of rows and number of columns
Step2. get the r x c matrix
Step3.  repeat till i goes from 0 to r-1 and j goes from 1 to n-2
Step3.1 check if the 2x2 L shape is formed if yes then assign f as 1 and display yes and break out from the loop else continue
Step4. if f has a value 0 then display no
Step5.End
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)


Input:
The number n and the addresses of the computers
Processing:
#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;
}
Output:
display the machines that are on the same local network
Program:
#include<stdio.h>
#include<string.h>
int main()
{
    int n,p,i,j;
    char s[20][50];
    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;
}

Pseuodocode:
Step1. get the number of ip addresses n
Step2. scan each of the n ip addresses and store them in a two dimensional array
Step3. run through each of the the ip addresses change them as soon as you reach the second '.' and change it to '\0'.
Step4. now compare each of the ip addresses and display the machines that are on the same network.
Step5.End
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
Ouput Format
Count of two digit prime numbers that can be formed when adjacent digits are taken
Input:
the number
Processing:
#include<stdio.h>
int isprime(int n)
{     
    for (int i = 2; i < n; i++)
    if (n%i == 0)
        return 0;
    return 1;
    
}

void main()
{
    char n[10];
    int i = 0;
    char num[3];
    int number = 0;
    int count =  0;
    while (n[i + 1] != '\0')
    {
        num[0] = n[i];
        num[1] = n[i + 1];
        num[2] = '\0';
        ++i;
        number = (num[1] - '0') + (num[0] - '0')*10;
        if (isprime(number))
            ++count;
    }
}
Program:
#include<stdio.h>
int isprime(int n)
{   
    for (int i = 2; i < n; i++)
    if (n%i == 0)
        return 0;
    return 1;
 
}

void main()
{
    char n[10];
    scanf("%s", &n);
    int i = 0;
    char num[3];
    int number = 0;
    int count =  0;
    while (n[i + 1] != '\0')
    {
        num[0] = n[i];
        num[1] = n[i + 1];
        num[2] = '\0';
        ++i;
        number = (num[1] - '0') + (num[0] - '0')*10;
        if (isprime(number))
            ++count;
    }
    printf("%d",count);
}
Pseudocode:
Step1. Get the number n
Step2. define a function isprime with one parameter n to check whether n is a prime number or not if yes return 1 else return 0
Step3.divide the number into numbers of two digits and check each of these numbers using isprime function and display the number if the number is prime

Step4.End
A bakery prepares bread by following a sequence of operations, as listed in the table. All the operations should be performed to make a bread. The bakery prepares two types bread : White and Sweet bread. It prepares bread in two different sizes : single and double size. Further, breads are baked either by  a semi-automation  process ( that is, a few operations will be done by using machines and then the remaining operations done manually) or by an automation process (using machines for all the operations).
The following table details the time taken by the machine in performing the different operations involved in baking a single size  bread using machines. For Eg., the time taken to make a single size, white bread using machine, is 2 hrs 58 minutes and 2 seconds.  The time taken by the machine to prepare a double size bread , will be  50%  more of the time taken to prepare the single size bread using machines. For eg., if ‘primary kneading’ for a single size white bread requires 15 minutes, then, for the double size white bread, ‘primary kneading’ requires 22.5 minutes (22 minutes 30 seconds)
 If baking is done by semi-automation process, use the machine till the ‘loaf-shaping cycle’ . Given the type of bread, size and type of baking, write an algorithm and the subsequent ‘C’ code to compute and display the time taken by the machine to prepare the bread.
Operation
White Bread
Sweet Bread
Primary Kneading
15 mins
20 mins
Primary rising
60 mins
60 mins
Secondary Kneading
18 mins
33 mins
Secondary rising
20 mins
30 mins
Loaf shaping
2 seconds
2 seconds
Final rising
75 mins
75 mins
Baking
45 mins
35 mins
Cooling
30 mins
30 mins
Input Format
First line contains the bread type (‘w’ or  ‘s’)
Next line contains the size of the loaf (‘1’ for single and ‘2’ for double)
Next line contains the type of baking (‘s’ for semi-automatic and ‘a’ for automatic)
Output Format
Print the duration in seconds, during which the machine is used .
Input:
First line contains the bread type (‘w’ or  ‘s’)
Next line contains the size of the loaf (‘1’ for single and ‘2’ for double)
Next line contains the type of baking (‘s’ for semi-automatic and ‘a’ for automatic)

Processing

#include<stdio.h>
int main()
{
    char type,size,bt;
    int t;
   switch(type)
   {
        case('w'):
        {
           t=263*60+2;
           if(bt=='s')
           t=113*60+2;
           if(size=='2')
            t=t*1.5;
            break;
        }
       case('s'):
       {
            t=283*60+2;
           if(bt=='s')
           t=143*60+2;
           if(size=='2')
           t=t*1.5;
       }
    }
   return 0;
}
Output:
The duration in seconds, during which the machine is used .
Program:
 #include<stdio.h>
int main()
{
    char type,size,bt;
    int t;
    scanf("%c",&type);
    scanf("%c",&size);
    scanf("%c",&bt);
    switch(type)
    {
        case('w'):
        {
            t=263*60+2;
            if(bt=='s')
            t=113*60+2;
            if(size=='2')
            t=t*1.5;
            break;
        }
        case('s'):
        {
            t=283*60+2;
            if(bt=='s')
            t=143*60+2;
            if(size=='2')
            t=t*1.5;
        }
    }
    printf("%d",t);
    return 0;
}
Pseudocode:
Step1: Enter the data
Step2:Make a switch case depending on type of bread
Step3:Calculate time depending on automatic or semi-automatic machine and size of bread
Step4:Print the time in seconds
Step5:End


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
Input:
the number of elements
the n elements
Processing:
void main()
{
int n;
int numbers[n];
for(int i =0;i<n-1;i++)
    {
        int sum = 0;
        for(int j = i+1;j<n;j++)
            sum+=numbers[j];
        if ((float)numbers[i]>(sum/(n-i-1)))
            printf("%d\n",numbers[i]);
    }
printf("%d",numbers[n-1]);
}
Output:
Display the excess numbers

Program:
#include<stdio.h>
void main()
{
    int n;
    scanf("%d", &n);
    int numbers[n];
    for (int i = 0; i<n; i++)
        scanf("%d", &numbers[i]);
    for (int i = 0; i<n - 1; i++)
    {
        int sum = 0;
        for (int j = i + 1; j<n; j++)
            sum += numbers[j];
        if ((float)(numbers[i]) > (sum / (n - i - 1)))
            printf("%d\n", numbers[i]);
    }
    printf("%d",numbers[n-1]);

}

Pseudocode:
Step1. get the number n
Step2. declare numbers as an integer array to store n numbers
Step3. get the n numbers using a for loop and storing the numbers in their respective positions
Step4. repeat till i goes from 0 to n-1
Step4.1 declare sum as an integer
Step4.2 repeat till j goes from i+1 to n-1
Step4.2.1 assign sum as the sum of sum and numbers[j]
Step4.3 if the numbers[i] is greater than the average of the numbers to the right of it then display the number else continue
Step5. display the last number
Step6. End

Comments

Popular posts from this blog

MATLAB WEEK-2

Practice Problem 6-Isomorphic Numbers