I am following the following function to calculate factorials of big numbers link, and I would like to understand a bit more why some things are happening...
#include<stdio.h>
#define MAX 10000
void factorialof(int);
void multiply(int);
int length = 0;
int fact[MAX];
int main(){
int num;
int i;
printf("Enter any integer number : ");
scanf("%d",&num);
fact[0]=1;
factorialof(num);
printf("Factorial is : ");
for(i=length;i>=0;i--){
printf("%d",fact[i]);
}
return 0;
}
void factorialof(int num){
int i;
for(i=2;i<=num;i++){
multiply(i);
}
}
void multiply(int num){
long i,r=0;
int arr[MAX];
for(i=0;i<=length;i++){
arr[i]=fact[i];
}
for(i=0;i<=length;i++){
fact[i] = (arr[i]*num + r)%10;
r = (arr[i]*num + r)/10;
//printf("%d ",r);
}
if(r!=0){
while(r!=0){
fact[i]=r%10;
r= r/10;
i++;
}
}
length = i-1;
}
My questions are:
What is the real meaning of the MAX constant? What does it mean if it's bigger or smaller?
I have found out that if I have a MAX = 10000 (as in the example), I can calculate up to 3250! If I try with 3251! I get a 'Abort trap: 6' message. Why is that number? Where does it come from?
Which would be the difference if I compile this code for a 32-bit machine with the flag -m32? Would it run he same as in 64-bit?
Thanks!
As Scott Hunter points out, MAX is the maximum number of elements in the fact and arr arrays, which means it's the maximum number of digits that can occur in the result before the program runs out of space.
Note that the code only uses MAX in its array declarations. Nowhere does it use MAX to determine whether or not it's trying to read from or write to memory beyond the end of those arrays. This is a Bad Thing™. Your "Abort trap: 6" error is almost certainly occurring because trying to compute 3251! is doing exactly that: using a too-large index with arr and fact.
To see the number of digits required for a given factorial, you can increase MAX (say, to 20,000) and replace the existing printf calls in main with something like this:
printf("Factorial requires %d digits.\n", length + 1);
Note that I use length + 1 because length isn't the number of digits by itself: rather, it's the index of the array position in fact that contains the most-significant digit of the result. If I try to compute 3251!, the output is:
Factorial requires 10008 digits.
This is eight digits more than you have available in fact with the default MAX value of 10,000. Once the program logic goes beyond the allocated space in the array, its behavior is undefined. You happen to be seeing the error "Abort trap: 6."
Interestingly, here's the output when I try to compute 3250!:
Factorial requires 10005 digits.
That's still too many for the program to behave reliably when MAX is set to 10,000, so the fact that your program calculates 3250! successfully might be surprising, but that's the nature of undefined behavior: maybe your program will produce the correct result, maybe it will crash, maybe it will become self-aware and launch its missiles against the targets in Russia (because it knows that the Russian counterattack will eliminate its enemies over here). Coding like this is not a good idea. If your program requires more space than it has available in order to complete the calculation, it should stop and display an appropriate error message rather than trying to continue what it's doing.
MAX is the number of elements in fact and arr; trying to access an element with an index >= MAX would be bad.
Error messages are often specific to the environment you are using, which you have provided no details for.
They are not the same, but the differences (for example, the size of pointers) should not affect this code in any discernible way.
Related
I need to make a program that can take numbers of up to 100 digits as input. No standard int datatype will be able to do that! I've never come across such a bizarre situation.
I don't get it at all. How am I supposed to solve this?
The question I'm working on is this:
A whole number will be given, and you have to make a program that will
determine whether it's an even or odd number.
Input Specification
In the first line, there will be an integer T denoting the number of
testcases. In the following T lines, a non-negative integer will be
given. The number can have a maximum of 100 digits.
Output Specification
For every whole number given, you will have to print whether it's odd
or even as output.
Can anyone guide me on how to solve the problem (if it is even possible to do so)?
The program will take a number as input and determine whether it's odd or even.
Read the input in a string (char [101]) and analyze only last digit to check whether number is odd or even. Rest of the digits are irrelevant for this task.
There is no standard numeric type guaranteed to hold that many digits. You need to store the value in a different way, e.g., as a string or other array. If you need to perform arithmetic on these numbers, you need to implement those operations for the types you use, or use some kind of arbitrary precision library.
(Tip: You also don't necessarily need the entire number for certain operations, e.g., you can tell whether it is even or odd by looking only at the last digit…)
The exercise is to determine whether a whole number of up to 100 digits is odd or even.
This does not require you to perform arbitrary arithmetic on the number, so if you need to handle numbers larger than the largest integer type on your system, you can treat them as a string of digits.
Whether it is even or odd only depends on the last digit.
To all those who took the time and effort to answer this question,
Thanks for the answers. And thanks for showing the way. I greatly appreciate the help!
The solution to the problem which I have coded is -
#include <stdio.h>
#include <string.h>
int main()
{
int T, i, j;
scanf("%d", &T);
for (i=1; i<=T; i++)
{
char N[101];
scanf("%s", N);
int k = strlen(N);
int p = N[k-1] - 48; //char to int conversion
if (p % 2 == 1)
{
printf("odd\n");
}
else
{
printf("even\n");
}
}
return 0;
}
the average is not correct for this code. How to solve the average with this code. I try all of things but every time its same. It shows last number average only.
#include<stdio.h>
void compute(int anumber,int *max,int *min,float *average,int count);
int main ()
{
int max=0,min=100;
float average=0;
int number,i=0;
printf("\nenter a number:");
scanf("%d",&number);
while(number>0){
i++;
compute(number,&max,&min,&average,i);
printf("\nenter a number:");
scanf("%d",&number);
}
printf("\naverage %.2f",average);
printf("\ncount %d",i);
printf("\nmin %d",min);
printf("\nmax %d",max);
return 0;
}
void compute(int anumber,int *max,int *min,float *average,int count)
{
float total;
total=total+anumber;
*average=total/count;
++count;
if(anumber>*max)
*max=anumber;
else if(anumber<*min)
*min=anumber;
}
Issue #1: Total and number are uninitialized.
In C no initialization of variables is done for you. What is the value of total after:
float total;
The answer: Whatever was in memory at the location it was allocated. It could 0, 20, 94, 15, 7, or -2.3 billion, unless you give it a value it has an arbitrary one. It's value is indeterminate.
total=total+anumber;
By adding anumber to an unknown number you have no idea what total will become.
Issue #2: You don't calculate total.
The total is calculated by taking all the numbers in a set and summing them.
I.E. With the numbers 2,4,6 the total is 2 + 4 + 6 which equals 12. The first thing you should note is the presence of repetition. The addition is a common aspect that can be abstracted. We normally abstract repetition into a loop i.e.
int i;
float total = 0;
for(i = 0; i <= 6; i+=2)
{
total += i;
}
You need a similar loop in your program.
Your compute function is void, it cannot return a value. There are a handful of ways to fix total so that you may calculate the average.
Define total in the global scope for example, this is done by moving it outside of any function body. You'll have to remove the definition of total from within your function, as the variable in the closest outer scope will be used.
Make total static, it will not be automatically disposed of between each function call. This allows you to pass in values and allow the total to remain.
Regardless of your choice you'll have to make some changes, as of now you are not actually calculating the average. Further details on the assignment or task you were given would benefit everyone. C has few keywords but many ways of using them, in order to fully answer your question we must know:
What is the program supposed to do? Anticipated input/output helps a lot. In order to come up with a solution you must first fully understand the problem. It can be a challenge to extract intended functionality even from well written and working code, it can be near impossible to do so with broken code.
Were you given specifications? Do you understand them? I see from your comment that you are not allowed to change function headers/prototypes, what other restrictions do you have?
One final note. I'm assuming this is a homework assignment, as someone who has taken many classes based around C programming and who has taught some myself I can guarantee you - You will get the most out of the exercise by receiving only the help that is necessary, strive to think about the problem as much as you can on your own as well.
Your variable "total" is local to the compute() function, so it does not contain the total of all the input numbers - it is as new variable each time compute() is called, which is every time through the loop.
If you want to process each number one at a time, you can find the total within the compute function, if you make total a global variable. Then the correct approach would be find the average once, by dividing that total by the number of values.
I have just started competitive programming in SPOJ.I'm confused from sometime why i'm getting runtime error in ideone.The question is:
A positive integer is called a palindrome if its representation in the decimal system is the same when read from left to right and from right to left. For a given positive integer K of not more than 1000000 digits, write the value of the smallest palindrome larger than K to output. Numbers are always displayed without leading zeros.
Input
The first line contains integer t, the number of test cases. Integers K are given in the next t lines.
Output
For each K, output the smallest palindrome larger than K.
Example
Input:
2
808
2133
Output:
818
2222
My program:
#include <stdio.h>
int main(void)
{
int t,i,reverse,same;
scanf("%d",&t); //t is no. of test cases
int num[t]; //num[t] is array of t elements
for(i=0;i<t;i++)
scanf("%d",&num[i]);
i=0; //since i will be equal to t therefore i is assigned to 0.
while(t--)
{
if(num[i]<=1000000)
{
while(num[i]++)
{
reverse=0;
same=num[i];
while(same>0)
{
reverse=reverse*10;
reverse=reverse+same%10;
same=same/10;
}
if(reverse==num[i])
printf("%d",reverse);
printf("\n");
if(reverse==num[i])
break;
}
}
i++;
}
return 0;
}
I don't know where i'm wrong.I'm sorry i'm asking this question may this question is asked by someone before.I tried to find the result but could not get the answer.Thankyou in advance and sorry for my bad english.
The question doesn't say that the number will be less than 1000000. It says that the number has less than 1 million digits. A number with a million digits looks like this
591875018734106743196734198673419067843196874398674319687431986743918674319867431986743198674319876341987643198764319876341987643198764319876431987643198763419876431987643198764319876139876...
You can't use scanf to read a number that has a million digits, and you can't store that number in an int.
The most likely reason for your error to occur is some memory fault. Keep in mind that online judges/compilers limit your available memory and if you try to allocate/use more memory than available, you get a runtime error. This also happens on your machine, but usually you have a lot more memory available for your program than in the case of online judges.
In your case, you could reduce the memory usage of your program by changing the data type of the num array from int to something like short or even char.
I have the following code to generate prime factors of a number. It is working fine with all the numbers except some. The error I am getting in these numbers is runtime error which is at line 5 and line 10, when trying to access 'factors' array.
and also why is it not giving error when accessing 'factors' array in line 15 and 18.
*This is working perfectly for numbers both greater and lesser than 48598496894, just not for 48598496894.
void getfactors(unsigned long long n){
unsigned long long *factors,i=0,k=0;
//array to store prime factors//
factors=(unsigned long long *)malloc(n*sizeof(unsigned long long));
//getting 2's which are factors of the number//
while(n%2==0){
factors[k++]=2; //line 5
n=n/2;
}
//getting other prime factors of the number//
for(i=3;i<=sqrt(n);i=i+2){
while(n%i==0){
factors[k++]=i; //line 10
n=n/i;
}
}
//last prime factor of number//
if(n>2)
factors[k++]=n; //line 15
printf("%d\n\n",k);
//printing all factors//
for(i=0;i<k;i++)
printf("%llu\n",factors[i]); //line 18
}
int main()
{
getfactors(48598496894);
return 0;
}
You should be checking that factors is not NULL after the malloc in case the allocation fails. You also need to call free to release the memory that you allocated once you're done with it. It would also help if you commented your code to explain your intended behaviour.
To solve your problem I'd recommend checking your array indexing whilst debugging. It might be easier if you used a smaller input value to begin with until you know that your algorithm works.
One other point is whether you need the factors array at all? If the purpose of the function is to print the prime factors, couldn't you just print them as you find them?
Edit
I think #BLUEPIXY has hinted at the issue in the comments. The input argument to malloc is a size_t. On my PC size_t is a 32 bit value. If that's the case on your platform then the size of the block of memory you're trying to allocate is too big. Check if (n*sizeof(unsigned long long)) > SIZE_MAX. As I mentioned earlier, could you just print the factors as you find them and do away with the array?
This small C script checks if a number is a prime... Unfortunately it doesn't fully work. I am aware of the inefficiency of the script (e.g. sqrt optimization), these are not the problem.
#include <stdio.h>
int main() {
int n, m;
printf("Enter an integer, that will be checked:\n"); // Set 'n' from commandline
scanf("%d", &n); // Set 'n' from commandline
//n = 5; // To specify 'n' inside code.
for (m = n-1; m >= 1; m--) {
if (m == 1) {
printf("The entered integer IS a prime.\n");
break;
}
if (n % m == 0) {
printf("The entered integer IS NOT a prime.\n");
break;
}
}
return 0;
}
I tested the programm with a lot of numbers and it worked... Then I tried a bigger number (1231231231231236) which is clearly not a prime...
BUT: the program told me it was!?
What am I missing...?
The number "1231231231231236" is too big to fit in an "int" data type. Add a printf statement to show what number your program thinks you gave it, and if that's prime, your program works fine; else, you might have a problem that merits checking. Adding support for integers of arbitary size requires considerable extra effort.
The reason you are having this problem is that intrinsic data types like int have a fixed size - probably 32 bits, or 4 bytes, for int. Given that, variables of type int can only represent 2^32 unique values - about 4 billion. Even if you were using unsigned int (you're not), the int type couldn't be used to store numbers bigger than around 4 billion. Your number is several orders of magnitude larger than that and, as such, when you try to put your input into the int variable, something happens, but I can tell you what doesn't happen: it doesn't get assigned the value 1231231231231236.
Hard to know without more details, but if your ints are 32-bit, then the value you've passed is outside the allowable range, which will no doubt be represented as something other than the value you've passed. You may want to consider using unsigned int instead.
The given number is too large for integer in C. Probably it only accepted a part of it. Try Printing the value of n.