473,398 Members | 2,165 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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'

Thanxxxx in advance.
Feb 13 '07 #1
17 5252
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'

Thanxxxx in advance.
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

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

Similar topics

22
by: Allen Thompson | last post by:
Is there a script that will round off a number to a certain number of decimal places? -Allen Thompson
2
by: johkar | last post by:
I picked up the following script from an old post on this newsgroup. It works fine for numbers to two decimal places, but I have a need to format numbers to three decimal spaces from this: ...
6
by: Stropher | last post by:
Hi! I just want to update a column in a table having a value of a decimal number. Then I tried this via sql-query analyzer update tablepost set preis = 0.71 where idnumber = 50 It worked...
9
by: Markus | last post by:
Hi all, two questions on formatting numbers: - Given is a decimal/money datatype in SQL-Server, e.g. decimal(9,4), thus displaying a value like 13.2000 How can I prevent to display the...
7
by: Gerard Flanagan | last post by:
All would anyone happen to have code to generate Cutter Numbers: eg. http://www1.kfupm.edu.sa/library/cod-web/Cutter-numbers.htm or is anyone looking for something to do?-) (I'm under...
14
by: Scott M. | last post by:
Ok, this is driving me nuts... I am using VS.NET 2003 and trying to take an item out of a row in a loosely-typed dataset and place it in a label as a currency. As it is now, I am getting my...
3
by: Nathan Sokalski | last post by:
I am using databinding to populate a dropdownlist with numeric values. Some of the values have more decimal places than others, and I only want the minimal number of decimal places necessary...
9
by: john coltrane | last post by:
Is there way to create a formatted string in a similar that is similar to sprintf? The same for printing, printf? C,D,E,F,G,N,X for currency, decimal, exponential, fixed, general, numerical,...
2
Pittaman
by: Pittaman | last post by:
Hello I am creating some crystal reports (for visual studio 2005) based on the content of certain .NET objects. I'm doing this in .NET 2.0. For one of them I'm using a Cross-table to summarize...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.