469,631 Members | 1,666 Online

# Formatting numbers with a given number of decimal places 24
Can somone please, please give me the solution for the following problem. I need to submit it on Monday.

Write a global function called format, which formats numbers with a given number of decimal places. The function accepts a compulsory first argument of type double (the number to be formatted) and a second optional argument of type integer (the number of decimal places) and returns a string object containing the formatted text. If the second argument is missing, the formatting should assume two (2) decimal places. No user-interaction code is allowed in this function.
Write the main function shown below as a test for your function.
Expand|Select|Wrap|Line Numbers
1. #include<iostream.h>
2. int main()
3. {
4.   double n1 = 1.24, n2 = 1.25, n3 = 3456.67953;
5.   cout << "'" << format( n1, 1 ) << "'" << endl;
6.   cout << "'" << format( n2, 1 ) << "'" << endl;
7.   cout << "'" << format( n3 )    << "'" << endl;
8.   cout << "'" << format( n3, 3 ) << "'" << endl;
9.   return 0;
10. }
11.
If the format function is written correctly, the above test should output
'1.2'
'1.3'
'3456.68'
'3456.680'

Feb 13 '07 #1
17 4958 sicarie
4,677 Expert Mod 4TB
Can somone please, please give me the solution for the following problem. I need to submit it on Monday.

Write a global function called format, which formats numbers with a given number of decimal places. The function accepts a compulsory first argument of type double (the number to be formatted) and a second optional argument of type integer (the number of decimal places) and returns a string object containing the formatted text. If the second argument is missing, the formatting should assume two (2) decimal places. No user-interaction code is allowed in this function.
Write the main function shown below as a test for your function.
Expand|Select|Wrap|Line Numbers
1. #include<iostream.h>
2. int main()
3. {
4.   double n1 = 1.24, n2 = 1.25, n3 = 3456.67953;
5.   cout << "'" << format( n1, 1 ) << "'" << endl;
6.   cout << "'" << format( n2, 1 ) << "'" << endl;
7.   cout << "'" << format( n3 )    << "'" << endl;
8.   cout << "'" << format( n3, 3 ) << "'" << endl;
9.   return 0;
10. }
11.
If the format function is written correctly, the above test should output
'1.2'
'1.3'
'3456.68'
'3456.680'

What did you get when you tried this? Can you post your code?
Feb 13 '07 #2
scan87
24 I wrote the global function but I tried to compile it the program is giving errors and the full program code is as follows:

Expand|Select|Wrap|Line Numbers
1. #include <iostream.h>
2. #include<math.h>
3. double format(double,int);    // function prototype
4. int main()
5. {
6.       double n1 = 1.24, n2 = 1.25, n3 = 3456.67953;
7.       cout<<"'"<<format(n1,1)<<"'"<<endl;
8.       cout<<"'"<<format(n2,1)<< "'"<<endl;
9.       cout<<"'"<<format(n3)<< "'"<<endl;
10.       cout<<"'"<<format(n3,3)<< "'"<<endl;
11.       return 0;
12. }
13.
14. double format(double val,int n)
15. {
16.     double mult=pow(10,n);
17.     long int ans=(val*mult);
18.     double res=ans/mult;
19.     return res;
20. }
21.
And the errors are:
format.cpp: In function âint main()â:
format.cpp:7: error: âformatâ was not declared in this scope
format.cpp: In function âdouble format(double, int)â:
format.cpp:15: error: âpowâ was not declared in this scope
format.cpp:16: warning: converting to âlong intâ from âdoubleâ
Feb 13 '07 #3
sicarie
4,677 Expert Mod 4TB
Are you writing this in C or C++?
Feb 13 '07 #4
scan87
24 I am writing this program in C++.
Feb 13 '07 #5
sicarie
4,677 Expert Mod 4TB
Updated minor bits of the code for readability.
Expand|Select|Wrap|Line Numbers
1. #include <iostream>
2. #include<math.h>
3. using namespace std;
4. double format(double val, int n);       // function prototype
5. int main()
6. {
7.         double n1 = 1.24, n2 = 1.25, n3 = 3456.67953;
8.         cout<<"'"<<format(n1,1)<<"'"<<endl;
9.         cout<<"'"<<format(n2,1)<< "'"<<endl;
10.         cout<<"'"<<format(n3)<< "'"<<endl;
11.         cout<<"'"<<format(n3,3)<< "'"<<endl;
12.         return 0;
13. }
14.
15. double format(double val,int n)
16. {
17.         double mult, ans, res;
18.         mult=pow(10,n);
19.         ans=(val*mult);
20.         res=ans/mult;
21.         return res;
22. }
23.
24.
So, one thing I see is line ten when you send format only n3. Your function has to pass this test, right? It is one your teacher wrote? Because your format() does not handle this.
Feb 13 '07 #6
sicarie
4,677 Expert Mod 4TB
And what I meant to ask was: can you pass it 0, or does it have to conform strictly to that test file?
Feb 13 '07 #7
scan87
24 Ok so what should I do now?
Feb 13 '07 #8
scan87
24 Its still giving errors.
Feb 13 '07 #9
sicarie
4,677 Expert Mod 4TB
Its still giving errors.
Can you explain to me what each line of your format function does? (To make sure the algorithm to set the decimal precision is correct).
Feb 13 '07 #10
RedSon
5,000 Expert 4TB
Expand|Select|Wrap|Line Numbers
1.
2. #include <iostream>
3. #include<math.h>
4. using namespace std;
5. double format(double val, int n);       // function prototype
6. int main()
7. {
8.     double n1 = 1.24, n2 = 1.25, n3 = 3456.67953;
9.     cout << "'" << format(n1,1) << "'" << endl;
10.     cout << "'" << format(n2,1) << "'" << endl;
11.     cout << "'" << format(n3,0) << "'" << endl;
12.     cout << "'" << format(n3,3) << "'" << endl;
13.     return 0;
14. }
15.
16. double format(double val,int n)
17. {
18.     double mult, ans, res;
19.     mult = ::pow(val, n);
20.     ans = (val*mult);
21.     res = ans/mult;
22.     return res;
23. }
24.
I fixed some stuff, and now it compiles. See if this works.
Feb 13 '07 #11
scan87
24 Thank you very much its working.
Feb 13 '07 #12
sicarie
4,677 Expert Mod 4TB
Thank you very much its working.
Thanks RedSon.
Feb 13 '07 #13
scan87
24 Can you please tell me what you did in the format( ) part of the program.
Feb 13 '07 #14
RedSon
5,000 Expert 4TB
Can you please tell me what you did in the format( ) part of the program.
I added the scope resolution operator before pow the "::" and also changed your method call to have two arguments.
Feb 13 '07 #15
Ganon11
3,652 Expert 2GB
The function accepts a compulsory first argument of type double (the number to be formatted) and a second optional argument of type integer (the number of decimal places) and returns a string object containing the formatted text. If the second argument is missing, the formatting should assume two (2) decimal places.
So far, people have changed your 3rd format( ) call to be format(n3, 0), but according to the assignment, the second argument is optional - thus, format(n3) should function. In order for this to work, in your function prototype, you must declare a default value for n, like this:

Expand|Select|Wrap|Line Numbers
1. double format(double val, int n = 2);
If no value is given for n in the call, then then program will assume that a 2 was passed. If a value is provided for n, then the program ignores the n = 2 and takes the given n.
Feb 13 '07 #16
sicarie
4,677 Expert Mod 4TB
So far, people have changed your 3rd format( ) call to be format(n3, 0), but according to the assignment, the second argument is optional - thus, format(n3) should function. In order for this to work, in your function prototype, you must declare a default value for n, like this:

Expand|Select|Wrap|Line Numbers
1. double format(double val, int n = 2);
If no value is given for n in the call, then then program will assume that a 2 was passed. If a value is provided for n, then the program ignores the n = 2 and takes the given n.
Thank you Ganon. I knew there was a way to do it, and have been trying to figure it out!
Feb 13 '07 #17
RRick
463 Expert 256MB
It appears that there is some kind of conflict between iostream and format. I'm not sure what it is, but gnu doesn't like it, either.

Try changing the name of format to something really different (like xxxformat) and see if that particular error goes away.
Feb 13 '07 #18

 22 posts views Thread by Allen Thompson | last post: by 2 posts views Thread by johkar | last post: by 6 posts views Thread by Stropher | last post: by 9 posts views Thread by Markus | last post: by 7 posts views Thread by Gerard Flanagan | last post: by 14 posts views Thread by Scott M. | last post: by 3 posts views Thread by Nathan Sokalski | last post: by 9 posts views Thread by john coltrane | last post: by 2 posts views Thread by Pittaman | last post: by reply views Thread by billypeterson | last post: by reply views Thread by strativab | last post: by reply views Thread by eddparker01 | last post: by 1 post views Thread by Simmer | last post: by reply views Thread by gheharukoh7 | last post: by reply views Thread by tieutu2004 | last post: by 2 posts views Thread by JamesNapier | last post: by 2 posts views Thread by Seth Schrock | last post: by reply views Thread by isladogs | last post: by