void calcPath(std::vector<std::vector<int>>& tmp, int h, int w) { if (w == (int)tmp.size()-1 && h == (int)tmp[0].size()-1) return; int sum1 = BIGNUM, sum2 = BIGNUM, sum3 = BIGNUM, sum4 = BIGNUM; if (h+1 != (int)tmp.size()) sum1 = calcSum(tmp, tmp[h][w], h+1, w); // down if (w+1 != (int)tmp[0].size()) sum2 = calcSum(tmp, tmp[h][w], h, w+1); // right if (h-1 != -1) sum3 = calcSum(tmp, tmp[h][w], h-1, w); // up if (w-1 != -1) sum4 = calcSum(tmp, tmp[h][w], h, w-1); // left if (sum1 == BIGNUM && sum2 == BIGNUM && sum3 == BIGNUM && sum4 == BIGNUM) return; if (h+1 != (int)tmp.size()) calcPath(tmp, h+1, w); if (w+1 != (int)tmp[0].size()) calcPath(tmp, h, w+1); if (h-1 != -1) calcPath(tmp, h-1, w); if (w-1 != -1) calcPath(tmp, h, w-1); }
// The main method int main(int argc, char** argv) { // Print a message along the sum and append a new line. printf("The sum of all multiples of 3 and 5 below 1000 are: %d\n", calcSum()); // Return the exit code for success. return 0; }
void printCounts(char *chr,int *posi,suint **counts,int nSites,size_t nInd,kstring_t &bpos,kstring_t &bbin,int dumpType,int *keepSites){ bpos.l=bbin.l=0; for(int s=0;s<nSites;s++){ if(keepSites[s]==0) continue; ksprintf(&bpos, "%s\t%d\t%d\n",chr,posi[s]+1,calcSum(counts[s],4*nInd)); //if we need per sample info if(dumpType>1) { if(dumpType==4)//count A,C,G,T for(int i=0;i<4*nInd;i++) ksprintf(&bbin,"%u\t",counts[s][i]); else if(dumpType==2){//print A+C+G+T for(int n=0;n<nInd;n++) ksprintf(&bbin,"%u\t",counts[s][n*4]+counts[s][n*4+1]+counts[s][n*4+2]+counts[s][n*4+3]); }else{//overall sum of A,C,G,T size_t tsum[4]={0,0,0,0}; for(int i=0;i<4*nInd;i++) tsum[i%4] +=counts[s][i]; ksprintf(&bbin,"%zu\t%zu\t%zu\t%zu",tsum[0],tsum[1],tsum[2],tsum[3]); } kputc('\n',&bbin); } } }
void doWorker() { printf("[%s.%d] worker started\n", processor_name, my_rank); // begin parallel code; fill in the missing code here MPI_Status status; int master_id = 0; int i = 0; int n = 0; int start = 0; MPI_Recv(&n, 1, MPI_INT, master_id, 0, MPI_COMM_WORLD, &status); int *array = (int*) malloc(sizeof(int) * n); MPI_Recv(array, n, MPI_INT, master_id, 0, MPI_COMM_WORLD, &status); int size = 6; int *newArray = (int*) malloc(sizeof(int) * n); newArray[0] = calcSum(array, n); newArray[1] = calcMean(array, n); newArray[2] = findMax(array, n); newArray[3] = findMin(array, n); MPI_Send(&n, 1, MPI_INT, worker_id, 0, MPI_COMM_WORLD); MPI_Send(&array[start], n, MPI_INT, worker_id, 0, MPI_COMM_WORLD); // end parallel code; no more code change required printf("[%s.%d] worker completed\n", processor_name, my_rank); }
void FutabaServo::shortPack(int type) { //set target ID switch (type) { case INIT: ID = 0xff; break; default: ID = target_ID; break; } //content has been set already //calculate the sum CMD.data[CMD.len - 1] = calcSum(&ID, CMD.len - 5); if(RPU==true) { mPort->writePort(rs303[type].data, rs303[type].len); } else { mPort->writePort(rs303[type].data+2, rs303[type].len-2); } Sleep(100); return; }
int main() { FILE *in,*out; int i,j,n,k,minp; long long ans=0; in=fopen("hotel.in","r"); fscanf(in,"%d%d%d\n",&n,&k,&minp); for (i=0;i<=n;i++) { int c,p; if (i<n) { fscanf(in,"%d %d\n",&c,&p); s[c].cnthotel++; }else c=p=-1; for (j=0;j<k;j++) { if (p<=minp) { if (s[j].cntoverminp>=2) ans-=calcSum(s[j].cntoverminp); s[j].cntoverminp=0; } if (c==j && p>minp) { s[j].cntoverminp++; } } } fclose(in); for (j=0;j<k;j++) ans+=calcSum(s[j].cnthotel); out=fopen("hotel.out","w+"); fprintf(out,"%I64d\n",ans); fclose(out); return 0; }
double Distance::DTW(const TimeSeries &ts1, const TimeSeries &ts2) { int len1 = ts1.length(); int len2 = ts2.length(); double *table_d = new double[len1 * len2]; double *table_g = new double[len1 * len2]; calcCost(ts1, ts2, table_d, table_g, len1, len2); calcGamma(table_d, table_g, len1, len2); double dist = calcSum(table_d, table_g, len1, len2); delete[] table_d; delete[] table_g; return dist; }
int main() { /* Feel free to modify this function to test different things */ /* For example, you can temporarily hide unimplemented functions * inside comments */ AnyData array[5]; array[0] = setDouble(3.14159); array[1] = setByte(2); array[2] = setByte(3); array[3] = setInt(10000); array[4] = setDouble(5.555); for (int i = 0; i < 5; i++) { printValue(array[i]); printf("\n"); } printValue(calcSum(&array[1], &array[4])); }
void doManager(int n, int z, int seed) { int N = n * n; int *matrix = createArray(N, z); printf("[%s.%d] manager started\n", processor_name, my_rank); // begin parallel code; fill in the missing code here // double mean = 0.0, sum = 0.0, std = 0.0, max = 0.0, min = 0.0; MPI_Status status; int start, worker_id, worker_count = world_size - 1; if (N % worker_count == 0) { int start = 0; MPI_Send(&N, 1, MPI_INT, worker_id, 0, MPI_COMM_WORLD); MPI_Send(&matrix[start], N, MPI_INT, worker_id, 0, MPI_COMM_WORLD); } else{ int start = 0; MPI_Send(&N, 1, MPI_INT, worker_id, 0, MPI_COMM_WORLD); MPI_Send(&matrix[start], N, MPI_INT, worker_id, 0, MPI_COMM_WORLD); } int count = world_size; // end parallel code; no more code change required printf("[%s.%d] manager completed\n", processor_name, my_rank); sleep(1); printArray(matrix, n); printf("N = %d\n", N); printf("sum = %g %g\n", sum, calcSum(matrix, N)); printf("mean = %g %g\n", mean, calcMean(matrix, N)); printf("std = %g %g\n", std, calcStd(matrix, N)); printf("max = %g %g\n", max, findMax(matrix, N)); printf("min = %g %g\n", min, findMin(matrix, N)); }
//recursive time limit exceeded void maxCoins_helper(vector<int>& nums, int sum, int &maxsum) { if (nums.size() == 1) { if (sum+ nums[0] > maxsum) { maxsum = sum+ nums[0]; } return; } for ( int i = 0; i < nums.size(); i++) { int temp1 = nums[i]; int temp2 = calcSum(nums, i); sum += temp2; nums.erase(nums.begin()+i); maxCoins_helper(nums, sum, maxsum); sum -= temp2; nums.insert(nums.begin()+i, temp1); } }
void FutabaServo::readPack(int type) { //request all servo to reply with certain information switch (type) { case READID: case READINFO: ID = 0xff; break; default: ID = target_ID; break; } CMD.data[CMD.len-2] = calcSum(&ID, CMD.len-6); if(RPU==true) { mPort->writePort(CMD.data, CMD.len); } else { mPort->writePort(CMD.data+2, CMD.len-3); } return; }
int main() { int result = calcSum(std::cin); printResult(std::cout, result); }
int main() { int cTotal = 0; int cState = STATE_READ; char toggleOut = FALSE; Signal motor1; Signal motor2; Signal estop; Signal rcMode; init_coridium(); setbaud(0, 17); statusMessage[0] = '#'; statusMessage[1] = '%'; statusMessage[2] = 0x82; INPUT(RC1); motor1.pin = RC1; INPUT(RC2); motor2.pin = RC2; INPUT(RC3); estop.pin = RC3; INPUT(RC4); rcMode.pin = RC4; OUTPUT(TX); OUTPUT(ESTOP); LOW(ESTOP); OUTPUT(RCMode); OUTPUT(FreqPin); OUTPUT(LIGHT); motor1.state = STATE_WAIT; motor1.duty = 0; motor1.valid = 0; motor2.state = STATE_WAIT; motor2.duty = 0; motor2.valid = 0; estop.state = STATE_WAIT; estop.duty = 0; estop.valid = 0; rcMode.state = STATE_WAIT; rcMode.duty = 0; rcMode.valid = 0; SLEEP(2); while (TRUE) { switch (cState) { case STATE_READ: if (toggleOut) { toggleOut = FALSE; HIGH(FreqPin); } else { toggleOut = TRUE; LOW(FreqPin); } cTotal += getDuty(&motor1); cTotal += getDuty(&motor2); cTotal += getDuty(&estop); cTotal += getDuty(&rcMode); if (cTotal > 8) { cState = STATE_SERIAL; cTotal = 0; } break; case STATE_SERIAL: if (estop.lastVal > 0) { sendESTOP(0); gESTOP = FALSE; if (rcMode.lastVal > 0) { HIGH(LIGHT); HIGH(RCMode); // 11 == Manual (teleop) Mode statusMessage[3] = 0x00; statusMessage[4] = 0xFF; statusMessage[5] = 0xFF; statusMessage[6] = 0xFF; calcSum(); //statusMessage[7] = 0x84; //SEROUT(TXD0,9600, 1, 3, statusMessage); TXD0(statusMessage[0]); TXD0(statusMessage[1]); TXD0(statusMessage[2]); TXD0(statusMessage[3]); TXD0(statusMessage[4]); TXD0(statusMessage[5]); TXD0(statusMessage[6]); TXD0(statusMessage[7]); } else { if (flashcount > 0) { HIGH(LIGHT); } else { LOW(LIGHT); } if (flashcount > LIGHTDUTY) { flashcount = -LIGHTDUTY; } LOW(RCMode); // 00 == Autonomous Mode statusMessage[3] = 0x00; statusMessage[4] = 0x00; statusMessage[5] = 0x00; statusMessage[6] = 0x00; calcSum(); //statusMessage[7] = 0x84; //SEROUT(TXD0,9600, 1, 3, statusMessage); TXD0(statusMessage[0]); TXD0(statusMessage[1]); TXD0(statusMessage[2]); TXD0(statusMessage[3]); TXD0(statusMessage[4]); TXD0(statusMessage[5]); TXD0(statusMessage[6]); TXD0(statusMessage[7]); } } else { HIGH(LIGHT); HIGH(RCMode); sendESTOP(1); gESTOP = TRUE; // 11 == Manual (teleop) Mode statusMessage[3] = 0x00; statusMessage[4] = 0xFF; statusMessage[5] = 0xFF; statusMessage[6] = 0xFF; calcSum(); //statusMessage[7] = 0x84; //SEROUT(TXD0,9600, 1, 3, statusMessage); TXD0(statusMessage[0]); TXD0(statusMessage[1]); TXD0(statusMessage[2]); TXD0(statusMessage[3]); TXD0(statusMessage[4]); TXD0(statusMessage[5]); TXD0(statusMessage[6]); TXD0(statusMessage[7]); } #ifdef DEBUG putchar( binToHexstr((char)((motor1.lastVal >> 4) & 0x0F)) ); putchar( binToHexstr((char)(motor1.lastVal & 0x0F)) ); putchar(' '); putchar( binToHexstr((char)((motor2.lastVal >> 4) & 0x0F)) ); putchar( binToHexstr((char)(motor2.lastVal & 0x0F)) ); putchar('\n'); //printf("%d\n", estop.lastVal); #endif // motor1 == forward value // motor2 == left/right value int left_motor = motor1.lastVal/2 - motor2.lastVal/2; int right_motor = motor1.lastVal/2 + motor2.lastVal/2; sendSerial(right_motor, 1); sendSerial(left_motor, 0); cState = STATE_READ; break; } flashcount++; } return 0; }
void abcCounts::run(funkyPars *pars){ if(doCounts==0) return; assert(pars->chk!=NULL&&pars->counts==NULL); pars->counts = countNucs(pars->chk,pars->keepSites,setMinDepthInd,setMaxDepthInd); // fprintf(stderr,"%d\n",pars->keepSites[0]); for(int s=0;s<pars->numSites;s++){// Why is this loop needed? is it to remove sites with no data above minQ filters? size_t ss=0; for(int i=0;i<4*pars->nInd;i++) if(pars->keepSites[s]) if(pars->counts[s][i]){ ss++; break; } if(ss==0) pars->keepSites[s]=0; } //modify keepsites; if(minInd!=0) { for(int i=0;i<pars->numSites;i++){ if(pars->keepSites[i]==0) continue; //THIS IS REALLY STUPID but lets count number of samples wiht info int nDep =0; for(int s=0;s<pars->nInd;s++){ int dep=0; for(int j=0;j<4;j++) dep += pars->counts[i][s*4+j]; if(dep) nDep++; } //nDep is now the number of sapmles wiht info if(nDep<minInd) pars->keepSites[i] = 0; else pars->keepSites[i] = nDep; } } if(setMaxDepth!=-1){ for(int s=0;s<pars->numSites;s++){ size_t totSum = calcSum(pars->counts[s],4*nInd); if(totSum>setMaxDepth) pars->keepSites[s]=0; } } //fprintf(stderr,"minDepth=%d nInd=%d pars->numsites=%d\n",minDepth,nInd,pars->numSites); if(setMinDepth!=-1){ for(int s=0;s<pars->numSites;s++){ if(pars->keepSites[s]==0) continue; size_t totSum = calcSum(pars->counts[s],4*pars->nInd); if(totSum<setMinDepth) pars->keepSites[s]=0; else{ int nDep =0; for(int i=0;i<pars->nInd;i++){ int dep=0; for(int j=0;j<4;j++) dep += pars->counts[s][i*4+j]; if(dep) nDep++; } pars->keepSites[s]= nDep; } } } if(setMaxObs!=0){ //only for first sample... for(int s=0;s<pars->numSites;s++){ if(pars->keepSites[s]==0) continue; int ndiff=0; for(int i=0;i<4;i++) if(pars->counts[s][i]) ndiff++; if(ndiff>setMaxObs) pars->keepSites[s]=0; } } }
double calcMean(int *array, int N) { return calcSum(array, N)/N; }