Untitled3.cpp - #include #include...

This preview shows page 1 - 3 out of 7 pages.

#include <stdio.h> #include <stdlib.h> #include <string.h> #define nullCheck( ptr ); if( ptr == NULL ) { printf("\n\nFailure allocating memory! Check line %d! Terminating Program!\n", __LINE__); exit(EXIT_FAILURE); } #define fileCheck( ptr ); if( ptr == NULL ) { printf("\n\nFile not found! Check line %d! Terminating Program!\n", __LINE__); exit(EXIT_FAILURE); } //#define DEBUG // KnightsMultiply void KnightsMultiply(FILE *fin, FILE *fout); double KnightsMultiplyRecurse(int k, int n); // KnightsFlip void KnightsFlip(FILE *fin, FILE *fout); void KnightsFlipRecurse(char *string, int pos, int num, FILE *fout); // KnightsShape void KnightsShape(FILE *fin, FILE *fout); void KnightsShapeRecurse(int num, int max, FILE *fout); void KnightsShapePrint(int num, int max, FILE *fout); // KnightsScotch void KnightsScotch(FILE *fin, FILE *fout); int KnightsScotchRecurse(int *line, int size, int start); // KnightsDepot void KnightsDepot(FILE *fin, FILE *fout); void KnightsDepotRecurse(int *planks, int num2x4s, int maxLength, int numPlaced, int spaceLeftOn2x4, int num2x4sUsed); void exchange2x4s(int *planks, int i, int j); // GLOBAL # of 2x4s Needed int numUsed; int recursiveCall; int main() { int ii = 1; FILE *fin = fopen("KnightsRecurse.in", "r"); fileCheck(fin); FILE *fout = fopen("KnightsRecurse.out", "w"); fileCheck(fout); while(!feof(fin)) { char token[50]; fscanf(fin, "%s", token); if(!strcmp(token, "KnightsMultiply")) { KnightsMultiply(fin, fout); } else if(!strcmp(token, "KnightsScotch")) { //printf("KnightsScotch %d\n", ii); KnightsScotch(fin, fout); } else if(!strcmp(token, "KnightsDepot")) { //printf("Enter KnightsDepot\n"); KnightsDepot(fin, fout); } else if(!strcmp(token, "KnightsShape")) {
Image of page 1

Subscribe to view the full document.

KnightsShape(fin, fout); } else if(!strcmp(token, "KnightsFlip")) { KnightsFlip(fin, fout); } ii++; } fclose(fin); fclose(fout); //system("PAUSE"); exit(EXIT_SUCCESS); } // KnightsMultiply WRAPPER Funcion void KnightsMultiply(FILE *fin, FILE *fout) { int k, n; double answer; fscanf(fin, "%d %d", &k, &n); answer = KnightsMultiplyRecurse(k, n); fprintf(fout, "KnightsMultiply: %.0f\n\n", answer); } // KnightsMultiply RECURSIVE Funcion // This was meant to be a "gimmee". Why? // Because it is basically the SAME as the sumNumbers function shown on // Day 2 of the Recursion slides (page 24). // Instead of summing numbers, we're multiplying them. double KnightsMultiplyRecurse(int k, int n) { if ( n == k ) return k; else return (n * KnightsMultiplyRecurse(k, n-1)); } // KnightsFlip WRAPPER Funcion void KnightsFlip(FILE *fin, FILE *fout) { // Initialize the state. int num; fscanf(fin, "%d", &num); fprintf(fout, "KnightsFlip:\n"); // malloc string, of size "num+1", for the string of F's and K's.
Image of page 2
Image of page 3

{[ snackBarMessage ]}

Get FREE access by uploading your study materials

Upload your study materials now and get free access to over 25 million documents.

Upload now for FREE access Or pay now for instant access
Christopher Reinemann
"Before using Course Hero my grade was at 78%. By the end of the semester my grade was at 90%. I could not have done it without all the class material I found."
— Christopher R., University of Rhode Island '15, Course Hero Intern

Ask a question for free

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern