C/C++ Coding challenges

madurax86

Member
Jun 29, 2006
4,385
88
0
well if there's one thing I can't stand thats algos lol here goes,
Code:
#include<stdio.h>
#include<stdlib.h>
inline void addhex(char *s, int num){
    switch (num){
        case 15: { *s = 'F'; break; }
        case 14: { *s = 'E'; break; }
        case 13: { *s = 'D'; break; }
        case 12: { *s = 'C'; break; }
        case 11: { *s = 'B'; break; }
        case 10: { *s = 'A'; break; }
        default: { *s = (char)48+num; break; }
    }
}
int main()
{
    unsigned long long int num, pnum, pos=0;
    char out[256]="";
    char out2[256]="";
    
    scanf("%lld", &num);
    
    while (num>16){ 
        pnum=num;
        num=num>>4;
        addhex( &out[pos], pnum-(num<<4));
        pos++;
    }
    addhex( &out[pos], num);
    for (num=0;num<=pos;num++) out2[num] = out[pos - num];
    printf("0x%s ", out2);
    return 0;
}

for those who are interested, heres the timings for decimal 999999999999 on a 2.0 GHz
Code:
999999999999 
0xE8D4A50FFF 
real	0m0.001s
user	0m0.000s
sys	0m0.004s

anyone can edit the code and make improvements, :D
 

Enigma_1

Junior member
  • Feb 5, 2008
    70
    3
    8
    Colombo
    well if there's one thing I can't stand thats algos lol here goes,
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    inline void addhex(char *s, int num){
        switch (num){
            case 15: { *s = 'F'; break; }
            case 14: { *s = 'E'; break; }
            case 13: { *s = 'D'; break; }
            case 12: { *s = 'C'; break; }
            case 11: { *s = 'B'; break; }
            case 10: { *s = 'A'; break; }
            default: { *s = (char)48+num; break; }
        }
    }
    int main()
    {
        unsigned long long int num, pnum, pos=0;
        char out[256]="";
        char out2[256]="";
        
        scanf("%lld", &num);
        
        while (num>16){ 
            pnum=num;
            num=num>>4;
            addhex( &out[pos], pnum-(num<<4));
            pos++;
        }
        addhex( &out[pos], num);
        for (num=0;num<=pos;num++) out2[num] = out[pos - num];
        printf("0x%s ", out2);
        return 0;
    }
    for those who are interested, heres the timings for decimal 999999999999 on a 2.0 GHz
    Code:
    999999999999 
    0xE8D4A50FFF 
    real    0m0.001s
    user    0m0.000s
    sys    0m0.004s
    anyone can edit the code and make improvements, :D

    wow this is grt. fantastic machan. ela kiri.
    :yes::yes::yes:
     

    rclakmal

    Active member
  • May 8, 2008
    698
    183
    43
    In a galaxy far far away
    Code:
    public class HexConverter {
    
        private String s = "";
        private long number;
    
        public HexConverter(long number) {
            this.number = number;
        }
    
        public String convertToHex() {
            long remainder = number;
            while (remainder >= 16) {
                long x;
                x = remainder % 16;
                increment(x);
                remainder = remainder / 16;
            }
            increment(remainder);
            return s;
        }
    
        public void increment(long x) {
            if (x <= 9) {
                s = x+s;
            } else {
                char c = (char) (x + 55);
                s =c+s;
            }
        }
    
        public static void main(String[] args) {
            long start = System.nanoTime();
            System.out.println(new HexConverter(999999999999l).convertToHex());
            long end = System.nanoTime();
            System.out.println("Time Taken:" + (end - start) + "nanosecond");
        }
    }

    Code:
    E8D4A50FFF
    Time Taken:613558nanosecond
     

    rclakmal

    Active member
  • May 8, 2008
    698
    183
    43
    In a galaxy far far away
    Anyway java has a inbuilt function to do this
    Code:
    long start = System.nanoTime();
     System.out.println(Long.toHexString(999999999999l);
      long end = System.nanoTime();
           System.out.println("Time Taken:"+(end-start)+"nanoseconds");

    It takes
    Code:
    e8d4a50fff
    Time Taken:524443nanoseconds
     

    Enigma_1

    Junior member
  • Feb 5, 2008
    70
    3
    8
    Colombo
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int number;
    char* HexNumber =NULL;
    
    int main()
    {
    
        printf("Please enter an integer :");
        scanf("%d",&number);
        HexNumber =(char*)malloc(50);
        sprintf(HexNumber,"%x",number);
        printf("1st method : hex representation is 0x%s\n",HexNumber);
        printf("2nd method : hex representation is 0x%p\n",number);
        printf("3rd method : hex representation is 0x%x\n",number);
        system("pause");
        return 0;
    }
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    unsigned int number,modval,Divval,index=0;
    char* HexNumber =NULL;
    char* tem;
    
    int main()
    {
        printf("Please enter an integer :");
        scanf("%d",&number);
        HexNumber =(char*)malloc(50);
        tem =(char*)malloc(3);
        modval = number%16;
        Divval = (int)number/16;
        if(Divval<16)
        {
            sprintf(tem,"%d%d",Divval,modval);
            strcpy(HexNumber,tem);
            printf("Hex value is :%s\n",HexNumber);
            system("pause");
            exit(0);
        }
        else
        {
            sprintf(tem,"%d",modval);
            strcpy(HexNumber,tem);
            HexNumber = HexNumber+1;
            index++;
        }
        while(Divval>16)
        {
            modval = Divval%16;
            switch(modval)
            {
            case 10:
                strcpy(HexNumber,"A");
                break;
            case 11:
                strcpy(HexNumber,"B");
                break;
            case 12:
                strcpy(HexNumber,"C");
                break;
            case 13:
                strcpy(HexNumber,"D");
                break;
            case 14:
                strcpy(HexNumber,"E");
                break;
            case 15:
                strcpy(HexNumber,"F");
                break;
            default:
                sprintf(tem,"%d",modval);
                strcpy(HexNumber,tem);
                break;
            }
            HexNumber = HexNumber+1;
            Divval = (int)Divval/16;
            index++;
        }
        sprintf(tem,"%d",Divval);
        strcpy(HexNumber,tem);
        printf("The Hex value is 0x");
        while(strlen(HexNumber)!=(index+2))
        {
            printf("%c",HexNumber[0]);
            HexNumber--;
        }
        printf("\n");
        system("pause");
        return 0;
    }
    here are my solutions.
    :D:D
     
    Last edited:

    Enigma_1

    Junior member
  • Feb 5, 2008
    70
    3
    8
    Colombo
    Code:
    public class HexConverter {
    
        private String s = "";
        private long number;
    
        public HexConverter(long number) {
            this.number = number;
        }
    
        public String convertToHex() {
            long remainder = number;
            while (remainder >= 16) {
                long x;
                x = remainder % 16;
                increment(x);
                remainder = remainder / 16;
            }
            increment(remainder);
            return s;
        }
    
        public void increment(long x) {
            if (x <= 9) {
                s = x+s;
            } else {
                char c = (char) (x + 55);
                s =c+s;
            }
        }
    
        public static void main(String[] args) {
            long start = System.nanoTime();
            System.out.println(new HexConverter(999999999999l).convertToHex());
            long end = System.nanoTime();
            System.out.println("Time Taken:" + (end - start) + "nanosecond");
        }
    }
    Code:
    E8D4A50FFF
    Time Taken:613558nanosecond

    Ah ela kiri machan. :yes::yes:
     

    madurax86

    Member
    Jun 29, 2006
    4,385
    88
    0
    Anyway java has a inbuilt function to do this
    Code:
    long start = System.nanoTime();
     System.out.println(Long.toHexString(999999999999l);
      long end = System.nanoTime();
           System.out.println("Time Taken:"+(end-start)+"nanoseconds");

    It takes
    Code:
    e8d4a50fff
    Time Taken:524443nanoseconds

    hey, nice seeing you again :D
    BTW that time i gave was the process time,(=time it takes to load the program up + time it takes to run until it ends).
     

    rclakmal

    Active member
  • May 8, 2008
    698
    183
    43
    In a galaxy far far away
    hey, nice seeing you again :D
    BTW that time i gave was the process time,(=time it takes to load the program up + time it takes to run until it ends).

    Same here machan :D ahhhh Yr I get it !! Anyway if you consider the same algorithm implemented in c++ and java ,java uses more resources because it runs on the JVM . Hoping to see more and more works from u in Elakiri !!!:D
     

    Enigma_1

    Junior member
  • Feb 5, 2008
    70
    3
    8
    Colombo
    well if there's one thing I can't stand thats algos lol here goes,
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    inline void addhex(char *s, int num){
        switch (num){
            case 15: { *s = 'F'; break; }
            case 14: { *s = 'E'; break; }
            case 13: { *s = 'D'; break; }
            case 12: { *s = 'C'; break; }
            case 11: { *s = 'B'; break; }
            case 10: { *s = 'A'; break; }
            default: { *s = (char)48+num; break; }
        }
    }
    int main()
    {
        unsigned long long int num, pnum, pos=0;
        char out[256]="";
        char out2[256]="";
        
        scanf("%lld", &num);
        
        while (num>16){ 
            pnum=num;
            num=num>>4;
            addhex( &out[pos], pnum-(num<<4));
            pos++;
        }
        addhex( &out[pos], num);
        for (num=0;num<=pos;num++) out2[num] = out[pos - num];
        printf("0x%s ", out2);
        return 0;
    }
    for those who are interested, heres the timings for decimal 999999999999 on a 2.0 GHz
    Code:
    999999999999 
    0xE8D4A50FFF 
    real    0m0.001s
    user    0m0.000s
    sys    0m0.004s
    anyone can edit the code and make improvements, :D

    This is grt. I ran his code with a windows high resolution timer. results are outstanding. by considering all the other codes (including mine) I believe this is the fastest solution for the above challenge. :yes::yes:. elama thama machan. keep it up. :D:D
     

    DURApix

    Well-known member
  • Aug 1, 2010
    1,259
    192
    63
    /dev/null
    hello friends this is my answer to dat problem..
    i used an integer array for the conversion process.
    :)

    //(c)dURA :)
    #include<iostream>
    using namespace std;

    int main()
    {
    int now,x,j;
    begin:

    const int size=20;
    int no[size]={0};

    cout<<"Please enter the integer value : ";
    cin>>x;

    now=x;
    int i=0;
    for(i=0;;i++)
    {
    if (now==0) break;
    no=now%16;
    now=now/16;
    if (now<16 and now>0)
    {i=i+1;no=now;break;}
    }

    cout<<"\nhexadecimal representation of "<<x<<" is 0x";

    for(j=i;j>=0;j--)
    {
    if (no[j]>9)
    {
    switch (no[j]){
    case 10 : cout<<"A";break;
    case 11 : cout<<"B";break;
    case 12 : cout<<"C";break;
    case 13 : cout<<"D";break;
    case 14 : cout<<"E";break;
    case 15 : cout<<"F";break;
    default : cout<<"Z";break;
    }
    }
    else cout<<no[j];
    }

    cout<<"\n\t\t____\n\n";
    goto begin;
    }