Thread: urgent help please...

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

    urgent help please...

    please help really urgent..the red colour is the correct output whereas the blue display is my display which has a simple error. please give me advice or help me ....

    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


    Code:
    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
        }
    }
    the blue colour has extra space... wat problem cause tat please.... i think is my insertinorder problem....correct me if i am wrong

  2. #2
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    You posted a function which has nothing to do with the output because I see no functions that would output that blue text.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    ok here is my implementation file ..

    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 this is my main file

    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;
    }

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Count number of times you use
    Code:
    cout << endl;
    in your programs
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    is there any other instead of changing anything in main/// cause tat main.cpp are supply by my lecturer

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    i show you the preview of the testing

  7. #7
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    this is the error but i couldnt wat is wrong with it ,...
    Code:
    =======================================
    (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
     

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    You found 2 occurences of the code that puts an empty line, right?

    If you cannot remove the code from the main function, you should... o! yes! Remove it from the second place.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    do you mean in
    bool IPlist::isPresent(string address) const
    function???

    can you find any error in the red code?

  10. #10
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    anyone help please

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    bumpy bumpy please it's urgent.
    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.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Start ONE new thread with your current question without using the word URGENT anywhere in the title or text content, and do NOT bump it with useless "anyone help please" phrases.
    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