GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C
Go To First  |  Previous Question  |  Next Question 
 C  |  Question 448 of 453    Print  
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?


  
Total Answers and Comments: 18 Last Update: October 15, 2009     Asked by: rolemodel2k7 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: elephantpaw
 
#include <iostream>
#include <math.h>
using namespace std;

int main()
{
cout<<"test ::"<< floor(2.5) <<" :: "<< floor(-2.25) ;
return 0;
}




Above answer was rated as good by the following members:
usha_martin, ashima_111, ronen_barak, srinivasdivi, vjy_2107, gbgovinddaprabhu
  Sorting Options  
  Page 1 of 2   « First    1    2    >     Last »  
February 13, 2009 00:08:54   #1  
elephantpaw Member Since: February 2009   Contribution: 1    

RE: Fix Function
#include <iostream>
#include <math.h>
using namespace std;

int main()
{
cout<<"test ::"<< floor(2.5) <<" :: "<< floor(-2.25) ;
return 0;
}



 
Is this answer useful? Yes | NoAnswer is useful 3   Answer is not useful 4Overall Rating: -1    
February 20, 2009 04:21:45   #2  
shreeshavailaya Member Since: February 2009   Contribution: 1    

RE: Fix Function
int fix(float n){if(n<0.0) return n-1;return n+1;}//converted to int by implicit casting
 
Is this answer useful? Yes | NoAnswer is useful 3   Answer is not useful 3Overall Rating: -N/A-    
March 19, 2009 01:09:48   #3  
seemakadavan Member Since: March 2009   Contribution: 2    

RE: Fix Function
There is one problem with this code. If you give the input as -2.0 it gives the output as -3.0 instead of -2.0
 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 2Overall Rating: -2    
March 19, 2009 01:10:44   #4  
seemakadavan Member Since: March 2009   Contribution: 2    

RE: Fix Function
int FixUp(float value)
{
int fixVal;

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


 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    
March 25, 2009 03:48:20   #5  
vbablu Member Since: February 2009   Contribution: 4    

RE: Fix Function
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);
}
}

 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    
March 28, 2009 12:26:27   #6  
zivora Member Since: March 2009   Contribution: 1    

RE: Fix Function
#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);
}

 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    
April 06, 2009 08:50:06   #7  
tweeto Member Since: April 2009   Contribution: 1    

RE: Fix Function
int Fix( float x)
{
return (int)x;
}

 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 2Overall Rating: -2    
April 16, 2009 05:29:28   #8  
kushaagra Member Since: April 2009   Contribution: 1    

RE: Fix Function
#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();
}

 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    
April 21, 2009 03:40:45   #9  
ashu_1988 Member Since: April 2009   Contribution: 1    

RE: Fix Function
#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);
}

 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    
April 26, 2009 14:07:37   #10  
iantuy Member Since: October 2008   Contribution: 2    

RE: Fix Function
#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;
}

 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    
  Page 1 of 2   « First    1    2    >     Last »  


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape