473,325 Members | 2,792 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,325 software developers and data experts.

Finding maximum value of a data type

I was trying the program to calculate the factorial of a number. The program is to identify the value of number at the which the factorial crosses the range of the data type. How should i do this? Please help me for the logic. I want to accommodate all kinds of data types.

Thanks.
Oct 1 '15 #1

✓ answered by weaknessforcats

In C just look in <limits.h>.

In C++ look in <limits>. Learn how to use he numeric_limit class template.

3 1840
weaknessforcats
9,208 Expert Mod 8TB
In C just look in <limits.h>.

In C++ look in <limits>. Learn how to use he numeric_limit class template.
Oct 1 '15 #2
I made the following attempt, but if fails
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<limits.h>
  3. int main(void)
  4. {
  5.    int number=0;
  6.    unsigned short factorial=1;
  7.    printf("Enter the number to calculate factorial\n");
  8.    scanf("%d",&number);
  9.    printf("us= %d\n",sizeof(unsigned short));
  10.    while(number>1)
  11.    {
  12.        factorial *= number;
  13.        if(factorial > USHRT_MAX)
  14.        {
  15.          break;
  16.        }
  17.        number -= 1;
  18.        printf("%d\n",factorial);
  19.    }
  20.    return 0;
  21. }
  22.  
And the output looks something like this
Expand|Select|Wrap|Line Numbers
  1. Enter the number to calculate factorial
  2. 9
  3. us= 2
  4. 9
  5. 72
  6. 504
  7. 3024
  8. 15120
  9. 60480
  10. 50368
  11. 35200
  12. Process returned 0 (0x0)   execution time : 1.874 s
  13. Press any key to continue.
  14.  
After 60480 the value of factorial crosses the range. How to identify this?
Oct 2 '15 #3
weaknessforcats
9,208 Expert Mod 8TB
This code:

Expand|Select|Wrap|Line Numbers
  1. while(number>1)
  2.    {
  3.        factorial *= number;
  4.        if(factorial > USHRT_MAX)
  5.        {
  6.          break;
  7.        }
  8. etc...
doesn't work because factorial has changed before it is tested to see if the max is exceeded.

You have to test before factorial is changed. Therefore, check before you multiply. The room left is USHRT_MAX - factorial. Next check to see how many factorial fit in the room by doing (USHRT_MAX - factorial)/factorial. If the number of factorials left is greater than the number you are going to multiply factorial by, then the new amount will fit:

Expand|Select|Wrap|Line Numbers
  1.     int count = 1;
  2.     while(count <= number)
  3.    {
  4.        if (USHRT_MAX - factorial)/ factorial) > count)
  5.        {
  6.           factorial *= count;
  7.        }
  8.        else
  9.        {
  10.            break;
  11.        }
  12.               ++count;
  13.        printf("%d\n",factorial);
  14.    }
  15.  
Also see that I use the loop counter to do the multiply rather than counting down.
Oct 2 '15 #4

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

Similar topics

21
by: Batista, Facundo | last post by:
Here I send it. Suggestions and all kinds of recomendations are more than welcomed. If it all goes ok, it'll be a PEP when I finish writing/modifying the code. Thank you. .. Facundo
29
by: garyusenet | last post by:
I'm trying to investigate the maximum size of different variable types. I'm using INT as my starting variable for exploration. I know that the maximum number that the int variable can take is:...
22
by: subramanian100in | last post by:
Consider the following program #include <limits.h> #include <stddef.h> int main(void) { size_t size; size_t bytes = sizeof(size_t);
4
by: Felix Kater | last post by:
Hi, when I use something like int Shift= 3; long Value= 1 << Shift; What is the data type of the const value '1' here? In other terms: What is the possible maximum of 'Shift' here?
6
by: =?Utf-8?B?U2hhcm9u?= | last post by:
I'm using the VScrollBar and set it as follow: m_vScrollBar.Minimum = -19602; m_vScrollBar.Maximum = 0; m_vScrollBar.SmallChange = 1; m_vScrollBar.LargeChange = 1089; m_vScrollBar.Value =...
15
by: timothytoe | last post by:
Situation: I have an array of objects. I want to find the maximum value of a given numeric value that exists in each of the objects. Suppose the array of objects is called "stat" and the numeric...
0
by: Fredrik Lundh | last post by:
maurizio wrote: What kind of file is it? Did you pick numpy because you want to do matrix operations (beyond just finding a maximum value), or was it just the first thing you stumbled upon...
1
by: maurizio | last post by:
thank you for your answer actually i've to do some statistics (maximum,minimum,mean,standard deviation,....) of a file of data in which each column is a particular type of data. (the file is a tab...
1
by: =?Utf-8?B?SGlwSG9wcGVy?= | last post by:
Hi, What's the Minimum and Maximum value for RangeValidator currency type? For example form -99,999,999,999,999 to 99,999,999,999,999? -- HipHopper
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.