Thread: Arrays/do-while loops problem

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    25

    Lightbulb Arrays/do-while loops problem

    ok so here is the problem : "Using an array, write a program to convert a decimal number (from 0 to 31) to binary number

    here is my program so far
    Code:
    "libraries are included"
    
    int main(){
        int array,de,i,max=2147483648;
                  do
                  {
                   printf ("Enter a Decimal number (if greater than 31 then it will quit): "); 
                   scanf ("%d", &array);
                   printf("\nThe Binary equivalent of %d is: \n", array);
                         for (int i=0;i<32;i++)
                             {
                                 array%2;
                                  
                                 if (array==0 && max==0)
                                   {
                                      printf("0");
                                   }
                   
                                 else
                                   {
                                      printf("1");
                                   }
                                 if (array>= 31 )
                                   {
                                      return(0);
                                   }
                                      array=array<<1; 
                             }
                   }while(1);                              
    getchar();
    getchar();
    }
    now i have one question, when i try and run it it doesn't stay open so im not even sure this is right

    any help on this program would be appreciated thanks in advance

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Where's the array?

    You also need to indent your code better, e.g.,
    Code:
    "libraries are included"
    
    int main() {
        int array, de, i, max = 2147483648;
        do
        {
            printf ("Enter a Decimal number (if greater than 31 then it will quit): ");
            scanf ("&#37;d", &array);
            printf("\nThe Binary equivalent of %d is: \n", array);
            for (int i = 0; i < 32; i++)
            {
                array % 2;
    
                if (array == 0 && max == 0)
                {
                    printf("0");
                }
                else
                {
                    printf("1");
                }
    
                if (array >= 31)
                {
                    return(0);
                }
                array = array << 1;
            }
        } while (1);
        getchar();
        getchar();
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    25
    oh ok i get that part im going to limit my array to 31 numbers so i put array[32]

    Code:
    int main(){
        int array[32],i,max=2147483648;
                  do
                  {
                   printf ("Enter a Decimal number (if greater than 31 then it will quit): "); 
                         scanf ("&#37;d", &array[32]);
                   printf("\nThe Binary equivalent of %d is: \n", array[32]);
                         for (int i=0;i<32;i++)
                             {
                                                        
                                 if (array[32]==0 && max==0)
                                   {
                                      printf("0");
                                   }
                   
                                 else
                                   {
                                      printf("1");
                                   }
                                   
                                 if (array[32] >= 31 )
                                   {
                                      return(0);
                                   }
                                      array[32]=array[32]<< 1; 
                             }
                   }while(1);                              
    getchar();
    getchar();
    }
    but this what i don't get, do i put array[32] to everything?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    hmm... it looks like you do not know how to use arrays.

    Here's a useful digression: write code that will fill the array with the integers from 0 to 31 inclusive. Once you can do this, then we can talk about the problem that you are trying to solve.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP with windows message loops problem
    By cloudy in forum C++ Programming
    Replies: 3
    Last Post: 01-21-2006, 01:09 PM
  2. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  3. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM