i have a problem. I've been making this for hours, and i finally thought i had a draft, i solved a few mistakes i made, but now it's typing out "Floating point exception (core dumped)" when i run it. I was able to solve a few other issues, but I don't think i can get over this one without basically starting from scratch, i have no idea what could be causing this, i wonder if any more knowledgeable people here could take a look and try to spot a possible mistake. My program is supposed to find Non-square numbers - numbers not divisible by squares of whole numbers. It first finds squares to divide by, and then divides Non-square number candidates up to a specified integer. Then it types out all the numbers it finds. I think it's quite possible i've made a mistake in pointer usage, i have not yet quite mastered those, and most likely couldn't solve a related issue anyway.
#include <stdio.h>
#include <limits.h>
#include <math.h>
#include <stdlib.h>
int isNonSq (int a,int sqr) {
int b=0.75*a;
for (int i=2;i<b;i++) {
if (a%sqr[i]==0) return 0;
}
return 1;
}
int main ( void ) {
int a;
int * resNum;
int * sqr;
while (!feof(stdin)) {
if (scanf(" %d",&a)!=1||a<=0) {
printf("Nespravny vstup.\n");
return 0;
} else {
int b, c=1;
b=0.75*a;
resNum=(int)malloc(a*sizeof(resNum));
sqr=(int)malloc(a*sizeof(*sqr));
for (int i=2;i<sqrt(a);i++)
sqr[i]=pow(i,2);
for (int i=1;i<b;i++) {
if (isNonSq(i,sqr)) {
resNum[c]=i;
c++;
}
}
}
}
for (int i=1;i<a;i++) {
printf(" %d",resNum[i]);
}
printf("\n");
free(resNum);
free(sqr);
return 0;
}
This could be occurring because of an NaN or an infinite number, I would recommend you use gdb to figure out what is happening when you run your compiled c code. https://www.cs.swarthmore.edu/~newhall/unixhelp/howto_gdb.php
Goodluck!
Related
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.
I'm confused as to why this does not cause an error.
void f(int I) {
++I;
if (I > INT_MAX) {
printf("OVERFLOW\n");
} else {
printf("NO OVERFLOW\n");
}
}
int main(void) {
f(INT_MAX);
printf("DONE\n");
}
I'm coding in C, and my school defined INT_MAX for us already to be $2^{31} - 1$ I think is what it is.
This can give you NO OVERFLOW on two different routes:
Your compiler has the right to optimize out your if statement. Since nothing of int type can be >INT_MAX, it's always false, thus your if is compiled to a simple printf("NO OVERFLOW\n");.
For the other one you need to know 2-complement. If you add 1 to INT_MAX, it turns around, and becomes the smallest number an int can represent. So 2147483647+1=-2147483648, which doesn't make sense in maths, but here it's a design feature. This makes adding reasonably large negative numbers to positive numbers work well. Just you need to keep in mind to use the proper type to hold your numbers.
(I'm assuming a "normal" 32 bit int, but some architectures use different size, e.g. on AVR8 ints are 16 bit long.)
OVERFLOW will not be printed in your case. What is happening in your case is that: When your I is already at the maximum, and if you further increase it using ++I, it will wrap around to the lowest value. Illustration:
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
void f(int I) {
++I;
printf("\nAfter incrementing I: %d\n", I);
if (I > INT_MAX) {
printf("OVERFLOW\n");
} else {
printf("NO OVERFLOW\n");
}
}
int main(void) {
f(INT_MAX);
printf("DONE\n");
}
Output:
After incrementing I: -2147483648
NO OVERFLOW
DONE
I am having a go at Project Euler question 4 - Finding the highest palindrome from a product of two 3-digit numbers. I have found many solutions online, none of which answer the issues I am experiencing with my own code. I am not an experienced coder and have just starter learning recreationally.
My code so far does a for loop which generates all of the numbers that are palindromes. Currently I have it printing all of them just to check that it is working, which it appears to be. I was wondering if there was a way to print only the largest number in the loop? I don't think running through manually is the most efficient way of doing it.
Here is the code so far:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
int main()
{
int a,b,c,d;
for(a=100;a<=999;a++){
for(b=100;b<=999;b++){
c=a*b;
d=reverse(c);
if(c==d){
printf("%d is a palindrome\n",c);
}
}
}
return 0;
}
int reverse(int number){
int answer=0;
while(number!=0){
int units=number%10;
answer=answer*10+units;
number=number/10;
}
return answer;
}
Assign the largest result to a variable. Then when you find another that is bigger, update that variable.
Then when the loop is complete, print the result from the variable which is the largest.
I'll let you work out the code. It's trivial.
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.
#include <stdio.h>
#include <math.h>
int fib();
int scan;
int main() {
scanf("%d", &scan);
printf("%d\n", fib());
scanf("%s");
return 0;
}
int fib() {
return floor((pow(1+sqrt(5)/2, scan)-(-pow(1-sqrt(5)/2, scan)))/sqrt(5));
}
I'm pretty new to programming with C and decided to try and calculate any number in the Fibonacci series. I based it off of my lua script here. I'm at a loss of what I've done wrong, could someone give me some insight?
You have the formula wrong. You want fib to be:
int fib() {
return round((pow((1+sqrt(5))/2, scan)-(-pow((1-sqrt(5))/2, scan)))/sqrt(5));
}
instead. You were missing parenthesis around the 1+sqrt(5) and 1-sqrt(5) terms and were using floor instead of round, which was underestimating the fibonacci numbers in my tests. (This mostly has to do with low precision in the pow function. The seventh fibonacci number, 13, came out to 12.969)
You also probably want to change
scanf("%s");
to
char tmp;
scanf("%c", &tmp);
Since the way you have it incorrectly omits an argument.
Hope this helps!