int main() { int numberArray[LEN]; int i; // counter for my for loops printf("Enter %d integers:\n", LEN); for(i = 0; i < LEN; i++) { // Reads in LEN integers. scanf(" %d", numberArray+i); } // Calling switcheroo various times. switcheroo(numberArray, numberArray + 7); // indexes 0 and 7 switcheroo(numberArray + 8, numberArray + 3); // indexes 8 and 3 switcheroo(numberArray + 14, numberArray); // indexes 14 and 0 printf("\nSwapped array:\n"); for(i = 0; i < LEN; i++) { // Prints out array again. printf("%d ", *(numberArray+i)); } printf("\n"); // For formatting's sake. return 0; }
int main(int argc, char *argv[]) { unsigned int i, j; unsigned int nCases, nCircles; char line[LINE_LENGTH]; Circles circles, bestCircles; double width; argc = argc; argv = argv; do fgets(line, LINE_LENGTH, stdin); while (1 != sscanf(line, "%u", &nCases)); for (i = 0; nCases > i; i++) { for (j = 0; CIRCLES_MAX > j; j++) { circles[j].x = UNDEFINED; circles[j].r = UNDEFINED; bestCircles[j].x = UNDEFINED; bestCircles[j].r = UNDEFINED; } do fgets(line, LINE_LENGTH, stdin); while (1 != sscanf(line, "%u", &nCircles)); sscanf(&(line[2]), "%lf %lf %lf %lf %lf %lf %lf %lf", &circles[0].r, &circles[1].r, &circles[2].r, &circles[3].r, &circles[4].r, &circles[5].r, &circles[6].r, &circles[7].r); width = UNDEFINED; for (j = 0; nCircles > j; j++) { switcheroo(&circles[nCircles - 1].x, &circles[j].x); switcheroo(&circles[nCircles - 1].r, &circles[j].r); solveCircles(circles, nCircles, 0.0l, &width, &bestCircles); switcheroo(&circles[nCircles - 1].x, &circles[j].x); switcheroo(&circles[nCircles - 1].r, &circles[j].r); } #ifdef PLOT printf("plot "); for (j = 0; nCircles > j; j++) { if (0 < j) printf(", "); printf("%f + %f * sin(t), %f + %f * cos(t)\\\n", bestCircles[j].x, bestCircles[j].r, bestCircles[j].r, bestCircles[j].r); } #endif printf("%.3f\n", width); } return 0; }
void solveCircles(Circles circles, unsigned int remaining, double result, double *bestResult, Circles *bestCircles) { unsigned int i; double minX, width; remaining--; minX = circles[remaining].r; for (i = remaining + 1; CIRCLES_MAX > i; i++) { if (UNDEFINED == circles[i].r) continue; if (minX < (width = getX(circles[i], circles[remaining]))) minX = width; } circles[remaining].x = minX; if (result < (width = circles[remaining].x + circles[remaining].r)) result = width; if (0 == remaining) { if (UNDEFINED == *bestResult || *bestResult > result) { *bestResult = result; #ifdef PLOT for (i = 0; CIRCLES_MAX > i; i++) { (*bestCircles)[i].x = circles[i].x; (*bestCircles)[i].r = circles[i].r; } #endif } return; } if (UNDEFINED != *bestResult && *bestResult < result) return; for (i = 0; remaining > i; i++) { switcheroo(&circles[remaining - 1].x, &circles[i].x); switcheroo(&circles[remaining - 1].r, &circles[i].r); solveCircles(circles, remaining, result, bestResult, bestCircles); switcheroo(&circles[remaining - 1].x, &circles[i].x); switcheroo(&circles[remaining - 1].r, &circles[i].r); } }
void remap_data(void *segment_start, void *segment_end, int w) { void *addr; unsigned long size; int data, prot; if(w) prot = PROT_WRITE; else prot = 0; prot |= PROT_READ | PROT_EXEC; size = (unsigned long) segment_end - (unsigned long) segment_start; data = create_mem_file(size); addr = mmap(NULL, size, PROT_WRITE | PROT_READ, MAP_SHARED, data, 0); if(addr == MAP_FAILED){ perror("mapping new data segment"); exit(1); } memcpy(addr, segment_start, size); if(switcheroo(data, prot, addr, segment_start, size) < 0){ printf("switcheroo failed\n"); exit(1); } }
/* reverse Polish calculator */ main() { int type, i, pos; int nopprint = 0; double op2; char s[MAXOP]; char line[MAXLINE]; while (getline(line, MAXLINE) > 0) { pos = 0; while ((type = getop(s, line, pos++)) != EOF) { switch (type) { case NUMBER: push(atof(s)); break; case SKIP: break; case '+': push(pop() + pop()); break; case '*': push(pop() * pop()); break; case '-': op2 = pop(); push(pop() - op2); break; case '/': op2 = pop(); if (op2 != 0.0) push(pop() / op2); else printf("error: zero divisor\n"); break; case '%': op2 = pop(); if (op2 != 0.0) push(fmod(pop(), op2)); else printf("error: zero divisor\n"); break; case '?': nopprint = 1; show(); break; case '#': nopprint = 1; dup(); break; case '~': nopprint = 1; switcheroo(); break; case '!': nopprint = 1; clear(); break; case '\n': if (nopprint) nopprint = 0; else printf("\t%.8g\n", pop()); break; default: printf("error: unknown command %s\n", s); break; } } } return 0; }