c - Changing code to make it parallel -


   #include <conf.h>    #include <kernel.h>    #include <stdio.h>    #define max_items 50    typedef long double ld;    int run_solve_equ();    void xmain(){        int pid;        pid = create(run_solve_equ, 20*initstk, initprio, "b", 0);        resume(pid);    }    int solve_equ(ld b, int n, ld a[], int choosen[] ){        if ( n == 1 )            if (a[0] == b) {                choosen[0] = 1;                return 1;            } /* if */            else if (b == 0) {                choosen[0] = 0;                return 1;            } /* else if*/            else {                choosen[0] = 0;                return 0;            }        else /* n > 1 */            if (solve_equ(b, n-1, a, choosen)) {                choosen[n-1] = 0;                return 1;            } /* if */        else if (solve_equ(b - a[n-1],n-1, a, choosen)) {            choosen[n-1] = 1;            return 1;        } /* else if */        else{            choosen[n-1] = 0;            return 0;        }    } /* solve_equ */    ld a[max_items];    int choosen[max_items];    char pstr[200];    extern long int tod;    int run_solve_equ() {        int n, i, result;        ld b, sum;        printf("how many numbers? no more %d:", max_items );        scanf("%d",&n);        puts("enter b:");        scanf("%lf",&b);        a[0] = 1;        (i = 1; < n; i++)            a[i] = a[i-1]*2;        result = 0;        sprintf(pstr, "time = %ld\n", tod);        printf(pstr);        result = solve_equ(b,n, a, choosen);        sprintf(pstr, "time = %ld\n", tod);        printf(pstr);        sprintf(pstr, "solution b = %lf, n = %d, value = %d :\n", b,            n,result);        printf(pstr);        printf("\ni:\n");        (i = 0; < n; i++) {            sprintf(pstr, "%-16d",i);            printf(pstr);        } //        printf("\na[i]:\n");        (i = 0; < n; i++) {            sprintf(pstr, "%-16.1lf", a[i]);            printf(pstr);        } //        printf("\nchoosen[i]:\n");        (i = 0; < n; i++) {            sprintf(pstr, "%-16d", choosen[i]);            printf(pstr);        } //        printf("\n");        sum = 0;        (i = 0; < n; i++)            if (choosen[i]) {                sum += a[i];                sprintf(pstr, " + %-16.1lf", a[i]);                printf(pstr);            } /* if */        sprintf(pstr, " = %-16.1lf\n", sum);        printf(pstr);        return 0;    } 

i need change program search "parallel" 2 processes: each search n-1, first user on [n-1] , other not. problem find number in array number togother equal number b !

not answer, have constructive comments you.

reading above code hurts. hurts eyes , hurts brain. why?

  1. variable names non-descriptive 1 letter (outside of i). b, n, a - don't know these @ glance , don't want find out understanding wrote. bet won't understand wrote next week either. favor , make names better.
  2. please, please use bit of white space between "code thoughts". code looks 1 long run-on sentence hides does.
  3. don't mix braced , non-braced code - when indentation not consistent. very, difficult follow if logic as-is right now. go ahead , waste few bytes of hard disk space , perhaps .25 seconds adding braces or @ least fixing indents.
  4. write useful comments. /* if */ terrible comment , similar stuff everywhere. know if is, perhaps quick not of checking?
  5. don't mix globals , extern defines in middle of code. keep them , makes easier remember global , not. minor offense though , others may disagree.

anyways, bit off topic question, more close you've put little effort solving yourself, comments may future coding projects , @ least make easier others going forward.


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -