Hello

In my program, the function largest_prime_factor is not being called/working? Can you tell me what may be wrong?

The function largest_prime_factor, should return the largest prime number of the variable n5 (n5 = n3 + n4).

Plus if you see any other errors or maybe a better way of doing any part of my coding I would be very greatful :D ; especially with a code example

#include <iostream>
#include <cmath>

using namespace std;

// The precondition statement indicates what
// must be true before the function is called.
// The postcondition statement indicates what
// will be true when the function finishes its work.

bool largest_prime_factor(int b);
// precondition: a > 0
// postcondition: the function returns the largest prime factor of a

int squared_digit_length(int a);
// precondition: a > 0
// postcondition: the function returns the squared digit length starting from a

// you can define other functions if necessary

int main()
{
	char ch1, ch2;
	int n1, n2, n3, n4, n5; // used for the 5 steps of the task

	cout << "Enter your first name and surname: " << flush;
	cin >> ch1;           // read the first letter of the first name
	cin.ignore(100, ' '); // ignore the rest characters in the first name
	cin >> ch2;           // read the first letter of the surname

	n1 = (int)ch1;
	n2 = (int)ch2;
	n3 = squared_digit_length(n1);
	n4 = squared_digit_length(n2);
	n5 = n4 + n5;

	cout << n1 << n2 << endl;
	squared_digit_length(n1);
	squared_digit_length(n2);
	largest_prime_factor(n5);

	return 0;
}

int squared_digit_length(int a)
{

	int ones,tens, hund;
	double dones, dtens, dhund;
	int co = 0;

	while (a != 4)
	{

		if (a >= 100)
			tens= (a/10)%10;
		else tens= a/10;

		ones= a%10;
		hund= a/100;
		dhund = pow((double)hund,2);
		dtens = pow((double)tens,2);
		dones = pow((double)ones,2);
		co++;
		a= (dhund + dones + dtens);
		cout << "Hundreds= " << hund << " Tens= " << tens;
		cout << " Ones= " << ones;
		cout << " Count= " << co << " n1= "<< a << endl;

	}
	cout << "n3 =" << co << endl;
}

bool largest_prime_factor(int b)
{
	bool p = true;

	if (b == 1)
		return false;
	else if (b == 2)
		return true;
	else if (b % 2 == 0)
		return false;

	for (int i = 2; i * i <= b; i++)
	{
		if (b % i == 0) p = false;
	}

	return p;
}
Member Avatar for r.stiltskin

I didn't try compiling/running it, but it seems to me that it might take a long time to get out the first call to squared_digit_length() unless num1 equals 2, and that won't happen unless the user's name begins with the non-printing char '\2' which is highly unlikely.

Suppose the first initial is 'A' which becomes 65. Tens = 6. Ones = 5. Then a becomes 36+25=61. Tens = 6. Ones = 1. a becomes 36+1=37. Tens=3. Ones=7. a becomes 9+49=58. Tens=5. Ones=8. and so on....

What is that function supposed to do anyway?

There is no return statement in squared_digit_length.

Hi

concerning the return function in Square digit... function, I did have it in but that wasn't returning anything so I did cout instead, the same thing is happening for prime ... function also, any ideas why?

Plus wih the square digit.... function, its role is to split a number, square them then add them, and continue to do so until the result is either 4 or 1, why, meh? lol its just a requirement of the assignment I am doing :)

Member Avatar for r.stiltskin

concerning the return function in Square digit... function, I did have it in but that wasn't returning anything so I did cout instead, the same thing is happening for prime ... function also, any ideas why?

That's easy - squared-digit-length() isn't returning anything because you removed the return statement. And even if you restore the return statement, sometimes it won't return because, depending on the argument passed to it, it can run forever without exiting the while loop. You should be able to see that by looking at the output of the cout statements that you added in the loop.

The cout statement that you placed instead of the return statement isn't particularly informative because you're already printing the counter inside the loop.

And if you think about the output when it runs infinitely you will see why that is happening -- and the answer is already contained in the second part of your answer to me.

That's easy - squared-digit-length() isn't returning anything because you removed the return statement. And even if you restore the return statement, sometimes it won't return because, depending on the argument passed to it, it can run forever without exiting the while loop. You should be able to see that by looking at the output of the cout statements that you added in the loop.

The cout statement that you placed instead of the return statement isn't particularly informative because you're already printing the counter inside the loop.

And if you think about the output when it runs infinitely you will see why that is happening -- and the answer is already contained in the second part of your answer to me.

No, if you run the program you will see that square...digits works perfectly ( in that it displays/returns the counter/n3/n4 value) with the cout statement in there.
The function not working is Prime... fucntion, either with cout << p or just return p, nothing is being returned when its is being run? I dont know why, or how to fix that?

Member Avatar for r.stiltskin

No, if you run the program you will see that square...digits works perfectly ...

Try running it with
> Enter your first name and surname: alec baldwin

Member Avatar for r.stiltskin

As far as largest_prime_factor() is concerned, a few observations:

- largest_prime_factor() returns a value, but your main() ignores the return value. If you want to use the return value, main should call the function with: some_var = largest_prime_factor( n5 ); - the return type of your largest_prime_factor() is bool. This makes no sense. The largest prime factor of an integer is an integer, not "true" or "false". You have not completely implemented the algorithm.

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.