Thread: Need help in an Analog to Digital converter program

  1. #1
    Registered User
    Join Date
    Jan 2011
    Location
    Japan
    Posts
    3

    Question Need help in an Analog to Digital converter program

    Hello. I'm currently trying to write a program that will convert a decimal number (0 ~ 65535) i insert into binary numbers. The problem is i want the conversion to happen using the principle of a Successive Approximation type A-D converter, where the process of conversion will be showed on the screen.

    For example: when i enter number 103 for conversion, the output will be:

    1000000
    1100000
    1100000
    1100000
    1100100
    1100110
    1100111

    with the final row 1100111 as the answer. The process will be concluded in a maximum 32 steps.

    So far, i have only managed to write a program with direct conversion without the process of converting showing on the screen.

    Code:
    #include<stdio.h>
    
    int main(void){
    int num,weight=1;
    printf("Insert a positive number:");
    scanf("%d",&num);
    printf("Change %d to binary\n",num);
    
    while(num-weight*2>=0){
    weight *= 2;
    }
    while(weight){
    printf("%d",num-weight>=0);
    if(num-weight>=0){
    num -= weight;
    }
    weight /= 2;
    }
    
    puts("");
    return 0;
    }

    Thank you for your time. This is my first post here. I'm still a beginner in programming and sorry for my bad english. Please help.
    Last edited by hiubaocong; 01-17-2011 at 09:07 AM. Reason: Wrong spelling in title

  2. #2
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Don't worry, your english is pretty damn good.

    You are printing each digit seperately, which means that you won't be able to achieve what you want.

    Instead of printing every digit seperately, you could essentially copy the bits one-by-one from one number to another, printing the new number each time. You would probably have to write a function to print the number in binary, but you already have the logic for this in your current program. (http://www.cprogramming.com/tutorial...operators.html)

    You could also just store the digits in an array, and print the array each time.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Location
    Japan
    Posts
    3
    Thank you. I will try it. And thanks for the compliment

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Either store each successive approximation in an array, as suggested, or use recursion.

  5. #5
    Registered User
    Join Date
    Jan 2011
    Location
    Japan
    Posts
    3
    Thank you. I'm still trying.

Popular pages Recent additions subscribe to a feed