floating point:invalid error turbo C - c

this program takes number of columns as input and should return equivalent amount of numbers of base 3.but in tc it gives a "floating point:invalid" error.
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int* count(int);
main()
{
int i,j,n,*a;
printf("n=");
scanf("%d",&n);
a=count(n);
for(i=0;i<pow(3,n-1);i++)
{
for(j=0;j<n-1;j--)
{
printf("%d",a[i*(n-1)+j]);
}
}
return 0;
}
//calculating and storing count values in a 2d array implemented by 1d array.
int* count(int n)
{
int i,j,*a,k;
a=(int*)malloc((n-1)*pow(3,n-1)*sizeof(int));
for(j=0;j<n-1;j--)
{
for(i=0;i<pow(3,n-1);i++)
{
k=(pow(3,n-1-j));
a[i*(n-1)+j]=i%k;
}
}
return a;
}

At this point:
for(j=0;j<n-1;j--)
You loop over the following values:
0, -1, -2, -3, -4, ...
and the loop will (probably) terminate when j wraps around to a positive value. If indeed that's what happens with your long out-of-date compiler.
When you then calculate
pow(3,n-1-j)
The exponent becomes a large positive value and you will overflow in due course.
I expect that's your fundamental problem.

Not sure what you're trying to achieve, but fixing the for loop for j won't crash.
Fix the loop at both places in your code to:
for(j=0;j<n-1;j++)

Most probably it seems you have not linked the math library while compiling your code.

Related

why does it show segmentation fault when I tried to use recursion here?

I tried to write a code to calculate how many 1 are there in a number's binary form. This is my code:
#include <stdio.h>
#include <math.h>
static int num = 0;
void binary(int target){
int n = 0;
int a = 1;
if(target != 0){
while(target >= a ){
n++;
a = pow(2, n);
}
a = pow(2, n-1);
num++;
binary(target - a);
}
}
int main() {
int target = 0;
scanf("%d", &target);
binary(target);
printf("%d",num);
return 0;
}
However, this shows segmentation fault when I run it. I don't know where has the code tried to access memories that are not allowed. I figured it might have something to do with the recursion in the binary function. Can anyone tell me what have caused the segmentation fault here? Thank you so much. I really can't understand segfaults :(
As mentioned in the comments, pow is not a good candiate here due to its signature:
double pow(double x, double y);
so any place that you are using pow, you are implicitly using floating point numbers. I was able to cause a segfault with the input 1<<31 which is the value -2147483648. This will cause your loop to terminate with n=0, a=1. You then set a = pow(2, -1), but since a is an integer, this gets floored down to just 0. You then recurse with binary(target - 0) which might as well just be binary(target) again, hence you have an infinite call with no termination.
I'll also leave as a note that recursion for this type of problem is probably not the right tool, unless your goal is to learn about recursion. There is a much more concise and reliable method via a loop and the & operator. I would also suggest using unsigned values to avoid issues like this with negative terms.

recursion c, program does not display

im facing an problem in this program, may anyone tell me, what im doing wrong, the program won't display anything after i give it input.
(Code is about sum of digits enter #example 12345 = 15)
#include<stdio.h>
int sum(int num);
int sum(int num){
int total=0;
if(sum==0){
return total;
}
else{
total+=num%10;
num/=10;
return sum(num);
}
}
int main()
{
int num,k;
printf("Enter 5 positive number: ");
scanf("%d",&num);
printf("Sum is: %d",sum(num));
}
Here is a rule of thumb, whenever you have a non-stopping recursion program try to verify your base cases.
Here you are verifying sum the function instead of num the parameter. The C compiler let's you do that because functions in C are pointers, and pointers hold the addresses as numeric value.
You just need to change the condition from sum==0 to num==0. It will now print something. However, the logic of your program is still wrong. You can change your sum function to this.
int sum(int num){
if(num==0) {
return 0;
}
return num % 10 + sum(num/10);
}
And you can try learning more about recursion through stack since recursion is basically just stack.
In your code the total gets initialized to zero every time the function is called. and a variable named sum is not initialized. Just change sum==0 to num==0.I have also given the logic to sum the digits of a number.

c program to find sum of first 1000 prime number

I want to print sum of the first 1000 prime numbers. I don't know if the following implementation is right and where it is wrong. Moreover, how can I optimize this implementation, which is required for extra off course?
#include<stdio.h>
#include<math.h>
int prime(int no,int lim)
{
int i=2,flag=0;
for(;i<=lim;i++)
{
if(no%i==0)
{
flag=1;
}
}
return flag ;
}
int main()
{
int i=4,count=2,j,k,l,n=4;
double sum=5.0;
for(;count<=1000;)
{
j=sqrt(i);
k=prime(i,j);
if(k==0)
{
//printf("\n%d",i);
sum+=(double)i;
//for(l=0;l<100000;l++);//just to reduce speed of the program
count++;
}
i++;
}
printf("\n%f",sum);
return 0;
}
I don't know if the following implementation is right and where it is wrong.
The implementation is correct except for an off-by-one error: Since count is the number of primes that were already taken into account, the loop condition count<=1000 causes the loop to be run one more time when 1000 primes have already been summed, adding the 1001. prime. Correct is: count<1000.
To optimize your code you better use Sieve of Eratosthenes to generate prime up to your limit, then add these primes. To know how sieve works, read this article.

C program to calculates the number of ways to choose k objects from n distinct objects. 'k' and 'n' both are integers

I wrote a C program to calculate the number of ways to choose k objects from n distinct objects using functions.
#include<stdio.h>
long f(int a)
{
if(a==1||a==0)return(0);
else return(a*f(a-1));
}
int combination(int N,int K)
{
long int NF,KF,NMKF;
NF=f(N);
KF=f(K);
NMKF=f(N-K);
return(NF/(KF*NMKF));
}
int main()
{
int n,k;
scanf("%d%d",&n,&k);
combination(n,k);
}
But the compiler shows following error message
floating point exception (core dumped)
How to avoid it?
The problem is in this line
if(a==1||a==0)return(0);
It should be
if(a==1||a==0)return(1);
While calculating factorial, n*(n-1)*(n-2)...*(2)*(1). Notice in the end, we multiply by 1 and not zero. multiplying with 0 would make the factorial 0. And later when you are performing division, the 0 comes in the denominator, and floating point exception is occurring. That's why your program is giving error.
For cases when factorial of 0 is needed. Then also this would work, because factorial of 0 is 1 and not 0.. Check this out.
Two problems:
if(a==1||a==0) you should return 1, not return 0. Because 1!=1, 0!=1.
Your intention is choose k objects from n distinct objects. But You should add param checking in order to not occur the n<k. If we input n=2, k=3.The program will go to error. It is bad!
I hope this can help you.

Why am I getting segmentation fault in my following C code?

This is a problem from spoj named prime1. The code seems to be correct to me. This even runs and produces desirable results on ideone.com but spoj gives me a runtime error, saying this is a segmentation fault. I can't find any memory leaks, no buffer overflow, etc. Please help me find the segmentation fault.
#include <stdio.h>
unsigned int arr[32200];
int prime()
{
unsigned int i,j,k=2;
int flag;
arr[0]=2;
arr[1]=3;
for (i=5;i<32200;i+=2)
{
flag=0;
for(j=3;j<i;j+=2)
{
if(i%j==0)
{
flag=1;
break;
}
}
if (flag==0)
{
arr[k++]=i;
}
}
return 0;
}
int main()
{
int t;
unsigned int a,b,i,m;
scanf("%d",&t);
prime();
while(t--)
{
scanf("%u%u",&a,&b);
for(i=0;;i++)
{
if (arr[i]>=a)
{
m=i;
break;
}
}
while(arr[m]<=b)
{
printf("%u\n",arr[m]);
m++;
}
printf("\n");
}
return 0;
}
If an a is given that is greater than all elements in arr, the first for() loop in main() overruns the array, yielding undefined behavior. The fact that the global variable arr will be zero initialized helps to trigger this condition: start with any a other than zero, and you immediately have undefined behavior.
The array you are keeping your primes is too small.
The maximum number you can have as b is 10^9 and the smallest for a is 1. Therefore, you need to store all primes between 1 and one billion.
If you type "how many primes between 1 and 1000000000" in wolfram alpha, for instance, you will get that there are 50847534 primes between those two. So your array is too small.
Also, after you fix that, you're getting a TLE. Your code is too inefficient for this problem. You need to develop a faster method to generate the prime numbers.

Resources