AceTheInterview
Jobs in Pune | Work better in teams | Socialize with friends | Submit Q&A | Tell a friend
Search site for 

Top 100 Interview Questions & Answers in a convenient and easy to read book!

“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!”

– Ravi, California

Read more comments...

Interview Questions And Answers RSS Feed

Answers »

  1. Submitted By: jyotsana — December 23, 2006
    +1 votes
      + -

    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)

  2. Submitted By: commonsense — December 23, 2006
    +20 votes
      + -

    add 0.5 to it and then cast it to an int.

    int round(float x)
    {
    return (int)(x+0.5);
    }

  3. Submitted By: commonsense — December 23, 2006
    +5 votes
      + -

    void swap(int *ptr1, int *ptr2)
    {
    ptr1=ptr1+ptr2;
    ptr2= ptr1-ptr2;
    ptr1=ptr1-ptr2;
    }

  4. Submitted By: cjolly — January 7, 2007
    -3 votes
      + -

    # 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

  5. Submitted By: onlyonegod — January 19, 2007
    +2 votes
      + -

    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;
    }

  6. Submitted By: Arun R — May 21, 2007
    -3 votes
      + -

    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;
    }

  7. Submitted By: MHM — May 22, 2007
    +8 votes
      + -

    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);
    }

  8. Submitted By: Nick — May 29, 2007
    +1 votes
      + -

    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.

  9. Submitted By: Nick — May 29, 2007
    +1 votes
      + -

    Rounding also depends on the precision you want to achieve.

  10. Submitted By: Regulus — July 24, 2007
    +1 votes
      + -

    private static int FloattoInt(float x){

    float i = x % 1;
    float integer = x - i;

    return (int)integer;
    }

  11. Submitted By: slice4you — August 2, 2007
    +0 votes
      + -

    #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);

    }

  12. Submitted By: BS — August 5, 2007
    +6 votes
      + -

    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);

  13. Submitted By: ray — August 7, 2007
    -1 votes
      + -

    void swap(int *a,int *b)
    {
    int *tmp;
    tmp = a;
    a = b;
    b = tmp;
    }

  14. Submitted By: Dinesh — August 21, 2007
    +3 votes
      + -

    a,b are 2 nos to be swapped.
    a^=b^=a^=b
    After this operation, The nos will get swapped

  15. Submitted By: Sam — September 10, 2007
    +2 votes
      + -

    private int Round(double x)
    {
    int result = (int) x;

    int tmp = (int)(x*10)%10;
    if (tmp >= 5)
    result++;
    else if (tmp

  16. Submitted By: Bojo — October 1, 2007
    not yet rated
      + -

    I would never write a function for swapping two integers. This is a waste. Better a macro or inlined template.

  17. Submitted By: pv — October 9, 2007
    +0 votes
      + -

    int round(float x) {
    return (int)(x + .5);
    }

  18. Submitted By: goug — October 24, 2007
    +1 votes
      + -

    you should give the function like this int **a, int **b

  19. Submitted By: tellme — November 23, 2007
    not yet rated
      + -

    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

  20. Submitted By: tellme — November 23, 2007
    not yet rated
      + -

    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

  21. Submitted By: clint — May 22, 2008
    not yet rated
      + -

    Regulus rounding code won’t compile in my C++ compiler. Both operands of the modulus operator (%) need to be int.

  22. Leave an Answer/Comment

    To prove you're a person (not a spam script), type the security text shown in the picture. Click here to regenerate some new text.
    Click to hear an audio file of the anti-spam word

Our Sponsors
Our Sponsors
Contact Us | FAQ | Sitemap | Terms of Use | Privacy Policy | Tell a Friend

Copyright © 1999-2006 Jeeve Technologies LLC. All rights reserved.