Thread: urgent please please..

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    141

    urgent please please..

    can someone correct this error for me ...

    Code:
    node * IPlist::find (string address) const
    // precondition : none
    // postcondition : returns a pointer to node containing address or
    //                 NULL if not present
    {
        node *searchList = NULL;
        searchList = list;                                                  // the searchList point to the first node of list
        
        while(searchList != NULL && searchList ->IPaddress != address)      // when searchList is NULL and Ipaddress is not equal to Address
        {
            searchList = searchList->link;                                  // we go to next nope
        }
        return searchList;                                                  // otherwise we found the exact Ip address in the link list
    }
    actually the output that lecturer want is follow but i got extra spaces.. i dont know what is the problem .. please help really urgent..

    IP address Count
    a 3
    b 2
    c 2
    d 1

    found d
    ** your output **
    IP address Count
    a 3
    b 2
    c 2
    d 1

    //extra space
    found d

  2. #2
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    The problem isn't actually in the it of code you have posted. The problem (guessing as you haven't posted you code) is probably in the way you output your results. There's probably an extra "endl" in their somewhere.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    no it is not the case.. as the main are supplied by my lecturer..
    can you correct it for me ???? the red section has an error tats why i just got half mark

    my implementation code

    Code:
    #include "IPlist.h"
    
    IPlist::IPlist()
    // default constructor
    // creates a new empty list
    {
        list = NULL;     
    }
                   
    
    IPlist::~IPlist()
    // destructor - reallocates all dynamic memory of list
    {
    while(list != NULL)
        {
            node *tmp = list;
            list = list-> link;
            delete tmp;             // release the memory pointed to by tmp
        }
    }
    
    
    IPlist::IPlist (const IPlist & other)
    // copy constructor to create a deep copy of other IPlist object
    {   
        if(other.list == NULL)
            list = NULL;
        else
        {
    //copy the first node
            node *current = other.list;                // pointer to traverse the list
            list = new node;                           // create the node
            list->IPaddress = current->IPaddress;      // copy the IPaddress
            list->link = NULL;                         // set the link field of the node to NULL
            current = current->link;                   // make current point to the next node
            node *newNode = list;                      // pointer to create a node
    // copy the remaining list
            while(current != NULL)
            {
                newNode->link = new node;              // create a node
                newNode = newNode->link;               
                newNode->IPaddress = current->IPaddress;//copy the IPaddress
                newNode->link = NULL;                  // set the link of newNode to NULL
                current = current->link;               // make current point to the next node
            }
        }
    }
    
    bool IPlist::empty() const
    // precondition : none
    // postcondition : returns true if list is empty 
    //                 and otherwise false
    {
        return list == NULL;               
    }
    
    
    void IPlist::insertInOrder(string address)
    // precondition : list is ordered and address is not in list
    // postcondition : list is ordered and contains address
    {
        node *tmp = new node;
        tmp->IPaddress = address;
        tmp->count = 1;
        tmp->link = NULL;
        
        if(list == NULL || list->IPaddress > address)      // if list is null or ipaddress in list is larger than address
        {
            tmp->link = list;                              // Insertion at
            list = tmp;                                    // the beginning
        }
        else
        {
            node *cur = list;                              
            node *prev = NULL;
            while(cur != NULL && cur->IPaddress < address)
            {
                prev = cur;                               // else insertion 
                cur = cur->link;                          // in the middle
            }
            prev->link = tmp;                             // otherwise insertion
            tmp->link = cur;                              // at the end
        }
    }
    
    bool IPlist::isPresent(string address) const
    // precondition : none
    // postcondition : returns true if a node containing address
    //                is present in list and otherwise false
    {
        node *current; //  pointer to traverse the list
        bool found = false;  
        
        current = list; //  set current to point to the first node in the list
        
        while(current != NULL && !found) //  search the list
            if(current->IPaddress == address) //  search item is found
                found = true;
            else
                current = current->link; //  make current point to next node
        return found;
    }
    
    void IPlist::updateCount(string address)
    // precondition : address is in list
    // postcondition : the count of node with address has been incremented
    {
        node *tmp = list;                                                       // tmp point to the first node of list
        while( tmp->IPaddress != address)                                       // if ipaddress is not address
            tmp = tmp->link;                                                   // go to next node 
        tmp->count++;                                                         // increament count by 1
    }
    
    
    node * IPlist::find (string address) const
    // precondition : none
    // postcondition : returns a pointer to node containing address or
    //                 NULL if not present
    {
        node *tmp;
        for(tmp = list; tmp != NULL; tmp = tmp->link)
            if(address == tmp->IPaddress)
                return tmp;
    }
    
    
    void IPlist::display() const
    // precondition : none
    // postcondition : the list has been displayed 
    // one record per line, tab-separated
    // with heading IPaddress Count
    {
        cout << "IP address" << '\t' << "Count" << endl;
        node *curr = list;
        while (curr != NULL)
        {
            cout << curr->IPaddress << '\t' << curr->count << endl;       // display Ip Address and Count
            curr = curr->link;
        }
        cout << endl;        
    }
    and my main code

    Code:
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    #include "IPlist.h"
    
    int main()
    {
        IPlist ip;
    
        string data[]={"a","b","c","a","b","c","a","d"};
        // some strings to insert into IPlist for testing
        // they don't really need to be IP addresses    
        for (int i = 0; i < 8; i++)
        {
           if (!ip.isPresent(data[i]))
              ip.insertInOrder(data[i]);
           else
              ip.updateCount(data[i]);
        }
        ip.display();
        cout << endl;
        node *current = ip.find("d");
        if (current != NULL)
           cout << "found "<< current->IPaddress << endl;
    
        system("pause");
        return EXIT_SUCCESS;
    }
    Code:
    From  	View message header detail Ros Ballantyne <[email protected]> 
    Sent  	Monday, October 30, 2006 7:13 pm
    To  	[email protected] 
    Subject  	COMP125 Assignment 3  preview
    
    =======================================
    (spaces,tabs and newlines do not matter)
    *******************************************
    * FILE NAMES DO MATTER                    *
    *                                         *
    * SAME preview (as last night)            *
    * BUT  gives 2 marks for each test        *
    * and some marks for some correct output  *
    * and tells you if filenames are incorrect*
    ******************************************
    CHECKING FILENAMES
    Testing Application
    Program compiled
    There are 4 tests each worth 1 mark
    ====== Test 1  Data File contains ========
    192.149.089.061
    100.001.004.031
    034.056.078.012
    192.149.089.061
    100.001.004.031
    192.149.089.061
    111.022.033.004
    192.149.089.061
    111.022.033.004
    111.022.033.004
    ============================
    ====== Test 1  Batch input to program contains ========
    data1.txt
    ============================
    ** Passed test 1
    ====== Test 2  Data File contains ========
    111.022.033.004
    111.022.033.004
    111.022.033.004
    111.022.033.004
    111.022.033.004
    111.022.033.004
    111.022.033.004
    111.022.033.004
    111.022.033.004
    111.022.033.004
    ============================
    ====== Test 2  Batch input to program contains ========
    data2.txt
    ============================
    ** Passed test 2
    ====== Test 3  Data File contains ========
    192.149.089.061
    100.001.004.031
    034.056.078.012
    192.149.089.061
    100.001.004.031
    192.149.089.061
    111.022.033.004
    192.149.089.061
    111.022.033.004
    111.022.033.004
    ============================
    ====== Test 3  Batch input to program contains ========
    error.txt
    error2.txt
    error.txt
    data3.txt
    ============================
    ** Passed test 3
    ====== Test 4  Data File contains ========
    ============================
    ====== Test 4  Batch input to program contains ========
    data4.txt
    ============================
    ** Passed test 4
    Testing Linking With Other Main programs
    There are 4 tests
    =================Main 1 contains =====================
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    #include "IPlist.h"
    
    int main()
    {
        IPlist ip;
        if (ip.empty())
          cout << "empty" << endl;
        system("pause");
        return EXIT_SUCCESS;
    }
    =======================================================
    Program compiled
    ** Passed test 1
    =================Main 2 contains =====================
    
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    #include "IPlist.h"
    
    int main()
    {
        IPlist ip;
    
        string data[]={"a","b","c","a","b","c","a","d"};
        // some strings to insert into IPlist for testing
        // they don't really need to be IP addresses   
        for (int i = 0; i < 8; i++)
        {
           if (!ip.isPresent(data[i]))
              ip.insertInOrder(data[i]);
           else
              ip.updateCount(data[i]);
        }
        ip.display();
        cout << endl;
        node *current = ip.find("d");
        if (current != NULL)
           cout << "found "<< current->IPaddress << endl;
    
        system("pause");
        return EXIT_SUCCESS;
    }
    =======================================================
    Program compiled
    ** Half marks for test 2
    ** sample output **
    IP address  Count
    a 3
    b 2
    c 2
    d 1
    
    found d
    ** your output **
    IP address Count
    a 3
    b 2
    c 2
    d 1
    
    
    found d
    =================Main 3 contains =====================
    
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    #include "IPlist.h"
    
    void testCopyConstructor(IPlist x);
    int main()
    {
        IPlist ip;
    
        string data[]={"a","b","c","a","b","c","a","d"};
        // some strings to insert into IPlist for testing
        // they don't really need to be IP addresses   
        for (int i = 0; i < 8; i++)
        {
           if (!ip.isPresent(data[i]))
              ip.insertInOrder(data[i]);
           else
              ip.updateCount(data[i]);
        }
        ip.display();
        testCopyConstructor(ip);
        ip.display(); // should be same as before
        system("pause");
        return EXIT_SUCCESS;
    }
    
    void testCopyConstructor(IPlist x)
    {
    // copy constructor will be automatically called when pass by value
    // if we have written a correct copy constructor to make a deep copy
    // then changing x will not change the corresponding argument
    // (in this case ip in the main program)
          x.insertInOrder("w");
          x.insertInOrder("x");
    }
    =======================================================
    Program compiled
    ** Half marks for test 3
    ** sample output **
    IP address  Count
    a 3
    b 2
    c 2
    d 1
    IP address  Count
    a 3
    b 2
    c 2
    d 1
    ** your output **
    IP address Count
    a 3
    b 2
    c 2
    d 1
    
    IP address Count
    a 3
    b 2
    c 2
    d 1
    
    =================Main 4 contains =====================
    
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    #include "IPlist.h"
    
    int main()
    {
        IPlist ip;
    
        string data[]={"a","b","c","a","b","c","a","d","xx","zz"};
        // some strings to insert into IPlist for testing
        // they don't really need to be IP addresses   
        for (int i = 0; i < 8; i++)
        {
           if (!ip.isPresent(data[i]))
              ip.insertInOrder(data[i]);
           else
              ip.updateCount(data[i]);
        }
        ip.display();
        cout << endl;
        if (ip.empty())
          cout << "PROBLEMS " << endl;
        node *current;
        for (int i = 0; i < 10; i++)
        {
            current = ip.find(data[i]);
            if (current != NULL)
               cout << "found "<< current->IPaddress << endl;
            else
               cout << "Not found" << endl;
        }
        system("pause");
        return EXIT_SUCCESS;
    }
    =======================================================
    Program compiled
    ** Half marks for test 4
    ** sample output **
    IP address  Count
    a 3
    b 2
    c 2
    d 1
    
    found a
    found b
    found c
    found a
    found b
    found c
    found a
    found d
    Not found
    Not found
    ** your output **
    IP address Count
    a 3
    b 2
    c 2
    d 1
    
    
    found a
    found b
    found c
    found a
    found b
    found c
    found a
    found d
    Not found
    Not found
    
    
    

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Also here http://cboard.cprogramming.com/showthread.php?t=84752
    Don't say it's urgent, it just makes people ignore you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Actually, I'm ........ed off enough to close both of them.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. display character size...(quite urgent..)
    By karthi in forum C Programming
    Replies: 10
    Last Post: 07-11-2007, 09:42 PM
  2. beginner plzz help urgent
    By sara101 in forum C Programming
    Replies: 11
    Last Post: 01-14-2007, 10:38 PM
  3. Linked List Need Help Urgent
    By ykchua in forum C Programming
    Replies: 5
    Last Post: 08-17-2004, 02:57 PM