Fix Function

Write the implementation of Fix function? fix(2.5) = 2 and fix(-2.25) = -3, this is the expected result. Write the code to implement this behaviour?

Questions by rolemodel2k7

Showing Answers 1 - 57 of 57 Answers

int FixUp(float value)
{
    int fixVal;

    fixVal = (int)value;
    if (value < 0)
    {
        if(value < (float)fixVal)
        {
            fixVal = fixVal - 1;
        }
    }
    return fixVal;
}

  Was this answer useful?  Yes

vbablu

  • Mar 25th, 2009
 

void fix(float);
void main()
{
  float f;
scanf("%",&f);
fix(f);
getch();
}
void fix(float f)
{
  float x;
 if(f<0)
{
   x=f-(int)f;
  f=f-x;
   printf("%d",(int)f);
}
else if(f<0)
{
  x=f-(int)f;
  f=f-x-1;
 printf("%d",(int)f);
}
}

  Was this answer useful?  Yes

zivora

  • Mar 28th, 2009
 

#include<stdio.h>
#include<conio.h>
#include<math.h>

int fix(float z)
{
    if(z>0)
    {
        return floor(z);
    }
    else if(z<0)
    {
        z--;
           return    ceil(z);
    }
    else return z;
}
void main()
{
    float x=2.5, a=-2.25;
    int fx,fa;
    fx= fix(x);
    printf("This is for 2.5 nn %d",fx);
    fa= fix(a);
    printf("This is for - 2.25 nn %d",fa);
}

  Was this answer useful?  Yes

kushaagra

  • Apr 16th, 2009
 

#include<math.h>

#include<conio.h>
#include<stdio.h>

void main()
{
  clrscr();
  printf("n The Value of 2.25 is = %lf, and -2.25 is = %lf",floor(2.25), floor(-2.25));
  getch();
}

  Was this answer useful?  Yes

ashu_1988

  • Apr 21st, 2009
 

#include<stdio.h>
#include<conio.h>
main()
{
int flr(float);
clrscr();
printf("the value of 2.5=%dnthe value of -2.25=%d",flr(2.5),flr(-2.25));
getch();
}
int flr(float n)
{
int p;
p=n;
if(n-p)
{
if(p<0)
return(n-1);
else if(p>=0)
return(n);
}
else return(n);
}

  Was this answer useful?  Yes

iantuy

  • Apr 26th, 2009
 

#include<stdio.h>
int fix(float a) {
  return a>=0?a:(float)(int)a-a?a-1:a;
}
int main() {
  printf("%d %d %d",fix(2.5f),fix(-2.1),fix(-2.0f));
  return 1;
}

  Was this answer useful?  Yes

alokag

  • Jun 25th, 2009
 

#include <stdio.h>

int fix(float n)
{
   if (n<0)
   return( int(n-1) );

   return( int(n) );

}

  Was this answer useful?  Yes

vjy_2107

  • Jun 28th, 2009
 

#include<stdio.h>
#include<float.h>
#include<math.h>
int fix(float);
int main()
{
    int result;
    float val;
    printf("Enter the value to be converted: ");
    scanf("%f",&val);
    printf("nValue entered: %f", val);
    result=fix(val);
    printf("n Value after rounding off: %d",result);
    getch();
    return 0;
}
int fix(float value)
{
   if(value<0.0)
   {
      if(floor(value)-(int)(value))
      return --value;
   }
   else
      return value;
   if(value>0.0)
   {
      if(ceil(value)-(int)(value))
      return --value;
   }
   else
      return value;
}

  Was this answer useful?  Yes

gudluck

  • Jul 15th, 2009
 

int Fix(float f)
{

   if((f!=(int)f) && f<0.0)
      return ((int)f-1);
   else
      return (int)f;
}

  Was this answer useful?  Yes

HariGM

  • Aug 3rd, 2010
 

void fix(float a)
{

float i=0;
pid_t=pid;

i=a;

 if (i>0)
{
 printf("Fixed Value :%d n", (int)i);
}
else if (i<0)
{
   printf("Fixed Value :%dn", (int)--i);
}

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions