Thread: Binary!!! How do I manage that?

  1. #16
    Registered User
    Join Date
    Jan 2007
    Posts
    14
    Quote Originally Posted by Prelude
    Let's start by fixing the glaring problems.

    First, you need to include <stdio.h>. You would also need to include <conio.h> if I weren't recommending that you not use clrscr.

    Always. No excuses. Yes, I've heard them all before, and no, I'm not interested in hearing them again. Just use it.

    >int a = 12;
    You don't use this, and in the current code it's just filler anyway.

    >clrscr();
    This is a great way to ........ users off. I can guarantee that if I start a console program and it clears all of my previous output from other programs, I'll never use it again.

    >count++;
    You just invoked undefined behavior by accessing an indeterminate value from an uninitialized variable.

    >how can I re-write this as a while loop?
    If you don't mind the binary value in reverse, it's trivial:

    If you do mind, you need to cache the results and then print them properly. Using an array, for example.
    Well its just college C... I included the files I needed or duh it won't complile mr 9000 + posts!

    Plus the 12 is just as a refernece for ease-of-use to get the thing working...

    And yes your code works nicely! But I DO remind reverse... does anyone know how to make it print the right-way-round?

  2. #17
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Quote Originally Posted by expresspotato
    I included the files I needed or duh it won't complile mr 9000 + posts!
    Then put it in your posts. And don't make Prelude angry.
    Quote Originally Posted by expresspotato
    Well its just college C...
    So? That's what you'll do in the real world, too.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  3. #18
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    >does anyone know how to make it print the right-way-round??
    What do you want. Which one is that??

    ssharish2005

  4. #19
    Registered User
    Join Date
    Jan 2007
    Posts
    14
    Quote Originally Posted by ssharish2005
    >does anyone know how to make it print the right-way-round??
    What do you want. Which one is that??

    ssharish2005
    Sorry about making whoever angry... I really didn't mean to, hes clearly pro tho and me mr noooob.


    But yeah which do I want? the right way round, with the while loop!
    His version prints it backwards, heh :S and no u can't just store it as a string then like swap it round ;(

    who can do it!

  5. #20
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    A whole bunch of us can do it. Let's see your attempt. Lazy. You haven't done a damn thing here, other than cry because people are "clearly pro". You're not "mr noooob". You're "mr. too ........ing lazy to even make an attempt".


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

  6. #21
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Well the answer for u is in the Prelude post. Go through it. She has mentioned it.

    ssharish2005

  7. #22
    Registered User
    Join Date
    Jan 2007
    Posts
    14
    Quote Originally Posted by quzah
    A whole bunch of us can do it. Let's see your attempt. Lazy. You haven't done a damn thing here, other than cry because people are "clearly pro". You're not "mr noooob". You're "mr. too ........ing lazy to even make an attempt".


    Quzah.

    Okay you can take whatever anger out on me its kewl!

    But anyways here's my attempt... :S

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    void convert(int);
    
    main (){
    	int x;
    	//Sample X
    	x = 12;
    
    	clrscr();
    
    	convert(x);
      getche();
    }
    
    int convert(int x){
    
    	int z,i,ans, d = 0x00, count=1;
    
    	printf("X (%x) or D (%x) \n",x,d);
    
    	z = x & d;
    
    	for (i = 0; i <8; i++){
    
    		if (z>0){printf("1");}
    		if (z<0){printf("0");}
    
    		if (count >= 4){printf(" "); count = -1;}
    
    		z = z<<1;
    		count++;
    	}
    }

  8. #23
    Registered User
    Join Date
    Jan 2007
    Posts
    14
    Quote Originally Posted by ssharish2005
    Well the answer for u is in the Prelude post. Go through it. She has mentioned it.

    ssharish2005
    Nah it has to print the right time-around...

  9. #24
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Have you read anything?
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  10. #25
    Registered User
    Join Date
    Jan 2007
    Posts
    14
    Quote Originally Posted by manutd
    Have you read anything?
    what do you mean? like here in the thread? yeah...


    LOL don't hate! Appreciate!

    But outside of the forum no, just my crazy cracka professor that reminds me of tweak from southpark, he didn't teach this well if at all...

  11. #26
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Do you think your program prints some think. It dosn't. Well before that aren't u trying to print the binary values in the right form??

    ssharish2005

  12. #27
    Registered User
    Join Date
    Jan 2007
    Posts
    14
    Wacha talkin' about, I thought it printed something... jeeze!

    THATS WHY I"M HERE LOOKING FOR HELP, you programmers sure got an attitude problem!

  13. #28
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    OK, I'll be nice.
    Quote Originally Posted by expresspotato
    you programmers sure got an attitude problem!
    Yeah, so
    Anyways, here's how the code should look:
    Code:
    #include <stdio.h>#include <conio.h>	//Eww. See below.
    void convert(int);
    int main (void){	
         int x;	//Sample X	
         x = 12;	
         //clrscr();	
         convert(x);  
         getche();	//Eww. View the FAQ.
    }
    int /*void*/ convert(int x)
    {	
         int z,i,ans, d = 0x00, count=1;	
         printf("X (%x) or D (%x) \n",x,d);	
         z = x & d;	
         for (i = 0; i <8; i++)
         {		
              if (z>0){printf("1");}		
              if (z<0){printf("0");}		
              if (count >= 4){printf(" "); 
              count = -1;
              }		
              z = z<<1;		
              count++;	
         }
    }
    That's the syntax stuff. The rest is for you to fix.
    Last edited by manutd; 01-23-2007 at 08:04 PM.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  14. #29
    Registered User
    Join Date
    Jan 2007
    Posts
    14
    THANK YOU MANUTD!

    I will test and report back! lol


    Hurray, hell has frozen over

  15. #30
    Registered User
    Join Date
    Jan 2007
    Posts
    14
    Quote Originally Posted by manutd
    OK, I'll be nice.
    Yeah, so
    Anyways, here's how the code should look:
    Code:
    #include <stdio.h>#include <conio.h>	//Eww. See below.
    void convert(int);
    int main (void){	
         int x;	//Sample X	
         x = 12;	
         //clrscr();	
         convert(x);  
         getche();	//Eww. View the FAQ.
    }
    int /*void*/ convert(int x)
    {	
         int z,i,ans, d = 0x00, count=1;	
         printf("X (%x) or D (%x) \n",x,d);	
         z = x & d;	
         for (i = 0; i <8; i++)
         {		
              if (z>0){printf("1");}		
              if (z<0){printf("0");}		
              if (count >= 4){printf(" "); 
              count = -1;
              }		
              z = z<<1;		
              count++;	
         }
    }
    That's the syntax stuff. The rest is for you to fix.

    What you didn't actually fix anything! Syntax-shmintax, it ran before, you're code didn't even run off the bat!

    still no dice on printing binary here, I think I'm about to cry! :'(

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays vs lists? And containers in general!
    By clegs in forum C++ Programming
    Replies: 22
    Last Post: 12-03-2007, 02:02 PM
  2. Replies: 0
    Last Post: 11-04-2006, 11:07 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM