“I bought this guide a few days ago to prepare for my interview with Oracle. Many of the questions they asked me were from this guide. I found this book absolutely great!”
# 4 is wrong
Both functions are below with test code and output. I should mention that just casting a float to an int will round it down so a round function is not necessary. swap function assumes that the size of an int * is a long.
#include
int round(float x ) {
//return int(x); // or return (int) x;
return (int) x;
}
void swap(int *& ptr1, int * & ptr2) {
ptr1=(int *) ((long) ptr1 ^ (long) ptr2); // ^ means XOR
ptr2=(int *) ((long) ptr2 ^ (long) ptr1);
ptr1=(int *) ((long) ptr1 ^ (long) ptr2);
}
int main ( int argc, char ** argv ) {
8. is good solution for rounding float to and integer.
—
swapping pointers.
the only trick, I think, here is that we need to pass pointer variable by reference not by value
(Pass by reference can be implemented either by pointer to pointer or by means of references.)
int SwapPointers(int **p1, int **p2)
{
int *temp = *p1;
*p1=*p2;
*p2 = temp;
}
— calling a swap function.
int a = 4, b = 5;
int *pa = &a;
int *pb = &b;
SwapPointers(&pa, &pb);
for swap two integers, e.g. x and y, we should use pointers or references, i.e.
void main ()
{
int x = 3, y = 4;
swap(&x, &y);
}
swap (int *a, int *b)
{
int temp
temp=*a;
*a=*b;
*b=temp;
}
HOWEVER, here the problem is to swap two integer pointers, so only the post “BS — August 5, 2007 ” is correct, i.e.
void main()
{
int x=3, y=4;
int *a=&x, *b=&y;
cout=0, return (int)(x+0.5);
if x
for swap two integers, e.g. x and y, we should use pointers or references, i.e.
void main ()
{
int x = 3, y = 4;
swap(&x, &y);
}
swap (int *a, int *b)
{
int temp
temp=*a;
*a=*b;
*b=temp;
}
HOWEVER, here the problem is to swap two integer pointers, so only the post “BS — August 5, 2007 ” is correct, i.e.
void main()
{
int x=3, y=4;
int *a=&x, *b=&y;
printf(”Before swap”);
printf(”%d %d”,a,b);
swapPtr(&a,&b);
printf(”After swap”);
printf(”%d %d”,a,b);
}
void swapPtr(int **s, int **t)
{
int *temp=*s;
*s=*t;
*t=temp;
}
output example:
Before swap:
0012FF78 0012FF74
After swap:
0012FF74 0012FF78
to conver floating point number to an integer,
float x;
int i=(int)x;
or we can use ceil or floor functions which are available in header file
or int y=floor(x)
add 0.5 to it and then cast it to an int.
int round(float x)
{
return (int)(x+0.5);
}
void swap(int *ptr1, int *ptr2)
{
ptr1=ptr1+ptr2;
ptr2= ptr1-ptr2;
ptr1=ptr1-ptr2;
}
# 4 is wrong
Both functions are below with test code and output. I should mention that just casting a float to an int will round it down so a round function is not necessary. swap function assumes that the size of an int * is a long.
#include
int round(float x ) {
//return int(x); // or return (int) x;
return (int) x;
}
void swap(int *& ptr1, int * & ptr2) {
ptr1=(int *) ((long) ptr1 ^ (long) ptr2); // ^ means XOR
ptr2=(int *) ((long) ptr2 ^ (long) ptr1);
ptr1=(int *) ((long) ptr1 ^ (long) ptr2);
}
int main ( int argc, char ** argv ) {
float x = 23.45;
int y = round(x);
std::cout
i think in round function we have to consider the fact that if the fractional portion is more than 0.5, we should ceil it.
// round the float to integer
int round(float fnum) {
int inum=(int)fnum;
if(fnum-inum>=0.5)
++inum;
return inum;
}
int round(float x)
{
int y=(int)x;
if(x-(float)y>=0.5)
y=y+1;
return y;
}
void swap(int *a,int *b)
{
a=a^b;
b=a^b;
a=a^b;
}
add 0.5 to x if positive, otherwise subtract 0.5 from x and return the int part:
int round(float x)
{
if (x >= 0)
return (int)(x+0.5);
else
return (int)(x-0.5);
}
Depends on how you want to round it. IEEE specifies 4 rounding modes RI, RZ, RNE and RMI.
RNE is the most general rounding mode.
Rounding also depends on the precision you want to achieve.
private static int FloattoInt(float x){
float i = x % 1;
float integer = x - i;
return (int)integer;
}
#include
//using pointers
void swap(int *a,int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
// using references
void swap1(int &a,int &b)
{
int temp;
temp = a;
a = b;
b = temp;
}
// swap with out a temp var
void swap2(int &a,int &b)
{
a = a+b;
b = a-b;
a = a-b;
}
int main(){
int x, y;
x = 1;
y = 2;
printf(”x = %i, y = %i \n”, x, y);
swap(&x, &y);
printf(”x = %i, y = %i \n”, x, y);
swap1(x, y);
printf(”x = %i, y = %i \n”, x, y);
swap2(x, y);
printf(”x = %i, y = %i \n”, x, y);
}
8. is good solution for rounding float to and integer.
—
swapping pointers.
the only trick, I think, here is that we need to pass pointer variable by reference not by value
(Pass by reference can be implemented either by pointer to pointer or by means of references.)
int SwapPointers(int **p1, int **p2)
{
int *temp = *p1;
*p1=*p2;
*p2 = temp;
}
— calling a swap function.
int a = 4, b = 5;
int *pa = &a;
int *pb = &b;
SwapPointers(&pa, &pb);
void swap(int *a,int *b)
{
int *tmp;
tmp = a;
a = b;
b = tmp;
}
a,b are 2 nos to be swapped.
a^=b^=a^=b
After this operation, The nos will get swapped
private int Round(double x)
{
int result = (int) x;
int tmp = (int)(x*10)%10;
if (tmp >= 5)
result++;
else if (tmp
I would never write a function for swapping two integers. This is a waste. Better a macro or inlined template.
int round(float x) {
return (int)(x + .5);
}
you should give the function like this int **a, int **b
for swap two integers, e.g. x and y, we should use pointers or references, i.e.
void main ()
{
int x = 3, y = 4;
swap(&x, &y);
}
swap (int *a, int *b)
{
int temp
temp=*a;
*a=*b;
*b=temp;
}
HOWEVER, here the problem is to swap two integer pointers, so only the post “BS — August 5, 2007 ” is correct, i.e.
void main()
{
int x=3, y=4;
int *a=&x, *b=&y;
cout=0, return (int)(x+0.5);
if x
for swap two integers, e.g. x and y, we should use pointers or references, i.e.
void main ()
{
int x = 3, y = 4;
swap(&x, &y);
}
swap (int *a, int *b)
{
int temp
temp=*a;
*a=*b;
*b=temp;
}
HOWEVER, here the problem is to swap two integer pointers, so only the post “BS — August 5, 2007 ” is correct, i.e.
void main()
{
int x=3, y=4;
int *a=&x, *b=&y;
printf(”Before swap”);
printf(”%d %d”,a,b);
swapPtr(&a,&b);
printf(”After swap”);
printf(”%d %d”,a,b);
}
void swapPtr(int **s, int **t)
{
int *temp=*s;
*s=*t;
*t=temp;
}
output example:
Before swap:
0012FF78 0012FF74
After swap:
0012FF74 0012FF78
=================
For round function,
if x>=0, return (int)(x+0.5);
if x
Regulus rounding code won’t compile in my C++ compiler. Both operands of the modulus operator (%) need to be int.
Leave an Answer/Comment