PLEASE HELP! Vectors, Stacks, Queues...

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

49 Replies - 3445 Views - Last Post: 07 December 2013 - 09:46 PM Rate Topic: -----

#1 vcj2d  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 07-December 13

PLEASE HELP! Vectors, Stacks, Queues...

Posted 07 December 2013 - 02:21 PM

I Know the forum rules says not to ask for homework help... but i'm desperate!

My Lab assignment just corrupted and I hadn't finished debugging my errors. I finished this assignment a few days ago... I can't remember what I had to solve the problems. I just went online to submit and noticed that my labs are both corrupted. I have been working on trying to save the files.. but they are hopeless... I know my limitations it took my 2 weeks to get this lab done.. and I have 1 hour before the submission closes. my professor is very black and white... either it compiles (without error) or it doesnt at all. I can't afford to fail this lab. I am nowhere! Its like my brain is fried from finals! Any and all help .. please! here is the info....

code given:
// Lab10a  Stacks and Queues

#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <queue>

using namespace std;

template <class T>
bool Same(vector <T> tVector);


int main()
{
	const int SIZE_INT = 8;
	const int SIZE_STRING = 6;

	
	int iTemp1[]= {1,1,2,3,3,2,1,1};
	int iTemp2[]= {1,1,2,3,2,3,1,1};


	vector <int> iVector1(iTemp1, iTemp1 + SIZE_INT);
	vector <int> iVector2(iTemp2, iTemp2+ SIZE_INT);

	string sTemp1[]= {"aa", "bb", "cc", "cc", "bb", "aa"};
	string sTemp2[]= {"aa", "bb", "cc", "bb", "cc", "aa"};

	vector <string> sVector1(sTemp1, sTemp1 + SIZE_STRING);
	vector <string> sVector2(sTemp2, sTemp2 + SIZE_STRING);
	
	if (Same(iVector1))
		cout << "iVector1 values are in the same order forwards and backwards\n";
	else
		cout << "iVector1 values are NOT in the same order forwards and backwards\n";

	if (Same(iVector2))
		cout << "iVector2 values are in the same order forwards and backwards\n";
	else
		cout << "iVector2 values are NOT in the same order forwards and backwards\n";

	if (Same(sVector1))
		cout << "sVector1 values are in the same order forwards and backwards\n";
	else
		cout << "sVector1 values are NOT in the same order forwards and backwards\n";

	if (Same(sVector2))
		cout << "sVector2 values are in the same order forwards and backwards\n";
	else
		cout << "sVector2 values are NOT in the same order forwards and backwards\n";

	system("pause");
	return 0;
}

template <class T>
bool Same(vector <T> tVector)
{

	/*Write the code for this function.  You must use one STL stack of 
	type T and one STL queue of type T.  You should not have more than 15 lines of
	code total.*/
	
}


and documentation
[quote]Finish the code for Lab10a.cpp . You will write code for the Same function which will return
true if the values of the elements of the vector that is passed to it are the same read both
forwards and backwards. If they are not the same in both directions, it will return false.
Donít change anything in the main function or add any additional functions to the program.

Output should match iVector1 same order, iVector2 Not same order, sVector1 same, and sVector2 not in same order.



and then the second part:
Trees - Part B (25 points)
Please complete the program below exactly as described. Donít add anything extra to it
(methods, extra variables, features, etc.) and donít leave any features described below out.
You are going to write a function that will use recursion to count the number of nodes in a tree. First
use the code on page 1141-1142 to develop the IntBinaryTree class (For the member functions,
you only need the code for the constructor and the insert member function). Your main program will
call a public member function called numNodes. This function will then call a private member
function, countNodes, that will use recursion to count the number of nodes in the entire tree:
int  IntBinaryTree::countNodes(TreeNode *nodePtr) 
{ 
 if (nodePtr == NULL) 
  //write only one line of code here 
 else 
  //write only one line of code here 
} 
 
int main() 
{ 
   IntBinaryTree tree; 
    
   tree.insertNode(14); 
   tree.insertNode(10); 
   tree.insertNode(8); 
   tree.insertNode(6); 
   tree.insertNode(5); 
   tree.insertNode(7); 
   tree.insertNode(9); 
   tree.insertNode(12); 
   tree.insertNode(11); 
   tree.insertNode(13); 
   tree.insertNode(22); 
   tree.insertNode(30); 
   tree.insertNode(32); 
   tree.insertNode(24); 
   tree.insertNode(20); 
   tree.insertNode(17); 
   tree.insertNode(15); 
   tree.insertNode(18); 
   tree.insertNode(21);    
 
   cout <<"The number of nodes in the tree is: " << tree.numNodes()<<endl; 
 
   return 0; 
}


Code from pages 1140-1142:

InBinaryTree.h
// Specification file for the IntBinaryTree class
#ifndef INTBINARYTREE_H
#define INTBINARYTREE_H

class IntBinaryTree
{
private:
   struct TreeNode
   {
      int value;         // The value in the node
      TreeNode *left;    // Pointer to left child node
      TreeNode *right;   // Pointer to right child node
   };

   TreeNode *root;       // Pointer to the root node
   
   // Private member functions
   void insert(TreeNode *&, TreeNode *&);
   void destroySubTree(TreeNode *);
   void deleteNode(int, TreeNode *&);
   void makeDeletion(TreeNode *&);
   void displayInOrder(TreeNode *) const;
   void displayPreOrder(TreeNode *) const;
   void displayPostOrder(TreeNode *) const;
   
public:
   // Constructor
   IntBinaryTree()
      { root = NULL; }
      
   // Destructor
   ~IntBinaryTree()
      { destroySubTree(root); }
      
   // Binary tree operations
   void insertNode(int);
   bool searchNode(int);
   void remove(int);
   
   void displayInOrder() const
      {  displayInOrder(root); }
      
   void displayPreOrder() const
      {  displayPreOrder(root); }
      
   void displayPostOrder() const
      {  displayPostOrder(root); }
};
#endif



Can Anyone help me!!!

Is This A Good Question/Topic? 0
  • +

Replies To: PLEASE HELP! Vectors, Stacks, Queues...

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13545
  • View blog
  • Posts: 54,057
  • Joined: 12-June 08

Re: PLEASE HELP! Vectors, Stacks, Queues...

Posted 07 December 2013 - 02:26 PM

Quote

Can Anyone help me!!!

Elaborate, please.
Was This Post Helpful? 0
  • +
  • -

#3 vcj2d  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 07-December 13

Re: PLEASE HELP! Vectors, Stacks, Queues...

Posted 07 December 2013 - 02:34 PM

View Postmodi123_1, on 07 December 2013 - 02:26 PM, said:

Quote

Can Anyone help me!!!

Elaborate, please.


I have no idea what to do... It took me forever to slowly figure out the two parts of this lab. A hint/snippet/ANYTHING with be so helpful!!!

I'm burnt out, I have been going onlne doing research forever... ANd still can't figure this out! I
Was This Post Helpful? 0
  • +
  • -

#4 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1392
  • View blog
  • Posts: 4,391
  • Joined: 20-March 10

Re: PLEASE HELP! Vectors, Stacks, Queues...

Posted 07 December 2013 - 02:43 PM

Well,

The best thing to do is contact your Tutor for an extension,

However saying your code corrupted isnt really helping us to help you...

What errors are you getting ?

Or are you saying you lost all your code and dont remember what you coded or how you solved the problems ??

Expecting us to finish your Tutor marked Assignment in an hour is really clutching at straws and again is not helping us to help you, you should really hand your assignments in a couple of days before the deadline closes to avoid stuff like this.

Thats about the best advice I can give at the moment..

Regards

Snoopy.
Was This Post Helpful? 1
  • +
  • -

#5 vcj2d  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 07-December 13

Re: PLEASE HELP! Vectors, Stacks, Queues...

Posted 07 December 2013 - 02:57 PM

View Postsnoopy11, on 07 December 2013 - 02:43 PM, said:

Well,

The best thing to do is contact your Tutor for an extension,

However saying your code corrupted isnt really helping us to help you...

What errors are you getting ?

Or are you saying you lost all your code and dont remember what you coded or how you solved the problems ??

Expecting us to finish your Tutor marked Assignment in an hour is really clutching at straws and again is not helping us to help you, you should really hand your assignments in a couple of days before the deadline closes to avoid stuff like this.

Thats about the best advice I can give at the moment..

Regards

Snoopy.


By corrupted I mean the actual files.. error message something like invalid file access path... so i go directly to files and open manually and get messages like corrupted file. i try to open as a text file and i just get nothing in the document. I checked as binary and there is data there but from prior dumps I have looked at there is no consistency. just repetition of the same character for an entire line .. with each line a different ascii character. So yes I lost all my code!

I am not expecting anyone to finish this assignment (nevertheless in less than an hour...) I am trying to get anyhelp I can to try and finish this (but like all stressed students I would never turn down a finished assignment... but only if the person explains to me how and why they did what they did)...

As for turning it in early.. thats why I finished it early... but my professor strongly disapproves of early submissions. why.... I HAVE NO CLUE! its frustrating...(especially when you are taking 23 credit hours in a semester. (6 classes + 2 labs)

I would ask for an extension but my professor has a system... once a semester we can have a no questions asked extension. I used mine when I was out sick with the flu in late october.. So I am out of luck... with that one.

snoopy, all i am asking is for anything I can get whether it be: weblinks, examples, guided assistance, code snippets, hints, ANYTHING!!!!
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12174
  • View blog
  • Posts: 45,241
  • Joined: 27-December 08

Re: PLEASE HELP! Vectors, Stacks, Queues...

Posted 07 December 2013 - 03:03 PM

Let's review here:
59	bool Same(vector <T> tVector)
60	{
61	 
62	    /*Write the code for this function.  You must use one STL stack of
63	    type T and one STL queue of type T.  You should not have more than 15 lines of
64	    code total.*/
65	     
66	}



So you have to use a stack and a queue. How do both of these structures manage data? Consider a small example, with your tVector holding the elements in the following order: [1, 2, 4, 2, 1]. What happens as you add each of these to the stack? How about a queue? What happens as you poll one element of the stack followed by one element of the queue? Compare these elements? What about when your tVector holds elements such as [1, 2, 4, 3, 1?

Quote

You are going to write a function that will use recursion to count the number of nodes in a tree. First
use the code on page 1141-1142 to develop the IntBinaryTree class (For the member functions,
you only need the code for the constructor and the insert member function). Your main program will
call a public member function called numNodes. This function will then call a private member
function, countNodes, that will use recursion to count the number of nodes in the entire tree:

How do you recursively traverse a tree? Are you familiar with the fact that the child nodes of a given node are roots of subtrees? So if I have:
        Root
       /    \
      1     2
     / \   / \
    3   4 5   6



And I recurse, starting at Root. I have one node right? Then I have to count numNodes for the subtree rooted at 1, and numNodes for the tree rooted at 2. So recurse and evaluate 1. I have a single node at node 1, plus however many nodes for the subtrees rooted at 3 and 4.

Do you follow the logic?
Was This Post Helpful? 0
  • +
  • -

#7 vcj2d  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 07-December 13

Re: PLEASE HELP! Vectors, Stacks, Queues...

Posted 07 December 2013 - 03:07 PM

View Postmacosxnerd101, on 07 December 2013 - 03:03 PM, said:

Let's review here:
59	bool Same(vector <T> tVector)
60	{
61	 
62	    /*Write the code for this function.  You must use one STL stack of
63	    type T and one STL queue of type T.  You should not have more than 15 lines of
64	    code total.*/
65	     
66	}



So you have to use a stack and a queue. How do both of these structures manage data? Consider a small example, with your tVector holding the elements in the following order: [1, 2, 4, 2, 1]. What happens as you add each of these to the stack? How about a queue? What happens as you poll one element of the stack followed by one element of the queue? Compare these elements? What about when your tVector holds elements such as [1, 2, 4, 3, 1?


I think what I am getting from this is:
stacks pull as LIFO : so use this to check from the end of the vector list
and
queues pull as FIFO : so use this to check from the front of the vector list

and compare these two results ... yes? no?
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12174
  • View blog
  • Posts: 45,241
  • Joined: 27-December 08

Re: PLEASE HELP! Vectors, Stacks, Queues...

Posted 07 December 2013 - 03:08 PM

Give it a try. ;)
Was This Post Helpful? 0
  • +
  • -

#9 vcj2d  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 07-December 13

Re: PLEASE HELP! Vectors, Stacks, Queues...

Posted 07 December 2013 - 03:19 PM

SO... I cant find how to incorporate the template t for part A...

so far logic is to have variable storing values of:
frontToBack //value moving from front to middle
backToFront // value moving from end to middle


AND... for Part B, based on the instructions I believe that the cpp files code should be something like? YES?NO?
int  IntBinaryTree::numNodes(TreeNode *nodePtr) 
{ 
 if (nodePtr == NULL) // A nothing with no children: zero nodes here
  return 0;
 else // A node with 0, 1 or 2 children and further "offspring"
  return 1 + numNodes(nodePtr->leftChild) + numNodes(nodePtr->rightChild);
} 

but the header file once simplified as instructions asked:
// Specification file for the IntBinaryTree class
#ifndef INTBINARYTREE_H
#define INTBINARYTREE_H

class IntBinaryTree
{
private:
   struct TreeNode
   {
      int value;         // The value in the node
      TreeNode *left;    // Pointer to left child node
      TreeNode *right;   // Pointer to right child node
   };

   TreeNode *root;       // Pointer to the root node
   
   // Private member functions
   void insert(TreeNode *&, TreeNode *&);
   void countNodes(TreeNode *); ////////////////
   
public:
   // Constructor
   IntBinaryTree()
      { root = NULL; }
   // Binary tree operations
   void insertNode(int)
   {}
   void numNodes (int) 
   { countNodes(int);} ////////////////////////////
    

};
#endif


note: "/////////////////" I dont think this is even close to right...
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12174
  • View blog
  • Posts: 45,241
  • Joined: 27-December 08

Re: PLEASE HELP! Vectors, Stacks, Queues...

Posted 07 December 2013 - 03:26 PM

Quote

AND... for Part B, based on the instructions I believe that the cpp files code should be something like? YES?NO?

The logic looks correct. I'm not a C++ guy, so I can't really help you much more than that. As always, compile and test your code.
Was This Post Helpful? 0
  • +
  • -

#11 vcj2d  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 07-December 13

Re: PLEASE HELP! Vectors, Stacks, Queues...

Posted 07 December 2013 - 03:32 PM

Quote

macosxnerd101 - I may not be the best a c++ programming but as a fellow mac osx enthusiast i expect you to find this humorous.
http://www.macgasm.n...se-apple-adobe/


Okay thank you macosxnerd101!!! But can anyone explain how to incorporate templates with stacks and queues for me????
Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12174
  • View blog
  • Posts: 45,241
  • Joined: 27-December 08

Re: PLEASE HELP! Vectors, Stacks, Queues...

Posted 07 December 2013 - 03:35 PM

I'm not a C++ guy, so I can't help you with that. It looks like C++ specific syntax.
Was This Post Helpful? 0
  • +
  • -

#13 vcj2d  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 07-December 13

Re: PLEASE HELP! Vectors, Stacks, Queues...

Posted 07 December 2013 - 03:56 PM

So I talked with my professor after a very long discussion we agreed on an additional extension until 9 pm EST (3hrs, ~6mins). But if I take it, he will deduct 10% from final grade (better than a straight F)... But Is there anyone who can help me with the C++ specific syntax for templates with stacks and queues?????
Was This Post Helpful? 0
  • +
  • -

#14 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1392
  • View blog
  • Posts: 4,391
  • Joined: 20-March 10

Re: PLEASE HELP! Vectors, Stacks, Queues...

Posted 07 December 2013 - 04:00 PM

Right well,

As with any large program you break it down into smaller manageable chunks

bool Same(vector <T> tVector)
{

	vector <T> forwards;
	vector <T> backwards;
	
}


First you need a forwards and backwards container as above
bool Same(vector <T> tVector)
{

	vector <T> forwards;
	vector <T> backwards;
    for(int i=0; i<tVector.size(); i++)
    {
        forwards.push_back( tVector[i] );

    }
    for(int i=tVector.size(); i>0; i--)
    {
        backwards.push_back( tVector[i-1] );

    }
    
}


Then you need to read in the values into forwards in a forward direction
and backwards in a reverse direction like above


finally you set up a bool variable to determine whether forwards = backwards

you do this with a loop and return the variable.

bool Same(vector <T> tVector)
{

	vector <T> forwards;
	vector <T> backwards;
	for(int i=0; i<tVector.size(); i++)
    {
        forwards.push_back( tVector[i] );

    }
    for(int i=tVector.size(); i>0; i--)
    {
        backwards.push_back( tVector[i-1] );

    }
    bool flag = true;
    for(int i=0; i<tVector.size(); i++)
    {
        if(forwards[i]!=backwards[i])
        {
            flag = false;
            break;
        }
    }

    return flag;
}


Thats one part down you do the same for the rest of the code.

This should really have never taken you two weeks.

Regards

Snoopy.
Was This Post Helpful? 1
  • +
  • -

#15 vcj2d  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 07-December 13

Re: PLEASE HELP! Vectors, Stacks, Queues...

Posted 07 December 2013 - 04:19 PM

I am starting to understand but I am still getting a error (not compilr error) Should I pop after to clear for next vector iteration??? or what!!?

This post has been edited by macosxnerd101: 07 December 2013 - 04:20 PM
Reason for edit:: No need to quote the person above you

Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »