1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Bubble sorting IP address

Discussion in 'C' started by czm, Apr 24, 2008.

  1. czm

    czm New Member

    Joined:
    Apr 24, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Am trying to a program that bubble sorts IP address. the IP address line has some string after which should remain in the same line after the bubble sort.

    Code:
    ypedef struct ipaddress
    
    {
    
        short int w, x, y, z;
    
    } ipaddress;
    
    typedef struct logStruct
    
    {
    
        ipaddress ip;
    
        char * as_line;
    
    } log;
    
    void getIP(ipaddress * ip, char * as_line)
    
    {
        as_line+=3;
    
        ip->w = atoi(strtok(as_line, "."));
    
        ip->x = atoi(strtok(NULL, "."));
    
    
        ip->y = atoi(strtok(NULL, "."));
    
    
        ip->z = atoi(strtok(NULL, " "));
    
      
    sortArray = calloc(sizeof(log *) , numLines);
    
        for (counter = 0; counter < numLines; counter++)
        {
            sortArray[counter] = calloc(sizeof(log),1);
            sortArray[counter]->as_line = calloc(sizeof(char),500);
            fgets(sortArray[counter]->as_line,500,myfile);
            strcpy(buffer,sortArray[counter]->as_line);
            getIP(&(sortArray[counter]->ip),buffer);
    	/*sortArray[counter]->as_line*/
    	}
    	 	
       bubbleSort(sortArray, numLines);
    
    void bubbleSort(log * * array, int numLines)
    
    {
    
    log * temp_store;
    
    	int i, j;
    
    	
        for (i = 0; i < numLines; i++)
    
            for (j = 0; j < length-1; j++)
    
            {
    
                
        
    
                  tempPointer = array[j];
    
                    array[j] = array[j+1];
    
                    array[j+1] = tempPointer;
     
    Last edited by a moderator: Apr 25, 2008

Share This Page