TAB

One of the works done by our Robotics and Machine Learning division,
SELF-LEVELING QUADCOPTER
Arduino based Quadcopter.
Self-leveling is acheived by the aligning the quadcopter using the readings from the gryo as well as the accelerometer.
A four channel RC transmitter is used to control the movement of the quadcopter when in flight. Kindly subscribe to our YouTube Channel and stay tuned.

Thursday 28 January 2016

PRACTICE PROBLEM 2 : (24-1 to 27-1) : Local Machines


It can be solved in a more efficient manner by using a struct to store several pairs of ip and corresponding name of the machine.However i have simply used a 2d array for ip and name.
I would encourage people to use struct.

 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

#include<stdio.h>
#include<string.h>
void main()
{
int i,j,t;
scanf("%d",&t);
char name[t][100];
char ip[t][100];
for(i=0;i<t;i++)
{
scanf("%s",ip[i]);
scanf("%s",name[i]);
}/*
for(i=0;i<t;i++)
{
printf("%s\n",ip[i]);
printf("%s",name[i]);
}*/
for(i=0;i<t;i++)
{
        int found=0;
        int c=0;
        char search[10];
        int k=0;
        while(c<2)
        {
            char temp[100];
            strcpy(temp,ip[i]);
            if(temp[k]=='.')
            {c++;}
            search[k]=temp[k];
            k++;
        }
        for(j=i+1;j<t;j++)
        {
            char search2[k];
            strncpy(search2,ip[j],k);
            if(strcmp(search,search2)==0)
            {
                printf("Machines %s and %s are on the same local network\n",name[i],name[j]);
            }

        }
    }
    }

15 comments:

  1. please explain the second half of the solution

    ReplyDelete
    Replies
    1. I take all the other elements in the ip array (one-by-one counter=j),extract the first k char(strncpy) and compare it.
      Understood?

      Delete
    2. no please write the pseudocode

      Delete
    3. For each element in ip[](counter=i)
      Extract first two segments of the ip( stored in search,length k)
      For each element in ip[](i+1 onwards,counter j)
      Extract k(length of search) characters of ip(search2) and compare with search
      Print RESULT
      END FOR
      END FOR
      Hope this is better!

      Delete
    4. For each element in ip[](counter=i)
      Extract first two segments of the ip( stored in search,length k)
      For each element in ip[](i+1 onwards,counter j)
      Extract k(length of search) characters of ip(search2) and compare with search
      Print RESULT
      END FOR
      END FOR
      Hope this is better!

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Yes, but you assume that the first two segments take 5 characters which may not satisfy all test cases. Consider 0.0.1.2 and 0.0.5.4, ur code will not show that they are local machines but they actually are.

    ReplyDelete
  4. #include
    #include
    #include
    main()
    {
    int n,i,j,l,c;
    char ip[50][50],name[50][50],a[50][50],b[50][50];
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
    fflush(stdin);
    scanf("%s",ip[i]);
    scanf("%s",name[i]);
    }
    for(i=0;i<n;i++)
    {
    l=strlen(ip[i]);
    for(j=0;j<l;j++)
    {
    if(ip[i][j]!='.')
    {
    a[i][j]=ip[i][j];
    }
    else
    {
    c=j;
    break;
    }
    }
    for(j=(c+1);j<l;j++)
    {
    if(ip[i][j]!='.')
    b[i][j-c-1]=ip[i][j];
    else
    break;
    }
    }
    for(i=0;i<(n-1);i++)
    {
    for(j=(i+1);j<n;j++)
    {
    if(strcmp(a[i],a[j])==0 && strcmp(b[i],b[j])==0)
    printf("Machines %s and %s are on the same local network",name[i],name[j]);
    else
    continue;
    }
    }
    }

    this is the code made by me... on skillrack its showing private (hidden) cases failed.. please rectify the problem.. thank you

    ReplyDelete
    Replies
    1. i tried this code too..its still showing private hidden cases failed.. please help.. thank you

      #include
      #include
      main()
      {
      int n,i,j,l,c;
      char ip[50][50],name[50][50],a[50][50],b[50][50];
      scanf("%d",&n);
      for(i=0;i<n;i++)
      {
      scanf("%s\n",ip[i]);
      gets(name[i]);
      }
      for(i=0;i<n;i++)
      {
      l=strlen(ip[i]);
      for(j=0;j<l;j++)
      {
      if(ip[i][j]!='.')
      {
      a[i][j]=ip[i][j];
      }
      else
      {
      c=j;
      break;
      }
      }
      for(j=(c+1);j<l;j++)
      {
      if(ip[i][j]!='.')
      b[i][j-c-1]=ip[i][j];
      else
      break;
      }
      }
      for(i=0;i<(n-1);i++)
      {
      for(j=(i+1);j<n;j++)
      {
      if(strcmp(a[i],a[j])==0 && strcmp(b[i],b[j])==0)
      printf("Machines %s and %s are on the same local network\n",name[i],name[j]);
      else
      continue;
      }
      }
      }

      Delete
    2. gets will help in reading names having spaces in between them.. it wll read string with space

      Delete
    3. There is some error i guess in that regard. Skillrack is not accepting any solution,it is always displaying "HIDDEN CASE".

      Delete
    4. Your code is correct according to me.

      Delete
  5. This will run ....try it !!!

    #include
    int cmp(char x[20],char y[20])
    {
    int i=0,j=0,m1,m2,n1,n2;
    m1=m2=n1=n2=0;
    for(i=0;x[i]!='.';i++)
    m1=m1*10+((int)x[i]-48);
    for(++i;x[i]!='.';i++)
    m2=m2*10+((int)x[i]-48);
    for(j=0;y[j]!='.';j++)
    n1=n1*10+((int)y[j]-48);
    for(++j;y[j]!='.';j++)
    n2=n2*10+((int)y[j]-48);
    if(m1==n1 && m2==n2)
    return(1);
    else
    return(0);
    }
    void main()
    {
    char ip[5][20],nm[5][20];
    int i,n,j;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
    scanf("%s",ip[i]);
    scanf("%s",nm[i]);
    }
    for(i=0;i<n-1;i++)
    {
    for(j=i+1;j<n;j++)
    {
    if(cmp(ip[i],ip[j])==1)
    printf("Machines %s and %s are on the same local network",nm[i],nm[j]);
    }
    }
    }

    ReplyDelete
    Replies
    1. Yes, even the code i posted is now working.
      Skillrack has made a few changes.
      Some test cases have been removed.

      Delete