what is going onm

This is a discussion on what is going onm within the C++ Programming forums, part of the General Programming Boards category; ////how can i get rid of all the odd numbers #include <iostream.h> #include <iomanip.h> struct NODE{ int info; NODE *next ...

  1. #1
    Unregistered
    Guest

    what is going onm

    ////how can i get rid of all the odd numbers


    #include <iostream.h>
    #include <iomanip.h>



    struct NODE{
    int info;
    NODE *next ;
    };


    void createList(NODE* &head);
    void InsertInOrder(NODE *&head, int num);
    void printList (NODE *head);
    void sieve (NODE* &head);

    int main()
    {
    NODE *head = 0;
    createList(head);
    printList(head);
    sieve(head);
    printList(head);
    }
    void createList(NODE* &head)
    {
    for (int i =2; i <= 100; i++)
    {
    InsertInOrder (head, i);
    }
    }

    void InsertInOrder (NODE* &head,int num)
    {
    NODE *tempNode;
    tempNode = new NODE ;
    tempNode -> info = num;
    tempNode -> next = 0;
    NODE *marker = head,*back = 0;
    while(marker && marker -> info <= num)
    {
    back = marker;
    marker = marker -> next;
    }

    tempNode -> next = marker;
    if(back)
    back -> next = tempNode;
    else
    head = tempNode;
    }

    void printList(NODE *head)
    {
    NODE *node;
    cout<<" ";
    for(node = head; node; node = node -> next)
    {
    cout<<node -> info;
    cout<< setw(5);
    }
    cout<<endl;
    }

    void sieve(NODE* &head)
    {
    int count = 2;
    NODE *marker =head;
    NODE *nodePtr = marker;
    while(marker)
    {
    while (nodePtr != 0)
    {
    for (int i = 1; i <= count; i++)
    {
    if (!nodePtr)
    break;
    nodePtr = nodePtr -> next;
    }
    if(nodePtr)
    nodePtr -> info = 0;
    }
    marker = marker ->next;
    count = marker -> info;
    }
    }

  2. #2
    Unregistered
    Guest

    Thumbs down here's what's going on

    Talk to Tully.....maybe he'll know how to do it. Mrs. DeBellis is only helping him, so he should be really smart soon

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    Code:
    for (int i =2; i <= 100; i++)
    {
    if(i%2 == 0)
    {
    InsertInOrder (head, i);
    }
    }

    Havent tested it, but I guess it should work
    Last edited by Fordy; 02-14-2002 at 08:58 AM.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    You have an access violation in the function sieve. In other words, you tried to go one link too many and the compiler choked. This seems to work okay.
    Code:
    void sieve(NODE* &head) 
    { 
      int count = 2; 
      NODE *marker = head; 
      NODE *nodePtr = marker; 
      while(marker->next) { 
        while (nodePtr != 0) { 
          for (int i = 1; i <= count; i++) { 
            if (!nodePtr) 
              break; 
            nodePtr = nodePtr -> next; 
          } 
          if(nodePtr) 
            nodePtr -> info = 0; 
        } 
        marker = marker -> next; 
        count = marker -> info; 
      } 
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21