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. -
#include<iostream.h>
-
int main()
-
{
-
double n1 = 1.24, n2 = 1.25, n3 = 3456.67953;
-
cout << "'" << format( n1, 1 ) << "'" << endl;
-
cout << "'" << format( n2, 1 ) << "'" << endl;
-
cout << "'" << format( n3 ) << "'" << endl;
-
cout << "'" << format( n3, 3 ) << "'" << endl;
-
return 0;
-
}
-
If the format function is written correctly, the above test should output
'1.2'
'1.3'
'3456.68'
'3456.680'
Thanxxxx in advance.
17 5252
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. -
#include<iostream.h>
-
int main()
-
{
-
double n1 = 1.24, n2 = 1.25, n3 = 3456.67953;
-
cout << "'" << format( n1, 1 ) << "'" << endl;
-
cout << "'" << format( n2, 1 ) << "'" << endl;
-
cout << "'" << format( n3 ) << "'" << endl;
-
cout << "'" << format( n3, 3 ) << "'" << endl;
-
return 0;
-
}
-
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?
I wrote the global function but I tried to compile it the program is giving errors and the full program code is as follows: -
#include <iostream.h>
-
#include<math.h>
-
double format(double,int); // function prototype
-
int main()
-
{
-
double n1 = 1.24, n2 = 1.25, n3 = 3456.67953;
-
cout<<"'"<<format(n1,1)<<"'"<<endl;
-
cout<<"'"<<format(n2,1)<< "'"<<endl;
-
cout<<"'"<<format(n3)<< "'"<<endl;
-
cout<<"'"<<format(n3,3)<< "'"<<endl;
-
return 0;
-
}
-
-
double format(double val,int n)
-
{
-
double mult=pow(10,n);
-
long int ans=(val*mult);
-
double res=ans/mult;
-
return res;
-
}
-
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â
Are you writing this in C or C++?
I am writing this program in C++.
Updated minor bits of the code for readability. -
#include <iostream>
-
#include<math.h>
-
using namespace std;
-
double format(double val, int n); // function prototype
-
int main()
-
{
-
double n1 = 1.24, n2 = 1.25, n3 = 3456.67953;
-
cout<<"'"<<format(n1,1)<<"'"<<endl;
-
cout<<"'"<<format(n2,1)<< "'"<<endl;
-
cout<<"'"<<format(n3)<< "'"<<endl;
-
cout<<"'"<<format(n3,3)<< "'"<<endl;
-
return 0;
-
}
-
-
double format(double val,int n)
-
{
-
double mult, ans, res;
-
mult=pow(10,n);
-
ans=(val*mult);
-
res=ans/mult;
-
return res;
-
}
-
-
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.
And what I meant to ask was: can you pass it 0, or does it have to conform strictly to that test file?
Ok so what should I do now?
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).
-
-
#include <iostream>
-
#include<math.h>
-
using namespace std;
-
double format(double val, int n); // function prototype
-
int main()
-
{
-
double n1 = 1.24, n2 = 1.25, n3 = 3456.67953;
-
cout << "'" << format(n1,1) << "'" << endl;
-
cout << "'" << format(n2,1) << "'" << endl;
-
cout << "'" << format(n3,0) << "'" << endl;
-
cout << "'" << format(n3,3) << "'" << endl;
-
return 0;
-
}
-
-
double format(double val,int n)
-
{
-
double mult, ans, res;
-
mult = ::pow(val, n);
-
ans = (val*mult);
-
res = ans/mult;
-
return res;
-
}
-
I fixed some stuff, and now it compiles. See if this works.
Thank you very much its working.
Thank you very much its working.
Thanks RedSon.
Can you please tell me what you did in the format( ) part of the program.
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.
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: - 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.
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: - 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!
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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
|
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: ...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |