Stuck with a couple exercises

#1 Get the "name and age" example to run. Then, modify it to write out the age in months: read the input in years and multiply (using the * operator) by 12. Read the age into a double to allow for children who can be very proud of being five and a half years old rather than just five.

My current code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;

int main()
{
    cout << "Please enter your name and age:\n";
    string name;
    double age;
    cin >> name >> age;
    age = age * 12;
    cout << "Hello, " << name << " (age " << age << ")\n";
    return 0;
}


So here I ask for the name and age. I define the string and double int. Double for people who are .5 years older or anyone that uses '.'. They enter their name and age. I use the formula age = age * 12. Is this code correct?

#2 Get this little program to run. Then, modify it to read an int rather than a double. Note that sqrt() is not defined for an int so assign n to a double and take sqrt() of that. Also, "exercise" some other operations. Note that for ints / is integer division and % is remainder (modulo), so that 5/2 is 2 and (and not 2.5 or 3) and 5%2 is 1. The defintions of integer *, /, and % guarantee that for two positive ints a and b we have a/b * b + a%b == a.

Code in question:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <math.h>
using namespace std;

int main()
{
    cout << "Please enter a double point value: \n";
    double n;
    cin >> n;
    cout << "n== " << n
         << "\nn+1== " << n+1
         << "\nthree times n == " << 3*n
         << "\ntwice n == " << n+n
         << "\nnsquared == " << n*n
         << "\nhalf of n == " << n/2
         << "\nsquare root of n == " << sqrt(n)
         << '\n'; // another name for newline ("end of line") in output
    return 0;
}


The code I'm working on to solve the question:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <math.h>
using namespace std;

int main()
{
    cout << "Please enter an integer value: \n";
    int n;
    cin >> n;
    cout << "n== " << n
         << "\nn+1== " << n+1
         << "\nthree times n == " << 3*n
         << "\ntwice n == " << n+n
         << "\nnsquared == " << n*n
         << "\nhalf of n == " << n/2
         << "\nsquare root of n == " << sqrt(n)
         << '\n'; // another name for newline ("end of line") in output
    return 0;
}


I'm confused on what it means by "Note that sqrt is not defined for an int so assign n to a double and take sqrt() of that." I dont understand what the last part is asking for either where it says " The defintions of integer *, /, and % guarantee that for two positive ints a and b we have a/b * b + a%b == a."

Also all of these exercises are from the Programming and Principles book by Stroustrup 2nd ed and I cant find the solutions on his website.
Last edited on
closed account (j3Rz8vqX)
The book was regarding old syntax/regulations.
It has been dealt with since:
inline double sqrt(int x) { return sqrt(double(x)); } // to match C++0x

http://stackoverflow.com/questions/19613191/in-c-why-does-sqrt-work-fine-on-an-int-variable-if-it-is-not-defined-for-an

Topic archived. No new replies allowed.