Thread: Need some help on writing code

  1. #1
    A slow C++ user
    Guest

    Need some help on writing code

    Hello everyone,
    Thanks for listening, here is my dilemena. I am working on a lab from my Data Structures and Algorithms class, and I am stuck thanks to some poorly worded instructions, at least in my mind. Here is the paragraph giving me trouble, if you need to know more about the code please say so in your reply. This is program has a header, driver, and implemenation file, and all rest of the code works fine.

    "Implement the setPos function. This is supposed to set currentPos to the value of the arguement. Of course, you must not allow the current position to become illegal, current always represents an ordinal position (1 means first, 2 means second, etc.) In addition, if a list has 2 items, current must be 'first', 'second', or 'third'. The last case indicates that the 'end of the list' is the current position. If setPos is given an illegal arguement, it is to ignore the request (do nothing). Build project to be sure you have no syntax errors."

    Maybe that all makes more sense to one of you. If you want to see my code thus far, I can email it to you if needed. Once again, thanks for listening.

    Andrew

  2. #2
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    nevermind..forget my comment
    nextus, the samurai warrior

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    e-mail is against forum rules

    just post your code here, in code tags

  4. #4
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    yes..please post code..i have 2 hours to kill
    nextus, the samurai warrior

  5. #5
    A slow C++ user
    Guest
    Alright here is the code....

    Code:
    #include<iostream>
    using namespace std;
    
    #include"List.h"
    
    List::List(){
    	cout<<"List constructor called - creating empty list"<<endl;
    	numItems = 0;
    	//current starts at 'first' position
    	// (which is also the 'end of the list' position for an empty list)
    	currentPos = 1;
    	items[0] = 0;
    }
    
    List::~List(){
    	cout<<"List destructor called"<<endl;
    }
    
    
    bool List::insert(const ITEM_TYPE &A ){
    
    	bool status;
    
    	if(numItems < MAX_ITEMS )
    	{
    
    		for(int x = numItems; x > 0; x--)
    			items[x + 1] = items[x];
    
    		items[1] = A;
    
    		numItems++;
    
    		status = true;
    	}
    	else
    		status = false;
    
    	return status;
    	
    }
    
    
    void List::print(){
    
    	cout<<"[";
    
    	for(int x = 1; x < numItems; x++)
    		cout<<items[x]<<",";
    	
    	cout << items[numItems] << "]" << endl;
    
    }
    
    int List::length(void) const
    { return numItems;
    }
    
    void setPos(int n=1)
    {
    	currentPos = n;
    
    	
    		
    }

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    6
    hmm.... having a "current position" variable doesn't seem to be much help for your code... It looks like what you are supposed to be doing is making a linked list (a list where each node has a pointer to the next one)... and it seems like what you have here is just an array. it would be helpful to see your header file and your assignment, but I think you might have to entirely rethink your code.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I've seen better worded instructions, but I've seen worse too. I assume that List.h is a given file and dictates how you are to implement a list. USUALLY, the term list is used to indicate a linked list, as implied by logicalhippo, but I've seen lists implemented as an array before. After all, the word list is about as specific as the word house. Lists implemented as arrays suffer from all the woes of an array (difficult to "expand" at will--or inability to expand--depending on your point of view, problem of overreading, need to flag deleted entries or shift remaining entries after deletions/insertions into the middle, etc), and benefit from all the good points of the virtues of an array (uh, random access only I guess).

    No matter how the list is implemented however, the concept of setPos() is to identify a given element/node/link of the list that contains the specific value sent to setPos() as an argument. If the list is implemented as an array, then currentPos will be an integer related to the offset from the beginning of the array, whereas if the list is implemented as a linked list, then currentPos will be a pointer (or an iterator if the STL list class is used--STL containers typically implement the "end" iterator to be the one just after the "last" entry) containing the address of the node/link that has the desired value.

    If you use a list implemented as an array, then use a loop to search the elements of the array. At the end of the loop, the loop variable will be the value of currentPos - 1, unless the value wasn't found, in which case the value of currentPos will be the same as the value of the loop variable.

    Code:
    for(i = 0; i < length; ++i)
    {
      if(array[i] == desired value)
          break;
    }
    if(i == length)
      currentPos = i;
    else
       currentPos = i +1;
    Alternatively, if you implement the list as a linked list, then the nodes will need to have a numerical indicator, which will be used as the ordinal value, as well as a data value.
    Code:
    struct Node
    {
       int ordinalPosition;
       //data;
       Node * next;  
       //optional second Node pointer if using doubly linked list
    };
    
    class List //1templated to be able to hold any value type
    {
       public:
          int currentPos;
          int numNodes;//variable to hold number of nodes/links in list
          void setPos(argument type);
         //etc
    };
     
    void List::setPos(argument type)
    {
       Node * current = head;
       while(current != Null && current->data != desired value)
       {
           current = current->next;
       }
       if(current == NULL)//desired value not found
          currentPos = numNodes + 1;
       else
          currentPos = current->ordinalPosition;
    }
    The most frequent use of the concept of currentPos is to indicate the address of the node/link with the desired value, not the ordinal position of the node/link with the desired value. But so be it. If that's what you need to do, then that's what you need to do.

  8. #8
    Registered User Xei's Avatar
    Join Date
    May 2002
    Posts
    719
    Originally posted by alpha
    e-mail is against forum rules

    just post your code here, in code tags
    http://cboard.cprogramming.com/misc....q&page=2#email

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing Code but am having problems
    By Choppers in forum C Programming
    Replies: 20
    Last Post: 06-25-2009, 06:18 PM
  2. Writing code for a program in C
    By Sure in forum C Programming
    Replies: 7
    Last Post: 06-11-2005, 01:33 PM
  3. professional programmers, do you spend more time writing or modifying code
    By Terrance in forum A Brief History of Cprogramming.com
    Replies: 29
    Last Post: 11-25-2002, 10:54 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM