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 05-Jul-2004, 16:14
siicon siicon is offline
New Member
 
Join Date: Jul 2004
Posts: 2
siicon is on a distinguished road

Reverse output for a Stack


Hi everyone, I've been taking a class on cprogramming and have been successful up thus far. I am now learning about stacks and am having tremendous problems. I've written a program that will calculate the prime factors of numbers. The only problem is that I needed to reverse the output as show in the example below
e.g. Prime factors of #
1
2
3
4

need printout to read
4
3
2
1

I understand the concept in which the output will first be stored in an array [1,2,3,4] but am not entirely sure on how to retreive it without making the program really long. I know about pop functions but have only used this with push empty and full functions in class. I am just having problems on trying to figure out how to implement it here.
I've tested my prime factorization program by itself and I know it works but when I try to integrate it into the stack I get numerous errors and I am not sure if I am on the right track
Please let me know if you have any suggestions. I feel as if I am close but am started to get frustrated because it feels as if I am missing something small
Any help will be greatly appreciated

Thanks in advance
Sean

CPP / C++ / C Code:
#include <iostream.h>
const int STACK_MAX= 20;
typedef int StackElement;

class Stack
 {
  private:
   StackElement StackArray[STACK_MAX];
   int StackTop;

 public:
	bool empty();
	int primefactor(int number);
	bool full( );
    bool isprime(int number);
    void push(const StackElement & value);
    void pop();
	int top();
	void display();

    Stack::Stack()                          //Constructor
   {
    StackTop=-1;

   }

	Stack::primefactor(int number)
{
   bool isPrime = isprime();
   int prime = number;
   int i = 2, j;
   double squareroot = sqrt(static_cast<double>(number));
	int count = 0;
   cout << "The prime factorization of the number " << number << " is:" << endl;
	if(isPrime)
      cout << space << number << " is a prime number." << endl;
   else {
      while((prime > 0) && (i <= number)) {
         if((prime % i) == 0) {
				count++;
				for(j = 0; j < count; j++)
					cout << space;
            cout << i << " is a factor" << endl;
            prime /= i;
			} else         
         	i++;
      }
 return true;
}


	 void Stack::push(const StackElement & value)   //Add value to the Stack
   {
    if(StackTop<STACK_MAX-1)                     //If Stack is not full add element
    	{
       ++StackTop;
       StackArray[StackTop]=value;
      }
    else
     cout<<"Stack is full. \n";
   }



	inline bool Stack::empty() const       //Check for emptyness
   {
    return(StackTop==-1);
   }

}

   
    bool isprime(int number)
{
   int i;
   
   for(i = 2; i < number; i++) {
      if((number % i) == 0)
         return false;
   }
   


    StackElement Stack::top()const     //function to retireve value at top of Stack
     {
      if(StackTop>=0)                   //If Stack is not empty perform task
       return StackArray[StackTop];
      else
      cout<<"Stack is empty\n";
      }

     void Stack::pop()         //Function to discard value at top of Stack
      {
       if(StackTop>=0)        //If Stack is not empty perform task
        StackTop--;
       else
       cout<<"Stack is empty\n";
      }

	 void Stack::display()     //Function to write the entire stack
    {
     for(int i=StackTop;i>=0;i--)
     cout<<StackArray[i]<<endl;
    }
               
int main()
 {
    Stack factors;
	int number;
   
	cout << "Enter a number > 1000 ";
   cin >> number;

  
   
			factors.primefactor();
			factors.top();
			factors.pop();
			factors.display();
	  };


Last edited by admin : 06-Jul-2004 at 02:23. Reason: Please insert [c] & [/c] tags between your example C codes
  #2  
Old 05-Jul-2004, 16:26
siicon siicon is offline
New Member
 
Join Date: Jul 2004
Posts: 2
siicon is on a distinguished road

Prime factor code (by itself)


Hi this is a copy of my prime factorization code before I implement the stack. Once I implement the stack I begin running into errors


CPP / C++ / C Code:
#include <iostream>
#include <string>
#include <cmath>
using namespace std;

const string SPACE_STR = "   ";

void prime_factor(int number);
bool is_prime(int number);

int main() {
   int number;

   cout << "Enter a number > 1000 -> ";
   cin >> number;

   prime_factor(number);

   return 0;
}

// Function I need help with.

void prime_factor(int number)
{
   bool isPrime = is_prime(number);
   int prime = number;
   int i = 2, j;
   double squareRoot = sqrt(static_cast<double>(number));
	int count = 0;
   
   cout << "The prime factorization of the number " << number << " is:" << endl;
	
	if(isPrime)
      cout << SPACE_STR << number << " is a prime number." << endl;
   else {
      while((prime > 0) && (i <= number)) {
         if((prime % i) == 0) {
				count++;
				for(j = 0; j < count; j++)
					cout << SPACE_STR;
            cout << i << " is a factor" << endl;
            prime /= i;
			} else         
         	i++;
      }
   }
}

bool is_prime(int number)
{
   int i;
   
   for(i = 2; i < number; i++) {
      if((number % i) == 0)
         return false;
   }
   
   return true;
}
Last edited by admin : 06-Jul-2004 at 02:24.
  #3  
Old 06-Jul-2004, 01:04
WaltP's Avatar
WaltP WaltP is online now
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by siicon
Hi everyone, I've been taking a class on cprogramming and have been successful up thus far. I am now learning about stacks and am having tremendous problems. I've written a program that will calculate the prime factors of numbers. The only problem is that I needed to reverse the output as show in the example below
e.g. Prime factors of #
1
2
3
4

need printout to read
4
3
2
1

I understand the concept in which the output will first be stored in an array [1,2,3,4] but am not entirely sure on how to retreive it without making the program really long.
Unless I'm not understanding your problem, it's a simple thing.
You have an array with the following values:
[1,2,3,4]
You probably have a variable that has the value 4 (the next empty space in the above array), which I will call nxt. If this is so, then simply:
CPP / C++ / C Code:
for (i=nxt-1; i > 0; i--)
{
    // output this value
}
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
Last edited by admin : 06-Jul-2004 at 02:24.
 
 

Recent GIDBlogStupid Management Policies 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
IP tables rogermark100 C Programming Language 6 18-Apr-2004 08:22
using vector or array oshiotse C++ Forum 4 16-Apr-2004 11:59
Re: Piping and Redirection WaltP C Programming Language 1 11-Apr-2004 09:01
urgent: problem + output filters jack Apache Web Server Forum 0 04-Feb-2004 23:32
articles -> output Allowee Learning Journal by J de Silva 1 05-Jun-2003 20:47

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

All times are GMT -6. The time now is 02:30.


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