How to reduce the source code to a more simple one ???

This is a discussion on How to reduce the source code to a more simple one ??? within the C Programming forums, part of the General Programming Boards category; Hi , I have completed a programming. Just wondering if anyone have any idea on how to reduce the source ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    21

    How to reduce the source code to a more simple one ???

    Hi ,

    I have completed a programming. Just wondering if anyone have any idea on how to reduce the source code to a more simple one(display part, red color). For this program , i check for whether there is any hexadecimal 10~15. if yes, i display accordingly.
    That mean i am now using 16 "if" function. Not a very tidy programming.
    Anyone with a much better idea ???

    Code:
    #include<stdio.h>
    
    main()
    {
    
     int i,j,ch,decimal,count,search;
     
     int temp;
    
     int hex[10];
     int number[10];
    
     i=0;
     count=0;
     decimal=0;
     
     printf("\nPlease enter your input in decimal form: ");
     ch = getchar();
     
     while(ch != '\n')
        {
            if('0' <= ch && ch <= '9')
             {
              decimal = decimal * 10;
              decimal = decimal + (ch - '0');
             }
          ch = getchar();
        }
    
     printf("\nInput? %d\n",decimal);
    
     while(decimal != 0)
        {
         hex[i] = decimal%16;
         decimal= decimal/16;
         i++;
         count++;
    	}
    	
       i=i-1;		
      for(j=0; j < count; j++,i--)
         {
          number[j]=hex[i];
    	 }	
      
      printf("\nThe hexadecimal is : ");
      
      for(j=0; j < count; j++)
      {
       if(number[j]==0) {putchar('0');}
       else if(number[j]==1) {putchar('1');}
       else if(number[j]==2) {putchar('2');}
       else if(number[j]==3) {putchar('3');}
       else if(number[j]==4) {putchar('4');}
       else if(number[j]==5) {putchar('5');}
       else if(number[j]==6) {putchar('6');}
       else if(number[j]==7) {putchar('7');}
       else if(number[j]==8) {putchar('8');}
       else if(number[j]==9) {putchar('9');}
       else if(number[j]==10) {putchar('A');}
       else if(number[j]==11) {putchar('B');}
       else if(number[j]==12) {putchar('C');}
       else if(number[j]==13) {putchar('D');}
       else if(number[j]==14) {putchar('E');}
       else {putchar('F');}
      } 
     putchar('\n');
    }

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    I've not seen your whole code but just the red part. From it when the number is between 0 to 9 you're just printing the corresponding char of it. So you can shrink it to two lines by using
    Code:
    if number is between 0to9, print the number as it is.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    21
    I need to use putchar() to display the output.
    number[] is int. So i cannot directly display the output using putchar(number[i]) , right ?
    I am struck here. That is why i am using the long method

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,720
    One option to consider:
    Code:
    if (number[j] >= 0 && number[j] < 16)
    {
        putchar("0123456789ABCDEF"[number[j]]);
    }
    By the way, it is good that you posted in code tags, but you could improve on your indentation. Try to use a consistent amount of spaces per indent level (or stick to a single tab). I prefer four spaces, but generally 2 or more would suffice as long as you are consistent.

    You should explicitly declare main as returning an int, and you should explicitly return 0 at the end of the main function unless compiling with respect to the 1999 edition of the C standard.

    Consider breaking up the main function into functions that do one thing and do it well.

    Quote Originally Posted by spurs01
    I need to use putchar() to display the output.
    number[] is int. So i cannot directly display the output using putchar(number[i]) , right ?
    You can, e.g.,
    Code:
    putchar(number[j] + '0');
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    21
    Hi , i don't understand the following code. Can u explain to me again ?
    U mean i should copy the code directly ??


    Code:
    if (number[j] >= 0 && number[j] < 16)
    {
        putchar("0123456789ABCDEF"[number[j]]);
    }

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,720
    Quote Originally Posted by spurs01
    Hi , i don't understand the following code. Can u explain to me again ?
    Basically, it prints the character at index number[j] of the string literal "0123456789ABCDEF", e.g., if number[j] has a value of 10, then it will print 'A', since 'A' is at index 10 of "0123456789ABCDEF".

    Quote Originally Posted by spurs01
    U mean i should copy the code directly ??
    Pretty much, at least if you know where to put it, though you might want to remove that safety check that number[j] is within range since it should be guaranteed by the earlier parts of your program, or you could just leave it there to be paranoid.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Nov 2009
    Posts
    21
    Thanks a lot !! it works perfectly.
    Here is the final version!!

    Code:
    #include<stdio.h>
    
    int main()
    {
    
     int i,j,ch,decimal,count,search;
     
     int temp;
    
     int hex[10];
     int number[10];
    
     i=0;
     count=0;
     decimal=0;
     
     printf("\nPlease enter your input in decimal form: ");
     ch = getchar();
     
     while(ch != '\n')
        {
            if('0' <= ch && ch <= '9')
             {
              decimal = decimal * 10;
              decimal = decimal + (ch - '0');
             }
          ch = getchar();
        }
    
     printf("\nInput? %d\n",decimal);
    
     while(decimal != 0)
        {
         hex[i] = decimal%16;
         decimal= decimal/16;
         i++;
         count++;
    	}
    	
       i=i-1;		
      for(j=0; j < count; j++,i--)
         {
          number[j]=hex[i];
    	 }	
      
      printf("\nThe hexadecimal is : ");
      
        for(j=0; j < count; j++)
         {
          if (number[j] >= 0 && number[j] < 16)
          {
           putchar("0123456789ABCDEF"[number[j]]);
          }   
         } 
      
    
     putchar('\n');
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Weird error in this simple code! C2248!
    By gross100 in forum C++ Programming
    Replies: 2
    Last Post: 12-10-2005, 12:31 AM
  2. Search for patterns in source code
    By MiamiCuse in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 10-23-2005, 11:28 PM
  3. learning to work with SDKs & source code out there...
    By psasidisrcum in forum C++ Programming
    Replies: 3
    Last Post: 05-14-2005, 09:26 PM
  4. 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
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 02:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21