Thread: Help with few programs..

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    6

    Help with few programs..

    I came across an interview process and i couldn able to answer few of their questions. But i want to enlighten myself with the answers of the questions.. plz help me with the answers to those questions. .. . .

    1 . Program to truncate a given floating point value (e.g.16.25=16). You should not assign the float value to integer & then copy the int value to float….


    2 . Program to find the position of decimal point in a given floating point value.


    3. Program to swap two numbers without using assignment operator in any part of the program.



    i answered others questions ... but found it confusing

  2. #2
    Registered User GL.Sam's Avatar
    Join Date
    Aug 2009
    Posts
    88
    In order to answer the first two questions you need to know how float is represented internally. Here's a handy link:
    Clusty Search » ieee floating point

    >3. Program to swap two numbers without using assignment operator in any part of the program.
    It could be done using XOR.

    EDIT: Oh, I thought "without temp variable" lol.
    Last edited by GL.Sam; 05-12-2010 at 11:39 AM.
    The only good is knowledge and the only evil is ignorance.
    ~Socrates

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    6
    i found out the answer for the 3rd question..
    u could use inline asm code ...

    like asm mov ax,a
    this will solve that assingment operator problem..

  4. #4
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    I'm sure it won't.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    6
    Quote Originally Posted by Brafil View Post
    I'm sure it won't.
    got any idea other than that ?? ? ? ??!?!

  6. #6
    Registered User GL.Sam's Avatar
    Join Date
    Aug 2009
    Posts
    88
    >3. Program to swap two numbers without using assignment operator in any part of the program.
    Okay, the only thing that came into my mind is to read values from file/stdin, then make a temporary file, write them there, and read backwards. Looks dumb, but as for me, the very question looks pretty dumb either.
    The only good is knowledge and the only evil is ignorance.
    ~Socrates

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    6
    #include < stdio.h >
    #include < conio.h >
    void main()
    {
    int a,b;
    clrscr();
    printf("\nEnter variables to swap-->");
    scanf("%d %d",&a,&b);
    printf("\nBefore swapping:- %d %d",a,b);
    asm mov AX,a;
    asm mov BX,b;
    asm mov b,AX;
    asm mov a,BX;
    printf("\nAfter swapping :- %d %d",a,b);
    getch();
    }

  8. #8
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    GL.Sam had already posted the solution - simply XOR the two numbers, but beware, if they are the same, they will both be 0. I think this is what your instructors want - just google "xor swap".

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    6
    Quote Originally Posted by Brafil View Post
    GL.Sam had already posted the solution - simply XOR the two numbers, but beware, if they are the same, they will both be 0. I think this is what your instructors want - just google "xor swap".
    XOR will still require an assignment statement " = " .

  10. #10
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    No. You can use "^=", which is a compound operator.

  11. #11
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Brafil View Post
    No. You can use "^=", which is a compound operator.
    I'd consider that an assignment operator...
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  12. #12
    Registered User GL.Sam's Avatar
    Join Date
    Aug 2009
    Posts
    88
    I've found it
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
    	int a, b, temp;
    
    	scanf("%d %d", &a, &b);
    
    	memcpy(&temp, &a, sizeof(int));
    	memcpy(&a, &b, sizeof(int));
    	memcpy(&b, &temp, sizeof(int));
    
    	printf("%d %d\n", a, b); 
    
    	return 0;
    }
    The only good is knowledge and the only evil is ignorance.
    ~Socrates

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It kinda takes away from the whole "homework" aspect if you just show up and someone else does everything for you.


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User
    Join Date
    May 2010
    Posts
    6
    Quote Originally Posted by quzah View Post
    It kinda takes away from the whole "homework" aspect if you just show up and someone else does everything for you.


    Quzah.
    dude.. i said it was asked on my interviews. It was not an homework btw and am not a kid going to school. I just messed up that interview coz these kinds of logical questions.. i want to make myself clear thats why posted here.. :|

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Maybe you should go to school. I mean, you're interviewing for some sort of programming job, but you don't know how to program.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating C programs to test memory usage
    By dsollen in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2010, 10:12 AM
  2. Recommend upgrade path for C programs
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-22-2007, 07:32 AM
  3. Way to get the current open programs displayed?
    By CPPguy1111 in forum C++ Programming
    Replies: 6
    Last Post: 06-22-2005, 12:24 AM
  4. POSIX/DOS programs?
    By nickname_changed in forum C++ Programming
    Replies: 1
    Last Post: 02-28-2003, 05:42 AM
  5. executing c++ programs on the web
    By gulti01 in forum C++ Programming
    Replies: 4
    Last Post: 08-12-2002, 03:12 AM