I need some advice from people who know what they're are talking about. The question I am struggling with is below and my attempt is below that. Can you tell where I'm going wrong? Thanks.

I'm trying to acheive this:

(A) Read the first name and the surname of a person.
Calculate and output n1 = the integer representing the first letter (counting
from the left) of the first name in the ASCII table.
For example, if the first name is “James”, then n1=74 (i.e. the value representing ‘J’ is
74 in the ASCII table).
Note that there is no restriction that the first letter entered must be a capital letter.
(B) Calculate and display n2 = the integer representing the first letter of the
surname in the ASCII table.
For example, if the surname is “Bond”, then n2 = 66 (i.e. the value representing ‘B’ is
66 in the ASCII table).
(C) Calculate and display n3 = the squared digit length of n1.
The following process determines the squared digit length of an integer. Take any
integer and add up the squares of its digits. This will give you another integer. Repeat
this procedure until the number you end up with is 1 or 4. The number of times this
process has to be repeated before it gets to 1 or 4 is the squared digit length. For
example, if we start with 85, we get:

82 + 52 = 89
82 + 92 = 145
12 + 42 + 52 = 42
42 + 22 = 20
22 + 02 = 4

This process shows that the squared digit length of 85 is 5.
According to the experts, this process will always eventually reach either 1 or 4. The
squared digit length of 1 and 4 is zero, since we don't actually have to apply the
process to them to reach the stopping condition (interestingly, though, if we did apply
the process, to 1, we would keep getting 1, but if we applied it to 4, we would get a
repeating sequence: 4, 16, 37, 58, 89, 145, 42, 20, 4).
(D) Calculate and display n4 = the squared digit length of n2.
(E) Calculate and display n5 = the largest prime factor of n3+n4.
A prime factor of integer n is a factor of n which is a prime number. A prime number
is any integer greater than 1 and only divisible by itself and 1 (e.g. 2, 3, 5, 7, 11, 13,
17 etc). For example, 3 is the largest prime factor of 27 and 7 is the largest prime
factor of 49.

And this is what I have so far:

#include <iostream>
#include <cmath>

using namespace std;


bool largest_prime_factor(int b);
int squared_digit_length(int a);


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 result = 0;
    while (a)
    {
    int t = a % 10;
    result += t * t;
    a /= 10;
    }
    return result;
    }




bool largest_prime_factor(int a)
{
    int main()
    {

    int n3,n4,n5;
    cout << "n3=" ;
    cin >> n3 ;
    cout << "n4= " ;
    cin >> n4;
    n5 = n3+n4;
    cout << largest_prime_factor(n5) << endl;
    return 0;
    }

}

Recommended Answers

All 6 Replies

Wow, that post was a nightmare to read!
Can you use code tags in future please?!

The maths in this looked terrible at first glance too...How does 82 + 52 = 89??
It took me a while to work out that you meant, but I got there in the end!
85 becomes (8*8) + (5*5) = 89! Now I see!

Anyways, I've posted some comments in your code:

#include <iostream>
#include <cmath>

using namespace std;

// shouldn't this be returning an int??
bool largest_prime_factor(int b);  

int squared_digit_length(int a);

// This bit looks ok at a brief glance....
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;

	// until we get here...

	//  would it be worth adding "n1 = " and " n2= " to the cout below?
	cout << n1 << n2 << endl;
	//  do you also need to cout the squared digit lengths of n1 and n2? (values of n3 and n4)
	
	// Why are you calling squared_digit_length on n1 and n2 here?
	// you already have the relevant values stored in n3 and n4. Also you aren't even assigning the returned values to an identifier here!
	squared_digit_length(n1);
	squared_digit_length(n2);
	
	// you should also ensure you set up an int variable here to store the value returned by this function call
	largest_prime_factor(n5);

	return 0;
}

// This bit looks ok, you're passing in a value, perfoming a calculation and returning the result...
// But the algorithm you're using won't quite do what you want it to do! see your description between steps C and D
int squared_digit_length(int a)
{
	int result = 0;
	while (a)
	{
		int t = a % 10;
		result += t * t;
		a /= 10;
	}
	return result;
}


// But What is this block of code about??
// This function doesn't do anything practical...
// You aren't using the value that gets passed into the function for anything, your return type doesn't match the declared return type (bool)
// This function looks like it should return an int value.
// Plus the function doesn't actually do anything other than ask the user to input values for n3 and n4 before calling itself in the cout.
// Calling this function once will cause the function to recursively call itself in the final cout.
// also in the declaration of the function (at the top of the file) doesn't quite match the definition below:
bool largest_prime_factor(int a) 
{
	//int main()  - This bit shouldn't be in here!
	//{
	int n3,n4,n5;
	cout << "n3=" ;
	cin >> n3 ;
	cout << "n4= " ;
	cin >> n4;
	n5 = n3+n4;
	cout << largest_prime_factor(n5) << endl;
	return 0; // should return true or false with bool return type..
		// however, this function is supposed to be calculating and returning the largest prime factor of the passsed in number....
		// so you should change the return type of the function to int and return an int!
	//}
}

I recommend that you strip out the contents of the largest_prime_factor function and look up an algorithm for the desired functionality..
so for now:

int largest_prime_factor(int a)
{
	// todo: insert code for largest prime factor algorithm here
       //(return 0 for now until new code is added) 
	return 0; 
}

Also, as mentioned in the code comments your squared_digit_length function does not do what it's supposed to be doing. It should do something more like this:

int squared_digit_length(int a)
{	
	// val is a number we'll be recursively manipulating
	// initially assign it the passed in value (a).
	// result will store the result of the additions of the squared digits 
	// digit_length will count the number of iterations before the result is equal to 1 or 4 
	int val = a, result=0, digit_length=0;
	
        // loop until result is 1 or 4
	while(result!=1 && result!=4)
	{
		// split value of val into hundreds, tens and units
		int hundreds = val / 100;
		int remainder = val % 100;
		int tens = remainder / 10;
		int units = remainder % 10;
		
		// calculate result by adding the squares of the hundreds, tens and units....
		result = (hundreds*hundreds) + (tens*tens) + (units*units);
		val = result; // update val with the value of result		   
                digit_length++; // increment digit_length.
	}
	// squared digit length is the number of repeats of the process, so return digit_length...Not result!
	return digit_length; 
}

So I hope that's at least part of your problem solved.
I'll leave you to try to work out some kind of algorithm for the largest_prime_factor calculation!

Cheers for now,
Jas.

i have quickly read it and i think this is what you want

#include <conio>
#inlcude <iostream>
void main()
{
char a[50];
int b,c,r,s,t,i=0,h,z;
///////////////////////////////////// a part
cout<<"enter a name";
cin>>a;
b=int(a[0]);
cout<<"the ascii value of first letter of first name is"
<<b;
//////////////////////////////////// b part
while(a[i]!=' ')   //assuming that sir name is after a space
{
i++;
}
i++;
c=int(a[i]);
cout<<"the ascii value of first letter of sir name is"
<<c;
///////////////////////////////////// c part
z=b;
r=0;s=0;t=0;
i=0;
while(1)
{
while(1)
{
if(z>=100)
{r=(z%100)*(z%100);
z/=100;}
else if(z<100)
{s=(z%10)*(z%10);
z/=10}
else if(z<10)
{t=z*z;
break;}
}
z=r+s+t;
r=0;s=0;t=0;
i++;
if((z==1)||(z==4))
{break;}
}
cout<<"squared digit length of the ascii value of first letter of first name is"
<<i;
///////////////////////////////////// d part
z=c;
r=0;s=0;t=0;
h=0;
while(1)
{
while(1)
{
if(z>=100)
{r=(z%100)*(z%100);
z/=100;}
else if(z<100)
{s=(z%10)*(z%10);
z/=10}
else if(z<10)
{t=z*z;
break;}
}
z=r+s+t;
r=0;s=0;t=0;
h++;
if((z==1)||(z==4))
{break;}
}
cout<<"squared digit length of the ascii value of first letter of sir name is"
<<h;
//////////////////////////////////// e part
i=i+h;
h=2;
while(i>1)
{
if(i%h<0)
{h++;}
else
{i/=h;}
}
cout<<"the largest prime factor in this case is"
<<h;
}

My previous post was a little messy...so apologies for that....

Below is a tidied up version of your code....This is how I would've tackled the problem...

#include <iostream>
#include <cmath>
#include <string>

using namespace std;

int largest_prime_factor(int const &a);  
int squared_digit_length(int const &a);

int main()
{
	// store forename and surname in std::strings...Safer than using char arrays!
	string forename, surname;
	int n1,n2,n3,n4,n5;
	cout << "Enter your forename and surname : ";
	cin >> forename >> surname;

	// TODO: do you need to perform any range checking on the users input?

	// assuming no range checking required....just use the int values of the first characters stored in
	// the strings 
	n1 = (int)forename[0];
	n2 = (int)surname[0];
	cout << endl << "Integer value for 1st letter of forename (" << forename[0] << ") = " << n1 << endl;
	cout << "Integer value for 1st letter of surname (" << surname[0] << ") = " << n2 << endl;

	// calculate and output the digit lengths...
	n3 = squared_digit_length(n1);
	n4 = squared_digit_length(n2);
	cout << "squared digit length of " << n1 << " is " << n3 << endl;
	cout << "squared digit length of " << n2 << " is " << n4 << endl;

	// calculate and output the largest prime factor.
	n5=largest_prime_factor(n3+n4);
	cout << "The largest prime factor is: " << n5 << endl;
	
	return 0;
}

// Calculate squared digit length of passed-in value.
// NOTE: passing a constant reference to an int...Probably overkill for a simple example like this!
// Using constant references ensures that the passed in value remains constant and is not altered in any way!
// It's just good practice!
// If anything in the function attempts to modify the passed in value, the compiler will complain!
int squared_digit_length(int const &a)
{	
	// val is the number we'll be recursively manipulating
	// initially assign it the passed in value (a).
	// result will store the result of the additions of the squared digits 
	// digit_length will count the number of iterations before the result is 1 or 4 
	int val = a, result=0, digit_length=0;
	
	// loop until result is 1 or 4
	while(result!=1 && result!=4)
	{
		// split value of val into hundreds, tens and units
		int hundreds = val / 100;
		int remainder = val % 100;
		int tens = remainder / 10;
		int units = remainder % 10;
		
		// calculate result by adding the squares of hundreds, tens and units....
		result = (hundreds*hundreds) + (tens*tens) + (units*units);
		val = result; // set val to the value of result
		digit_length++; // increment digit_length.
	}
	
	// squared digit length is the number of repeats of the process, so return digit_length...
	return digit_length; 
}

// calculate largest prime factor of passed-in int value
int largest_prime_factor(int const &a)
{
	//TODO: You'll need to find an appropriate algorithm to code for this section...
	// I'm not doing all of your homework for you! ;)
	// Perhaps you could try decyphering Threats cryptic post!
	return 0; 
}

As you can see I've left a couple of blanks for you to fill....Have fun!

Cheers for now,
Jas.

threat do you think you could write out that program using her variables instead to make it easier to read?

okay, i've refined my code, my first code is wrong, i made it in a hurry, the code below is simple, perfect and complete and uses your variable names

#include <conio>
#include <iostream>
#include <stdio>
#include <math>
//NOTE: if code gives error, add .h to the name of header files
//i've checked this code works perfect in borland c++
void main()
{
	char a[50];
	int n1,n2,n3=0,n4=0,n5=2,i=0,r,s,t,z;
///////////////////////////////////// a part
	cout<<"enter a name\n";
	gets(a);            //to input a character array containing spaces we use gets (header file: stdio)
	n1=int(a[0]);       //converts a character into its ascii code
	cout<<"the ascii value of first letter of first name is\n"
	    <<n1;
//////////////////////////////////// b part
	while(a[i]!=' ')   //assuming that sir name is after a space
	{
		i++;
	}
	i++;
	n2=int(a[i]);
	cout<<"\nthe ascii value of first letter of sir name is\n"
	    <<n2;
///////////////////////////////////// c part
	z=n1;
	while((z!=1)&&(z!=4))
	{
		r=pow(z%10,2);      //using power function for taking square (header file: math)
		z/=10;
		s=pow(z%10,2);
		z/=10;
		t=pow(z%10,2);
		z=r+s+t;
		n3++;
	}
	cout<<"\nthe squared digit length of n1 is\n"
	    <<n3;
///////////////////////////////////// d part
	z=n2;
	while((z!=1)&&(z!=4))
	{
		r=pow(z%10,2);
		z/=10;
		s=pow(z%10,2);
		z/=10;
		t=pow(z%10,2);
		z=r+s+t;
		n4++;
	}
	cout<<"\nthe squared digit length of n1 is\n"
	    <<n4;
//////////////////////////////////// e part
	z=n3+n4;
   while(z>1)
   {
		if(z%n5==0)     //when remainder is zero, it means n5 is one of the prime factor
   	{z/=n5;}
   	else
   	{n5++;}         //if n5 is not prime factor, then we move on to next number
   }
   cout<<"\nthe largest prime factor of n3+n4 is\n"
       <<n5;         //the last factor in prime factorization is always the largest
	getch();
}

ok, thats funny, just correct a typo in my last post at line 52:

cout<<"\nthe squared digit length of n1 is\n"

it is actually

cout<<"\nthe squared digit length of n2 is\n"
commented: Three posts just do do someone else's homework? C'mon, let them write their own code, with your *help* -3
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.