mardi 4 août 2015

Choose a C binary according to the enviroment

I have compiled my code with specific flags (-Os, -O2, -march=native and their combinations) in order to produce a faster execution time.

But my problem is that I don't run always in the same machine (because in my lab there are several different machines). Sometimes I run within a MacOS, or within a Linux (in both cases with different OS versions).

I wonder if there is a way to determine which binary will be run depending on the environment where the binary will run (I mean cache size, cpu cores, and other properties about the specific machine)?. In other words, how to choose (when the program loads) the faster binary (previously compiled with different target binary sizes and instruction-set extensions) according to the specific machine used?

Thanks in advance.



via Chebli Mohamed

sscanf hex ints into array of ints vs. unsigned chars

I am converting a string representation of a mac address into an array of UINT8s defined as unsigned char. I am curious why sscanf() will read all 0s when I read into an array of UINT8s and actual values when I read into an array of regular 32 bit ints. Its almost like it's chopping off the 8 bits of the wrong end of the int.

char strMAC = "11:22:33:AA:BB:CC";

typedef unsigned char UINT8;
UINT8 uMAC[6];

int iMAC[6];

sscanf( (const char*) strMac, 
        "%x:%x:%x:%x:%x:%x", 
        &uMAC[0], &uMAC[1], &uMAC[2], &uMAC[3], &uMAC[4], &uMAC[5] );
printf( "%x:%x:%x:%x:%x:%x", 
        uMAC[0], uMAC[1], uMAC[2], uMAC[3], uMAC[4], uMAC[5] );
// output: 0:0:0:0:0:0

sscanf( (const char*) strMac, 
        "%x:%x:%x:%x:%x:%x", 
        &iMAC[0], &iMAC[1], &iMAC[2], &iMAC[3], &iMAC[4], &iMAC[5] );
printf( "%x:%x:%x:%x:%x:%x", 
        iMAC[0], iMAC[1], iMAC[2], iMAC[3], iMAC[4], iMAC[5] );
// output: 11:22:33:AA:BB:CC



via Chebli Mohamed

Want to grab text from anywhere on my phone screen

I want to code this in java for Android use. The desired outcome is that I could grab a text from my phone screen from anywhere in any other app that displays text on the phone screen. How would I go about coding this app?



via Chebli Mohamed

Plot lat and lon from C code

I'm wondering, is there any way to plot map coordinates directly from C code? I'm getting a lat and lon from a TCP Stream I dissect using Wireshark and I want to create a dynamic map representation of the various points. Is there any API or system I can use to display these coordinates?

One situation I have is that the system has to run on a offline PC, so I would need to cache de various map data.

Is there any possible way of implementing this?

Thank you.



via Chebli Mohamed

What alterations must I do to this code.. I am receiving a Wrong answer message

All submissions for this problem are available.

Pooja would like to withdraw X $US from an ATM. The cash machine will only accept the transaction if X is a multiple of 5, and Pooja's account balance has enough cash to perform the withdrawal transaction (including bank charges). For each successful withdrawal the bank charges 0.50 $US. Calculate Pooja's account balance after an attempted transaction. Input

Positive integer 0 < X <= 2000 - the amount of cash which Pooja wishes to withdraw. Nonnegative number 0<= Y <= 2000 with two digits of precision - Pooja's initial account balance. Output

Output the account balance after the attempted transaction, given as a number with two digits of precision. If there is not enough money in the account to complete the transaction, output the current bank balance.

  • Example - Successful Transaction

Input: 30 120.00

Output: 89.50

  • Example - Incorrect Withdrawal Amount (not multiple of 5)

Input: 42 120.00

Output: 120.00

  • Example - Insufficient Funds

Input: 300 120.00

Output: 120.00

**

  • In reference to the above problem... I am receiving a wrong output message on submission! Can somebody help me out? I believe i have covered all cases! But guys, do take a look

**

#include<stdio.h>
#define bank_charge 0.5
int main()
{
    float X, Y,new_balance;
    X=0, Y = 0;

    printf("With amt and balance ");
    scanf("%f%f", &X, &Y);
    if (X > 0 && X <= 2000 && Y >= 0 && Y <= 2000)
    {
        if (X > Y || ((Y-0.5-X)<0))
        {   printf("Insufficient Funds\n");
            printf("%0.2f", Y);
            return 0;

        }
        if ((int)X % 5 == 0)
        {
            new_balance = (Y - X) - bank_charge;
            printf("%0.2f", new_balance);
            return 0;

        }
        printf("%0.2f", Y);
            }
    return 0;
}



via Chebli Mohamed

how to delete an if statement without any code

I've constructed a Heap by using this code. However, there is one if statement without any codes so I'm trying to avoid using the if statement by giving it to the while statement.

Since the while statement is the opposite with the if. For instance, if(i==1) TRUE while(i!=1) TRUE

I've made the if statement opposite

if((*h).data[temp]<(*h).data[temp*2]&&(*h).data[temp]<(*h).data[temp*2+1])

while((*h).data[temp]>(*h).data[temp*2]&&(*h).data[temp]>(*h).data[temp*2+1])

and the result becomes different.

Here is my real source

int pop(heap* h)
{
int temp=1;
int min=(*h).data[1];
(*h).index--;
//head에 있는 것을 index의 것과 교환
(*h).data[1]=(*h).data[(*h).index];
(*h).data[(*h).index]=NULL;

//root가 가장 작은 경우
while(temp<(*h).capacity)
{
    if((*h).data[temp*2]!=NULL&&(*h).data[temp*2+1]!=NULL)
    {
        if((*h).data[temp]<(*h).data[temp*2]&&(*h).data[temp]<(*h).data[temp*2+1])
        {
        }
        else if((*h).data[temp*2]<(*h).data[temp*2+1])
        {
            //작은 것을 root와 교환
            (*h).data[temp*2]^=(*h).data[temp];
            (*h).data[temp]^=(*h).data[temp*2];
            (*h).data[temp*2]^=(*h).data[temp];
        }
        else if((*h).data[temp*2]>(*h).data[temp*2+1])
        {
            //작을 것을 root와 교환
            (*h).data[temp*2+1]^=(*h).data[temp];
            (*h).data[temp]^=(*h).data[temp*2+1];
            (*h).data[temp*2+1]^=(*h).data[temp];
        }
    }
    else if((*h).data[temp*2]!=NULL)
    {
        if((*h).data[temp*2]<(*h).data[temp])
        {
            (*h).data[temp*2]^=(*h).data[temp];
            (*h).data[temp]^=(*h).data[temp*2];
            (*h).data[temp*2]^=(*h).data[temp];
        }
    }
    temp*=2;
}

return min;

}



via Chebli Mohamed

Output of ++*p++

Can anyone explain me the output.

#include<stdio.h>
int main() {
    int a[]={10,20,30};
    int *p=a;
    ++*p++;
    printf("%d  %d  %d  %d",*p,a[0],a[1],a[2]);
}

output is 20 11 20 30

Postfix incrementation has a higher precedence, so value of second index should have been incremented. Why is the value of first index incremented?



via Chebli Mohamed