now i am working on an exercise that asks to write this without recursion....

can anyone guide me to what i can do to make this work? i dont want anyone to do it for me but help would be appreciated.

/*
 
#include <cmath>
using namespace std;

void getAllDivisors(int n);
int getLowestDivisors(int x);


int main()

{

	int n = 0;
	
	system("clear");
	
	while (true) {
		cout << "Enter a number (0 = exit) and press ENTER: ";
		cin >> n;
		if (n == 0)
			break;
		getAllDivisors(n);
		cout << "\n\n";
		}
	cout << endl;
	return 0;

}

// get divisors function
// this function prints all the divisors of n,
// by finding the lowes divisor, i , and then
// running funciton get lowestdivisors

void getAllDivisors (int n)

{
	int x;
	double sqrtOfN = sqrt(n);
		
	for (int i = 2; i <= sqrtOfN; i++) {
			
		if (n % i == 0) {
			cout << i << ", ";
			getLowestDivisors(n/i);
			}
		cout << n;
		return;
		
	}
}
				
	
int getLowestDivisors (int x)
	{
		
	double sqrtOfX = sqrt(x);
	
	for (int i = 2; i <= sqrtOfX; i++) {
		if (x % i == 0) {
			cout << i << ", ";
			}
		return x;
	}
	}

this is doing my head in, i understand now why recursions are so useful...

Try removing the comment indicators at the beginning and end of the code. It might compile better.

Help in what? You have to ask a question in order to get help.

ah yes the comments, must have been the way I copied it into the thread, thanks for that.

ok My question is. how do i calculate the prime factors like if I enter 30 the response will be: 2, 3, 5.

I have a program that works using recursive functions but the exercise is asking to modify it without recursion.

Modify Example 4.3 so that it uses a nonrecursive solution. You will end up having to write more code. (Hint: To make the job easier, write two func- tions: get_all_divisors and get_lowest_divisor. The main function should call get_all_divisors, which in turn has a loop: get_all_divisors calls get_lowest_divisor repeatedly, each time replacing n with n/i, where i is the divisor that was found. If n itself is returned, then the number is prime, and the loop should stop.)

Right off the bat, look at lines 45 and 46.

Line 45 -- Print i.
Line 46 -- Check to see if i is prime. It isn't. Uh oh. How do I un-print?

Line 46 -- Calls a non-void return type function and... does absolutely nothing with the return value.


Spec...

If n itself is returned, then the number is prime, and the loop should stop.

See last point. How do I know whether "n itself is returned"?

My question is. how do i calculate the prime factors like if I enter 30 the response will be: 2, 3, 5.

I have a program that works using recursive functions but the exercise is asking to modify it without recursion.

1) Throw away the program that uses recursion.
2) Figure out how to calculate the prime factors on paper.
3) Write down the steps used. Test the steps with many values.
4) Convert those steps into code.

Do not skip any of the above steps. Especially #3.

thanks waltp

I'll crack on with it and post the results

wait?? So what is your program supposed to do???

That's a tough program. I might wanna try doing that.

it is an exercise where use inputs number and then output is all prime factors, eg

input = 30
output is : 2, 3, 5

input = 100,
output is : 2, 2, 5, 5

now then i think i have cracked it.....

if u dont want a spoiler look away now.....

if u find any bugs please let me know :D ;)

#include <iostream>
#include <cmath>
using namespace std;

void getAllDivisors(int n); // this function calls getLowestDivisor and then divides n by returned value or if prime returns n
int getLowestDivisor (int x); // this function checks for divisibilty then returns lowest divisor or 0

int main()

{
	int n;
	system("clear");	// clears console on mac os x terminal
	
	while (true) {
		cout << "Enter a number (0 = exit) and press ENTER: ";
		cin >> n;
		if (n == 0)  // if number entered is n then break while loop and return 0 to end program
			break;
		getAllDivisors(n); // calls getALLDivisors function
		cout << "\n\n";
		}
	cout << endl;
	return 0;  // return 0 and end program

}

void getAllDivisors (int n)  // this function calls getLowestDivisor and then divides n by returned value

{
	while (true) {  // start infinite loop
		
		if (getLowestDivisor(n) == 0) {  // if getLowestDivisor returns 0 then
			cout << n;					// print n
			return;						// end loop and return to main()
		}
		cout << getLowestDivisor(n) << ", ";	// print lowest divisor found in getLowestDivisor function
		n = n / getLowestDivisor(n);
	}
}

int getLowestDivisor (int x)  // this function checks for divisibilty then returns lowest divisor or if number is prime it returns 0

{
	int sqRootOfX = sqrt(x); // variable is square root of x
	
	for (int i = 2; i <= sqRootOfX; i++)		// for loop 2 to sqroot of x
		{
			if (x % i == 0)			// if i divides equal then return i
				return i;
		}
		return 0;				// else return 0
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.