#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! //} }
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; }
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; }
#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; }
#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; }
#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(); }
cout<<"\nthe squared digit length of n1 is\n"
cout<<"\nthe squared digit length of n2 is\n"
| DaniWeb Message | |
| Cancel Changes | |