Identify machines in same local network (Id-2436)

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

3 comments:

  1. Code does not work
    Segmentation error

    ReplyDelete
    Replies
    1. It does work...this is the same code that i used for submitting...and i double checked the code and its working...

      Delete
  2. code runs! tested on vpropel, thankssss

    ReplyDelete

Bonus Practice Problems(GMT To IST)

IST (Indian Standard Time) is 5 hours 30 minutes ahead of GMT(Greenwich Mean Time). Develop an algorithm and write the Python code to find...