GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 15-Apr-2006, 21:39
Kubu Kubu is offline
New Member
 
Join Date: Mar 2006
Posts: 5
Kubu is on a distinguished road

prime factors with stack


I have to to write a program that uses a stack to print the prime factors of a positive integer in descending order.

I compiled this and it has alot of errors. Any advice?

I am not sure how to put the codes in special formatted windows for you to view.

Thank you

CPP / C++ / C Code:
#ifndef H_StackType
#define H_StackType

#include <iostream>
#include <cassert>

using namespace std;

template<class Type>
class stackType
{
public:
    const stackType<Type>& operator=(const stackType<Type>&); 
      //Overload the assignment operator.
    void initializeStack();
      //Function to initialize the stack to an empty state.
      //Postcondition: stackTop = 0
    bool isEmptyStack() const;
      //Function to determine whether the stack is empty.
      //Postcondition: Returns true if the stack is empty,
      //               otherwise returns false.
    bool isFullStack() const;
      //Function to determine whether the stack is full.
      //Postcondition: Returns true if the stack is full,
      //               otherwise returns false.
    void destroyStack();
      //Function to remove all the elements from the stack.
      //Postcondition: stackTop = 0

    void push(const Type& newItem);
      //Function to add newItem to the stack.
      //Precondition: The stack exists and is not full.
      //Postcondition: The stack is changed and newItem 
      //               is added to the top of stack.
    Type top() const;
      //Function to return the top element of the stack.
      //Precondition: The stack exists and is not empty.
      //Postcondition: If the stack is empty, the program 
      //               terminates; otherwise, the top element
      //               of the stack is returned.
    void pop();
      //Function to remove the top element of the stack.
      //Precondition: The stack exists and is not empty.
      //Postcondition: The stack is changed and the top 
      //               element is removed from the stack.

    stackType(int stackSize = 100); 
      //constructor
      //Create an array of the size stackSize to hold 
      //the stack elements. The default stack size is 100.
      //Postcondition: The variable list contains the base
      //               address of the array, stackTop = 0, and  
      //               maxStackSize = stackSize.
    stackType(const stackType<Type>& otherStack); 
      //copy constructor
    ~stackType(); 
      //destructor
      //Remove all the elements from the stack.
      //Postcondition: The array (list) holding the stack 
      // elements is deleted.

	
private:
    int maxStackSize; //variable to store the maximum stack size
    int stackTop;        //variable to point to the top of the stack
    Type *list;            //pointer to the array that holds the
                              //stack elements

    void copyStack(const stackType<Type>& otherStack); 
      //Function to make a copy of otherStack.
      //Postcondition: A copy of otherStack is created and
      //               assigned to this stack.
};


template<class Type>
void stackType<Type>::initializeStack()
{
	stackTop = 0;
}                                       //end initializeStack

template<class Type>
void stackType<Type>::destroyStack()
{
	stackTop = 0;
}                                       //end destroyStack

template<class Type>
bool stackType<Type>::isEmptyStack() const
{
	return(stackTop == 0);
}                                         //end isEmptyStack

template<class Type>
bool stackType<Type>::isFullStack() const
{
	return(stackTop == maxStackSize);
}                                        //end isFullStack

template<class Type>
void stackType<Type>::push(const Type& newItem)
{
    if (!isFullStack())
    {
		list[stackTop] = newItem;               //add newItem to the 
                                                //top of the stack
		stackTop++;                             //increment stackTop
    }
    else
		cout << "Cannot add to a full stack." << endl;
}                                                     //end push

template<class Type>
Type stackType<Type>::top() const
{
	assert(stackTop != 0);              //if stack is empty, 
 							                                          //terminate the program
	return list[stackTop - 1];	      //return the element of the 
				       //stack indicated by 
 					  	                                             //stackTop - 1
}                                                 //end top

template<class Type>
void stackType<Type>::pop()
{
	if (!isEmptyStack())
	    stackTop--;                               //decrement stackTop 
 	else
	   cout << "Cannot remove from an empty stack." << endl;
}                                                             //end pop

template<class Type>
stackType<Type>::stackType(int stackSize) 
{
	if(stackSize <= 0)
	{
		cout << "Size of the array to hold the stack must "
			 << "be positive." << endl;
		cout << "Creating an array of size 100." << endl;

		maxStackSize = 100;
	}
	else
		maxStackSize = stackSize;     //set the stack size to 
                                                                  //the value specified by 
 				     	 //the parameter stackSize

	stackTop = 0;		              //set stackTop to 0
	list = new Type[maxStackSize];          //create the array to
  						                    //hold the stack elements
	assert(list != NULL);
}                                                           //end constructor

template<class Type>
stackType<Type>::~stackType()               //destructor
{
   delete [] list;                          //deallocate the memory occupied 
                                               //by the array
}                                             //end destructor

template<class Type>
void stackType<Type>::copyStack(const stackType<Type>& otherStack)
{ 

 	delete [] list;				   
	maxStackSize = otherStack.maxStackSize;		   
  	stackTop = otherStack.stackTop;			   
	  
    list = new Type[maxStackSize];		   
	assert(list != NULL);			   

                                       //copy otherStack into this stack
    for (int j = 0; j < stackTop; j++)  
        list[j] = otherStack.list[j];
}                                    //end copyStack


template<class Type>
stackType<Type>::stackType(const stackType<Type>& otherStack)
{
	list = NULL;

	copyStack(otherStack);
}                                    //end copy constructor

template<class Type>
const stackType<Type>& stackType<Type>::operator=
   					(const stackType<Type>& otherStack)
{ 
   if (this != &otherStack)                 //avoid self-copy
 	  copyStack(otherStack);

   return *this; 
}                                            //end operator=         

template<class Type>
void stackType<Type>::reverseStack(stackType<Type> &otherStack)
{
   int j;

   if (stackTop > otherStack.maxStackSize)
   {
	   cout << "The size of other stack is less than the number of " << endl
		    << "elements currently in the stack." << endl;
	   cout << "This stack cannot be reversed." << endl;
   }
   else
   {
		otherStack.stackTop = stackTop;

	for (j = 0; j < stackTop; j++)   // copy other stack into this stack
	otherStack.list[j] = list[stackTop - 1 - j];
	}
}

#endif

program test
CPP / C++ / C Code:
#include <iostream>			
#include<stdio.h> 
#include<conio.h> 

#include "myStack.h"

using namespace std;

int main()
{
	stackType<int> stack1(50);
	stackType<int> stack2(50);

	stack1.initializeStack();
	stack1.push();
	stack1.push();
	stack1.push();
	stack1.push();
	stack1.push();
	stack1.push();

	cout << "stack1: ";

	while (!stack1.isEmptyStack())
	{
		cout << stack1.top() << " ";
		stack1.pop();
	}

	cout << endl;

	cout << "stack2: ";

	while (!stack2.isEmptyStack())
	{
		cout << stack2.top() << " ";
		stack2.pop();
	}

	cout << endl;

//find the prime factors of a number
cin << num;
int fact=2;
   while(num>1)
    {if((num%fact)==0)
    {push fact;num/=fact;)
     else {fact++;}} 


// print prime in between 2 -100

int primeFlag; 
int i,j; 
   clrscr(); 
   for (i = 2i<100;i++){ 
   primeFlag = 1; 
   for(j = 2; j<i/2+1 && primeFlag ; j++)  
{ 
    if(i%j==0 ) primeFlag=0; 
} 
       if(primeFlag)  
{ 
       printf(" %d ",i); 
    } 
}

// print factors of positive integers in descending order

int a1[100],i,z,j,s; 
clrscr(); 
printf("Enter 100 factors\n"); 
    for (i=2;i<100;i++) 
    { 
    scanf("%d",&a1[i]); 
    } 
for(i=2;i<9;i++) 
{ 
    for(j=i+1;j<100;j++) 
    { 
        if(a1[i]>a1[j]) 

return 0;
}
Last edited by cable_guy_67 : 16-Apr-2006 at 06:21. Reason: Please enclose c++ code in [c++] ... [/c++] tags
  #2  
Old 16-Apr-2006, 08:06
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,702
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: prime factors with stack


Quote:
Originally Posted by Kubu
I have to to write a program that uses a stack

For a better chance of getting meaningful help without getting a lot of information about things that you aren't supposed to do (and, therefore, wasting your time giving us a better explanation spread out over several posts):

State the assignment more precisely: Does the assignment include the requirement that you write your own class for a stack? Or can you use standard template library classes? Or what?

Quote:
Originally Posted by Kubu
I compiled this and it has alot of errors.

1. Post the errors. Paste the exact errors into your post (do not paraphrase). Post all compiler messages (warnings as well as errors). If there are hundreds and hundreds of errors, then post the first 20 or so.

2. Tell us what compiler/operating system you are using. Sometimes it makes a difference to people who are trying to help.

Quote:
Originally Posted by Kubu
I am not sure how to put the codes in special formatted windows for you to view.

Put [c] before the first line of code and put [/c] after the last line of code, as detailed in Guidlines for posting requests...

Regards,

Dave
  #3  
Old 16-Apr-2006, 11:47
Kubu Kubu is offline
New Member
 
Join Date: Mar 2006
Posts: 5
Kubu is on a distinguished road

Re: prime factors with stack


Thank you for your help. This assignment requests that we create our own Template<classType> one has to be myStack header.h and myStack testprogrm.

The program we use is C++ bloodshed. Below are some compiler errors.

myStacktestprogram.cpp:11:21: myStack.h: No such file or directory
myStacktestprogram.cpp: In function `int main()':
myStacktestprogram.cpp:17: error: `stackType' undeclared (first use this function)
myStacktestprogram.cpp:17: error: (Each undeclared identifier is reported only once for each function it appears in.)
myStacktestprogram.cpp:17: error: expected primary-expression before "int"
myStacktestprogram.cpp:17: error: expected `;' before "int"
myStacktestprogram.cpp:18: error: expected primary-expression before "int"
myStacktestprogram.cpp:18: error: expected `;' before "int"
myStacktestprogram.cpp:20: error: `stack1' undeclared (first use this function)
myStacktestprogram.cpp:40: error: `stack2' undeclared (first use this function)
myStacktestprogram.cpp:49: error: `num' undeclared (first use this function)
myStacktestprogram.cpp:53: error: `push' undeclared (first use this function)
myStacktestprogram.cpp:53: error: expected `;' before "fact"
myStacktestprogram.cpp:53: error: expected primary-expression before ')' token
myStacktestprogram.cpp:53: error: expected `;' before ')' token
myStacktestprogram.cpp:61: error: `clrscr' undeclared (first use this function)
myStacktestprogram.cpp:62: error: invalid operands of types `int __complex__' and `int' to binary `operator<'

make.exe: *** [myStacktestprogram.o] Error 1
  #4  
Old 16-Apr-2006, 13:07
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,702
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: prime factors with stack


Quote:
Originally Posted by Kubu

myStacktestprogram.cpp:11:21: myStack.h: No such file or directory

This is pretty important: the compiler can't find your header file! Everything is lost until you tell it.
Add the header file to the project and try again. I'm not running dev-c++ just now, so I can't give details, but I know that there others nearby who might be able to help you with the project settings if you need it.

Now, after it finds the header, there may be other errors, but we'll take the next step when we get that far. Try to see exactly what the compiler tells you for each one. If you can't figure it out, you can ask.

The important step is to look at the messages: start with the very first one. If you can find and fix the problem that caused it, then some (or, maybe even all) of the others may go away. Of course, once you fix one problem, the compiler may find other things that it didn't even see before. Find-fix as you go. Think of it as an adventure.

Once the errors have been silenced, then begins the real adventure: testing/debugging. But you have to get the blooming thing to compile first (and, sometimes that's the hardest part for people just getting started).

Regards,

Dave
  #5  
Old 16-Apr-2006, 16:40
Kubu Kubu is offline
New Member
 
Join Date: Mar 2006
Posts: 5
Kubu is on a distinguished road

Re: prime factors with stack


Okay I found the header file would be #include <stack>. It is not showing the error: myStacktestprogram.cpp:11:21: myStack.h: No such file or directory
anymore.

I am showing this errors which are almost the sames errors as before. Most the errors codes shown below are already declared like the original codes I posted here. Can you still help?

myStacktestprogram.cpp: In function `int main()':
myStacktestprogram.cpp:22: error: `stackType' undeclared (first use this function)
myStacktestprogram.cpp:22: error: (Each undeclared identifier is reported only once for each function it appears in.)
myStacktestprogram.cpp:22: error: expected primary-expression before "int"
myStacktestprogram.cpp:22: error: expected `;' before "int"
myStacktestprogram.cpp:23: error: expected primary-expression before "int"
myStacktestprogram.cpp:23: error: expected `;' before "int"
myStacktestprogram.cpp:25: error: `stack1' undeclared (first use this function)
myStacktestprogram.cpp:45: error: `stack2' undeclared (first use this function)



make.exe: *** [myStacktestprogram.o] Error 1
  #6  
Old 16-Apr-2006, 17:08
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,702
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: prime factors with stack


Quote:
Originally Posted by Kubu
Okay I found the header file would be #include <stack>. It is not showing the error: myStacktestprogram.cpp:11:21: myStack.h: No such file or directory
anymore.

Is that because you took out the statement that included it?

The program must include myStack.h in order to be able create any objects from the class "stackType". You don't need to include the standard library header <stack>, since you won't be using anything from there.

So: you still should add the file myStack.h to your project before you compile it. It should be in your project files, and it has to be #included in your main program file. (And it should be in the same directory as your main program cpp file.)

Quote:
Originally Posted by Kubu

I am showing this errors which are almost the sames errors as before. Most the errors codes shown below are already declared like the original codes I posted here. Can you still help?

myStacktestprogram.cpp: In function `int main()':
myStacktestprogram.cpp:22: error: `stackType' undeclared (first use this function)
This error is because the compiler doesn't know what "stackType" is.

Go back to your original source files (the cpp file has #include "myStack" in it) and, in the dev-c++ interface, add myStack.h to the project files:

In the project window, right-click on the "Project" icon and select "add to project". You should get a window that shows your header file in addition to your cpp file. Select the header file so that both it and the cpp file are in the project.

Then try to compile. You will get errors (but not the same as before).



Regards,

Dave
 
 

Recent GIDBlogObservations of Iraq by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
New to OO need advice weeb0 C++ Forum 2 26-Jan-2006 08:03
Advice on Programming General virtualconcepts MySQL / PHP Forum 3 01-Aug-2005 20:46
Need advice on a disturbing problem JUNK KED Open Discussion Forum 6 31-Mar-2005 13:51
I need some anti-hacking advice! slayerbeatch Open Discussion Forum 6 16-Feb-2005 16:59
HELP! I need advice jmb Web Design Forum 3 25-Mar-2003 03:21

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 16:51.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.