Amazon Interview Question
Quality Assurance EngineersTeam: Kindle
Country: India
Interview Type: Written Test
for the example given, when rounding off to two digits the output should be 3.46, shouldn't it?? or did i miss something?
Yo can see code + Test cases here www "dot" gohired "dot" in/2014/07/test-cases-for-round-function "dot" html
firstly v can get 3.4567 as .4567 by subtracting the integer part from the no.. then we can multiply by 1000 and then take %10 and then check that number if it is greater than or equal to 5 then v can round off to next digit otherwise keep it as it is
#include<stdio.h>
#include<math.h>
#include<conio.h>
void main()
{
float fl,j;
int n,k,i;
clrscr();
printf("enter float value: ");
scanf("%f",&fl);
printf("enter precision: ");
scanf("%d",&n);
fl=(float)fl*pow(10,n);
printf("%f ",fl);
if((int)(fl*10)%10>=5)
fl=(int)fl+1;
else
fl=(int)fl;
fl=(float)fl/pow(10,n);
printf("%f",fl);
}
assert(round(1234.5678, 1) == 1234.6);
assert(round(1234.5678, 2) == 1234.57);
assert(round(1234.5678, 3) == 1234.568);
assert(round(1234.5678, 4) == 1234.5678);
assert(round(0.1234, 1) == 0.1);
assert(round(0.1234, 2) == 0.12);
assert(round(0.1234, 3) == 0.123);
assert(round(0.1234, 4) == 0.1234);
double round(double num , int n)
{
unsigned long long tmp, tmp1;
tmp = num * pow(10, n+1);
tmp1 = num * pow(10, n);
if (tmp%10 >= 5)
tmp1++;
return tmp1 / pow(10, n);
}
I think we need to write the test data, not a code.
- F August 06, 2014It should be something like that:
int (2, 2)
number of digits after the coma > precision (2.23434, 1)
number of digits after the coma = precision (3.23, 2)
number of digits after the coma < precision (4.23, 5)
zero (0, 0)
precision zero (3.45, 0)
negative (-1.2334, 3)
negative precision (3.34, -5)
empty ( ,3)
empty precision (3.245, )
etc.