Floating point bug common to different languages? [closed] - c

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
#include<stdio.h>
void main()
{
float i,j;
i=??;
j=i+1;
if(i==j)
printf("Bug");
}
My teacher gave me this qns to find the value of i so that the printf executes.
He said this is a common bug in a lot of languages.

Always try the boundaries for odd behavior. This worked for me:
#include <float.h>
int _tmain(int argc, _TCHAR* argv[])
{
float i,j;
i = FLT_MAX;
j = i + 1;
if(i == j)
{
printf("they're the same");
}
return 0;
}

Related

Online Judgement System - Why am I getting Wrong Answer for this thread? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Original page: http://acm.whu.edu.cn/learn/problem/detail?problem_id=1036
This should be a simple Dynamic Programming problem. I figured out the solution to be the following:
int main(void)
{
double d[501];
int i;
d[0] = d[1] = 1.;
d[2] = 2.;
for(i = 3; i<=500; i++)
d[i] = d[i-1] + d[i-2] + d[i-3];
int n;
while(scanf("%d", &n) == 1) {
if(n == 0) return 0;
printf("%.0lf\n", d[n]);
}
return 0;
}
But Wrong Answer reported after submission. I really don't know why.
double is not enough for the precision
you should use high-precision to solve it
Decimal point maybe. printf("%.0lf\n", 1.0); will print 1.0 but system may wait for 1.

How to convert a string containing a hex character code to the character value? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have an array of chars "0x55".
What I want to do is convert it to a char which is going to be U (because ASCII 0x55 = U).
So how to do this conversion?
#include <windows.h>
int main()
{
array[] = "0x55"
char test;
**// I want to move the string to that test to be one character which is U**
}
Any suggestions?
I think this is what you are after:
int main(int argc,char**argv)
{
char array[] = "0x55";
int value;
char test;
sscanf(array,"%x",&value);
test = value;
return 0;
}
In C++, I would code it a little differently, but this seems more like a C question.

Command-line options in C [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I would like to create a C program that accepts an argument of the form
-aK
where K is some integer from 0-9.
How would I parse/specify this option?
You might want to check out getopt and/or getopt_long.
a simple requirement like this can be solved with getopt.
Also you can do this:
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
char ch, a;
int d;
if(argc == 1) return;
if(argc == 2){
if(strlen(argv[1]) > 2){
sscanf(argv[1],"%c%c%d",&ch,&a,&d);
if(ch == '-' && a == 'a'){
printf("%d is your number",d);
}
}
}
return 0;
}

How to find square root of a number using a recursive function? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
Basically i want to create a recursive function to implement this program in C
#include <stdio.h>
main()
{
float guess=1,num,num1;
int i;
printf("enter any number:\n");
scanf("%f",&num);
num1=num;
for (i=1;num1>1;i++,num1/=10); //to calculate no of digits in input
i=i/2;
printf("i:%d\n",i); //to make a better guess
for (;i>0;i--,guess*=10);
printf("guess = %f\n",guess);
for (i=1;i<=10;i++) //evaluate square root improving accuracy with each loop
{
guess=(guess+num/guess)/2;
}
printf("sqrt: %f\n",guess);
}
Something like this:
#include <math.h>
#include <float.h>
float MySqrt(float num, float prev)
{
float next = (prev+num/prev)/2;
if (fabs(next-prev)<FLT_EPSILON*next)
return next;
return MySqrt(num, next);
}
To call it, pass 1.0 as your initial guess for the prev parameter.
You can easily make this fail with a stack overflow by passing in bad data, but you probably aren't going to be tested on that in this assignment.

factorial using recursion+pointers [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
i am learning C programming, i was trying to write a recursive function by using this prototype:
void fact(int *n);
The parameter of this function should be passed by reference. Thanks for your help.
I don't feel to be helpful in giving a complete solution -- this is just to show there is an answer:
void fact(int *n)
{
if (*n > 1)
{
int tmp = *n - 1;
fact(&tmp);
*n *= tmp;
}
}
I would never write a factorial function this way.

Resources