recently i have got a c++ assignment from my school, and i m completely dont have any idea on this project.
hope some experts in here can help me out or give me some tips about it, thank you so much.
the question is below:
SCENARIO 1 : no input parameters
SCENARIO 2 : two or more command line parameters is an error and the only output should be "C".
SCENARIO 3 * The command line parameter must be an 8 bit unsigned integer,
if not the output must be "P".
* Given an integer n the output should be the result of the
calculation n!. All calculations should be done using
a standard signed integer ( 32 bit) and the result printed out
using cout then a line feed (\n).
If the calculation proves impossible the output should be "X".
Can anyone help me on this c++ factorial problem?(linux)?
The way the unsigned integer would be 8 bits would be to be an unsigned char. Realistically that only means your values range from 0 to 255. However, that is pretty irrelevant since n! will get much larger than a 32-bit signed integer can hold very quickly.
You really shouldn't be posting homework on some website to get an answer. Real-life programming problems are much more difficult than calculating factorials. If you can't do this you are in some trouble.
Reply:I am not going to work this out because I don't believe in doing other people's homework but I did notice a couple things about answer 2
Which basically solves part 1. Except that choosing 1000 as a limit is a bit arbitrary, also the integer variable f is defined but not initially set to anything, wouldn't that start it out at zero? If so, it would always out the number "0" because the only operation that acts on it is multiplication.
an 8-bit unsigned integer would be one between 0 and 255, if I'm not mistaken.
Reply:i am going to write the code in c using comment as// line ok.
main()
{
int n; //used to store the no whose u want factorial
cout%26lt;%26lt;"enter the no:";
cin%26gt;%26gt;n;
int f;int m=1000;
//m is used to bound the user to enter the no within the limit 1000
if(m%26lt;=1000)
{
for(int i=n;i%26gt;=1;i--)
{
f=f*i;
}
cout%26lt;%26lt;"The factorial is "%26lt;%26lt;n%26lt;%26lt;endl;
}
else
cout%26lt;%26lt;"X"; //for impossible output
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment