samedi 25 avril 2015

fast way to get integers 0, 1, and 2 when given a random one from the set

So basically

int num = rand(2); //random number from 0-2
int otherNum, otherOtherNum;
otherNum = implement this
otherOtherNum = implement this

For example, if num is 2, otherNum and otherOtherNum must be set to 0 and 1 (or 1 and 0).

How would you implement this? Assume you can't use branching or look up tables.

I think a lookup might be the fastest but not sure, I dont like that solution though.

Creating multiple threads in C: write pid, tid and return integer

I must do this:

Write a program whose main thread creates 3 other threads. Each ot these threads (different from the main thread) shall write its pid and tid and terminate returning an integer number between 1 and 3 and different from the value returned by the other threads. The main thread shall print in the standard out its pid and the value returned by each of the other threads.

I think I'm doing it right, except that I don't understand how should I return the integer numbers between 1 and 3

I've the following code so far:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#define DEBUG 0

// int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg);

// int pthread_join(pthread_t, void **value_ptr);

void *printID(void *arg){

    int i;

    unsigned long tid;
    tid = *((unsigned long *) arg);           

    printf("Thread tid: %lu Thread pid: %d \n", tid, getpid());

}

int main(int argc, char *argv[]) {

    pthread_t id1, id2, id3;

    pthread_create(&id1, NULL, printID, &id1);

    pthread_create(&id2, NULL, printID, &id2);

    pthread_create(&id3, NULL, printID, &id3);

    pthread_join(id1, NULL);

    pthread_join(id2, NULL);

    pthread_join(id3, NULL);

    printf("Main thread pid: %d\n", getpid());

    sleep(1);

}

Random function changing randomly [on hold]

m looking for a random function to generate random numbers which (internally) changes randomly and should be unpredictable by the hackers....can anyone please provide me with some suggestions?...you all could also suggest me to develop my own function.

And how should I develop a relation between two random functions changing randomly? (if I use same function at two different places)...I hope that I am clear enough

Trouble with Bailey-Borwein-Plouffe formula

I am trying to implement the BBP formula. I wrote the following quick and dirty test code, based on the paper by Paul H. Bailey (that I found here):

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <gmp.h>
#include <limits.h>

long bin_exp_mod_k(int base, int exponent, int div) {
    long expmod = 1;
    long t;

    t = (long) pow(2, floor(log(exponent)/log(2.0)));

    while(42) {
        if(exponent >= t) {
            expmod = (base * expmod) % div;
            exponent -= t;
        }

        t = t/2;

        if(t >= 1) {
            expmod = ((long)pow(expmod, 2)) % div;
        } else
            break;
    }

    return expmod;
}

void bbp_part(mpf_t rop, int base, int index, int digit_index, int num_iter) {
    const int knum = num_iter;
    int k;
    long ldiv;
    mpf_t fbase;
    mpf_t fst_sum;
    mpf_t fst_sum_int;
    mpf_t snd_sum;
    mpf_t powval;
    mpf_t div;

    mpf_init(fst_sum);
    mpf_init(snd_sum);
    mpf_init(fst_sum_int);
    mpf_init(powval);
    mpf_init(div);
    mpf_init(rop);
    mpf_init_set_si(fbase, base);

    for(k = 0;k <= digit_index;k++) {
        ldiv = 8 * k + index;
        mpf_set_si(powval, bin_exp_mod_k(base, digit_index - k, ldiv));
        mpf_set_si(div, ldiv);
        mpf_div(powval, powval, div);
        mpf_add(fst_sum, fst_sum, powval);
    }

    mpf_trunc(fst_sum_int, fst_sum);
    mpf_sub(fst_sum, fst_sum, fst_sum_int);

    for(k = digit_index + 1;k < knum;k++) {
        ldiv = 8 * k + index;
        mpf_set_si(div, ldiv);
        mpf_pow_ui(powval, fbase, digit_index - k);
        mpf_div(powval, powval, div);
        mpf_add(snd_sum, snd_sum, powval);
    }

    mpf_set(rop, fst_sum);
    mpf_add(fst_sum, fst_sum, snd_sum);

    printf("S%i: %.20lf\n", index, mpf_get_d(rop));

    mpf_clear(fst_sum);
    mpf_clear(snd_sum);
    mpf_clear(fbase);
    mpf_clear(fst_sum_int);
    mpf_clear(powval);
    mpf_clear(div);
}

int main(int argc, char **argv) {
    const int base = 16;
    int d;
    int num_iter;
    mpf_t pi_digits;
    mpf_t part1;
    mpf_t part1_int;
    mpf_t part4;
    mpf_t part4_int;
    mpf_t part5;
    mpf_t part5_int;
    mpf_t part6;
    mpf_t part6_int;

    if(argc == 1) {
        return -1;
    }

    d = atoi(argv[1]);

    if(argc == 3) {
        num_iter = atoi(argv[2]);
    } else {
        num_iter = INT_MAX;
    }

    mpf_set_default_prec(128);

    mpf_init(pi_digits);

    mpf_init(part1_int);       
    mpf_init(part4_int);
    mpf_init(part5_int);
    mpf_init(part6_int);

    bbp_part(part1, base, 1, d, num_iter);
    bbp_part(part4, base, 4, d, num_iter);
    bbp_part(part5, base, 5, d, num_iter);
    bbp_part(part6, base, 6, d, num_iter);

    mpf_trunc(part1_int, part1);
    mpf_trunc(part4_int, part4);
    mpf_trunc(part5_int, part5);
    mpf_trunc(part6_int, part6);

    mpf_sub(part1, part1, part1_int);
    mpf_sub(part4, part4, part4_int);
    mpf_sub(part5, part5, part5_int);
    mpf_sub(part6, part6, part6_int);

    mpf_mul_ui(part1, part1, 4L);
    mpf_mul_ui(part4, part4, 2L);

    mpf_set(pi_digits, part1);
    mpf_sub(pi_digits, pi_digits, part4);
    mpf_sub(pi_digits, pi_digits, part5);
    mpf_sub(pi_digits, pi_digits, part6);

    mpf_clear(pi_digits);
    mpf_clear(part1);
    mpf_clear(part4);
    mpf_clear(part5);
    mpf_clear(part6);
    mpf_clear(part1_int);
    mpf_clear(part4_int);
    mpf_clear(part5_int);
    mpf_clear(part6_int);

    return 0;
}

and it seems to be correct, because according to his paper the partial results I get for S1, S4, S5 and S6 are, with a small precision difference, the same ones noted.

However, I can't figure out what I did miss with the "combining the results" part. No matter what I do, I get 0.57... instead of the 0.42... written on the paper.

Can someone see what am I missing here?

incrementing values in if loop (in C)

I am writing a self-controlled Pacman game (Pacman moves by itself by algorithms which decide it's movement, needs to chase ghosts, etc.) in C. Keep in mind that I am very new to programming in C so the method I use may not be a good one at all.

Anyways, I stumbled upon a problem.

my Pacman moves in an Array map with it's placement at

Map[yPacman][xPacman] == place of Pacman;

Now, when Pacman is in "rage mode"; after picking up a sweet, I would like him to find the ghosts he seeks. The ghost being a char 'G' in the array.

Now, I would like Pacman to see ahead of him to catch and follow the ghost when it reaches a distance near him. Let's say he's moving to the left; I would him to look 5 "X-Coords" left of him, including the X-coords "1,2,3,4,5". When any of these coordinates equal to a char 'G', it'd have to follow him.

Now, I would like to implement it, but I can't seem to find a way how to.

if (Map[yPacman][xPacman-i] == 'G')  { }

With "i" having a value from 1-5. Incrementing it during the process itself. I would not like to use

if (Map[yPacman][xPacman-1] == 'G' || Map[yPacman][xPacman-2] == 'G'|| Map[yPacman][xPacman-3] == 'G'|| Map[yPacman][xPacman-4] == 'G'|| Map[yPacman][xPacman-5] == 'G')  { }

Even though this is the exact result I would need.

I personally think it seems very easy to solve how to do this. But at the moment I have no clue. Thanks in advance

Pointer for pointer

I try to insert new node to tree without using recursive function.

Root is global defined first node of tree.

node *tree;
tree=&root;

.......

(tree->tab[first])=(node*)malloc(sizeof(node));

Where node is my own defined structure. I want to have handle to root, so I make new pointer, which I will use to move on tree. I m very suprised because new allocated nodes arent assigned to my root. How can I do that? How can edit root by editing tree.

Please help, I have spent hours for that problem. Greetings

matrix represents tree the function returns 0

I have some homwrok and it looks like this: Question 1 ( First program adjacency.c file ) Directed tree structure T has N nodes represented by the adjacency matrix A size NxN as follows:

A [ u ] [ v] == TRUE if and only if there is a directed arc from u to v in T , or in other words : u is the parent of v . In the following example a tree with N = 11 nodes :

tree

We obtain the following neighboring matrix :

matrix

this are the questions:

A. You must define with #define command and / or enum the N and permanent TRUE and FALSE. Typedef should be set with a character named adj_mat defines the neighboring matrix size N.

B. You must write a function called path, which accepts as a parameter adjacency matrix A and indexes of two nodes u and v and returns TRUE if and only if there is a directed path (by directional arrow) at the intersection u v, the tree is represented by a matrix A. Otherwise it returns FALSE.

For example: path (1,8) will return TRUE. The same path (1,3). On the other hand path (3,8) will FALSE.

C. First you must write a function (main) defines a variable type adj_mat, asks the user entries for this matrix, and indexes of the two nodes. The main function function call path, to see if there is a directed path between two nodes in the data. The function to print the test result output.

have to get some help guys

#include <stdio.h>

#define N 11
enum {FALSE, TRUE};
typedef int adj_mat[N][N];

int path2(adj_mat A, int u, int v, int temp)
{
if(u == temp && A[u][v] == FALSE)
return TRUE;

if(u == temp && A[u][v] == FALSE)
return FALSE;

if(A[u][v] == FALSE)
return path2(A, u-1, v, temp);

if(A[u][v] == TRUE)
return path2(A, N, u, temp);

return FALSE;
}

int path(adj_mat A, int u, int v)
{
return path2(A, N, v, u);
}



int main()
{

int arr[N][N]= {{0,1,1,1,0,0,0,0,0,0,0},{0,0,0,0,1,1,1,1,1,0,0},
{0,0,0,0,0,0,0,0,0,1,0},{0,0,0,0,0,0,0,0,0,0,1},{0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0}};
int u;
int v;
printf("please enter two numbers \n");
scanf("%d %d", &u, &v);
printf("The answer is %d", path(arr, u, v),".");
return 0;
}

The problem is at the terminal when i put 1,8 it returns 0. please help me guys im going inasane