By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,679 Members | 2,602 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,679 IT Pros & Developers. It's quick & easy.

Formatting numbers with a given number of decimal places

P: 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'

Thanxxxx in advance.
Feb 13 '07 #1
Share this Question
Share on Google+
17 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
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'

Thanxxxx in advance.
What did you get when you tried this? Can you post your code?
Feb 13 '07 #2

P: 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
Expert Mod 2.5K+
P: 4,677
Are you writing this in C or C++?
Feb 13 '07 #4

P: 24
I am writing this program in C++.
Feb 13 '07 #5

sicarie
Expert Mod 2.5K+
P: 4,677
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
Expert Mod 2.5K+
P: 4,677
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

P: 24
Ok so what should I do now?
Feb 13 '07 #8

P: 24
Its still giving errors.
Feb 13 '07 #9

sicarie
Expert Mod 2.5K+
P: 4,677
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
Expert 5K+
P: 5,000
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

P: 24
Thank you very much its working.
Feb 13 '07 #12

sicarie
Expert Mod 2.5K+
P: 4,677
Thank you very much its working.
Thanks RedSon.
Feb 13 '07 #13

P: 24
Can you please tell me what you did in the format( ) part of the program.
Feb 13 '07 #14

RedSon
Expert 5K+
P: 5,000
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
Expert 2.5K+
P: 3,652
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
Expert Mod 2.5K+
P: 4,677
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
Expert 100+
P: 463
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

Post your reply

Sign in to post your reply or Sign up for a free account.