Advertisement
Thefaceofbo

Midterm Answers - C

Nov 30th, 2018
503
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.46 KB | None | 0 0
  1. /* Question 1 */
  2. #include <stdio.h>
  3.  
  4. float arrAvg(float *, int);
  5.  
  6. int main(void) {
  7.    int sizeA = 5;
  8.    float a[5] = {2.1,6.3,4.9,8.1,9};
  9.    int sizeB = 8;
  10.    float b[8] = {1.1,-2.2,5,4.2,3.6,8,14,-8};
  11.    float avgA, avgB = 0;
  12.    avgA = arrAvg(a, sizeA);
  13.    avgB = arrAvg(b, sizeB);
  14.    printf("The average of A is %f\n", avgA);
  15.    printf("The average of B is %f\n", avgB);
  16.    return 0;
  17. }
  18.  
  19. float arrAvg(float *in, int size) {
  20.    int c = 0;
  21.    float sum = 0;
  22.    if (in == NULL) {
  23.       return 0; /* No array means average of 0 */
  24.    }
  25.    if (size == 0) {
  26.       return 0; /* No values means average of 0 */
  27.    }
  28.    /* Take the sum and average them */
  29.    for (c=0; c<size; c++) {
  30.       sum = sum + in[c];
  31.    }
  32.    return sum/size;
  33. }
  34.  
  35. /* Question 2 */
  36. #include <stdio.h>
  37.  
  38. struct blah {
  39.    int id;
  40.    int val[4];
  41.    char label[4];
  42.    int *data;
  43. };
  44.  
  45. int main(void) {
  46.    printf("The size of an int is %d\n", sizeof(int));
  47.    printf("The size of an int[4] array is %d\n", sizeof(int)*4);
  48.    printf("The size of a char[4] array is %d\n", sizeof(char)*4);
  49.    printf("The size of an int * is %d\n", sizeof(int *));
  50.    printf("The total theoretical size of blah is %d\n", sizeof(int)*5+sizeof(char)*4+sizeof(int *));
  51.    printf("The size of blah is %d\n", sizeof(struct blah));
  52.    return 0;
  53. }
  54.  
  55. /* Question 3 */
  56. #include <stdio.h>
  57.  
  58. struct llnode {
  59.    int val;
  60.    struct llnode *next;
  61. };
  62. typedef struct llnode llnode;
  63.  
  64. int main(void) {
  65.    printf("llnode 100 is %d\n", sizeof(llnode) * 100);
  66.    printf("int array of 100 is %d\n", sizeof(int) * 100);
  67.    return 0;
  68. }
  69.  
  70. /* Question 4 */
  71. #include <stdio.h>
  72.  
  73. int f(int);
  74.  
  75. int main(void) {
  76.    printf("Result at 5 is %d\n", f(5));
  77.    return 0;
  78. }
  79.  
  80. int f(int x) {
  81.    if (x==-1) {
  82.       return 2;
  83.    } else {
  84.       return x*f(x-2);
  85.    }
  86. }
  87.  
  88. /* Question 5 */
  89. #include <stdio.h>
  90. #include <stdlib.h>
  91.  
  92. int g(char **, int size);
  93. int f(void);
  94.  
  95. int main(void) {
  96.    int r = 0;
  97.    r = f();
  98.    if (r == -1) {
  99.       printf("An error occured in the allocation of memory");
  100.       return -1;
  101.    }
  102.    printf("Allocated Successfully!\n");
  103.    return 0;
  104. }
  105.  
  106. int f(void) {
  107.    int r = 0;
  108.    char *a = NULL;
  109.    r = g(&a, 10);
  110.    if (r == -1) {
  111.       return -1;
  112.    }
  113.    return 0;
  114. }
  115.  
  116. int g(char **x, int size) {
  117.    if (x == NULL) { return -1; }
  118.    *x = (char *)malloc(sizeof(char)*size);
  119.    return 0;
  120. }
  121.  
  122. /* Question 6 */
  123. #include <stdio.h>
  124. #include <stdlib.h>
  125.  
  126. struct multivar {
  127.    char vars[4];
  128.    int varTerms[4];
  129.    float **varCoefficients;
  130.    int **varExponents;
  131. };
  132.  
  133. int main(void) {
  134.    struct multivar *x = NULL;
  135.    int c,l = 0;
  136.    x = (struct multivar *)malloc(sizeof(struct multivar));
  137.    x->vars[0] = 'x';
  138.    x->vars[1] = 'y';
  139.    x->vars[2] = 'z';
  140.    x->vars[3] = 't';
  141.    for (c=0; c<4; c++) {
  142.       x->varTerms[c] = 2;
  143.       l = l + 2;
  144.    }
  145.    x->varCoefficients = (float **)malloc(sizeof(float)*4*l);
  146.    x->varExponents = (int **)malloc(sizeof(int)*4*l);
  147.    for (c=0; c<4; c++) {
  148.       x->varCoefficients[c] = (float *)malloc(sizeof(float)*x->varTerms[c]);
  149.       x->varExponents[c] = (int *)malloc(sizeof(int)*x->varTerms[c]);
  150.       for (l=0; l<x->varTerms[c]; l++) {
  151.          x->varCoefficients[c][l] = 3;
  152.          x->varExponents[c][l] = l;
  153.    }}
  154.    return 0;
  155. }
  156.  
  157. /* Question 7 */
  158. #include <stdio.h>
  159.  
  160.  
  161. int main(void) {
  162.    int *b;
  163.    int **a;
  164.    int c = 10;
  165.    int d = 20;
  166.    int q = 30;
  167.    b=&c;
  168.    a=&b;
  169.    q=**a;
  170.    *b=0;
  171.    d = c + 100;
  172.    q = q + 100;
  173.    printf("a --> %d\n", **a);
  174.    printf("b --> %d\n", *b);
  175.    printf("c --> %d\n", c);
  176.    printf("d --> %d\n", d);
  177.    printf("q --> %d\n", q);
  178.    return 0 ;
  179. }
  180.  
  181. /* Question 8_a */
  182. #include <stdio.h>
  183. #include <stdlib.h>
  184.  
  185. typedef struct {
  186.    int *exponents;
  187.    float *coefficients;
  188.    int terms;
  189. } polynomial;
  190.  
  191. int main(void) {
  192.    polynomial *first = NULL;
  193.    first = (polynomial *)malloc(sizeof(polynomial));
  194.    first->terms = 2;
  195.    first->exponents = (int *)malloc(sizeof(int)*(first->terms));
  196.    first->coefficients = (float *)malloc(sizeof(float)*(first->terms));
  197.    (first->coefficients)[0] = 1.4;
  198.    (first->coefficients)[1] = 2;
  199.    (first->exponents)[0] = 0;
  200.    (first->exponents)[1] = 1;
  201.    displayPoly(first);
  202.    return 0;
  203. }
  204.  
  205. int displayPoly(polynomial *x) {
  206.    int i = 0;
  207.    printf("y = ");
  208.    for (i=0; i<x->terms; i++) {
  209.       if (x->exponents[i] == 0) {
  210.          printf("%f", x->coefficients[i]);
  211.       } else {
  212.          printf("%fx^%d", x->coefficients[i], x->exponents[i]);
  213.       }
  214.       if (i+1 == x->terms) {
  215.          break;
  216.       }
  217.       printf(" + ");
  218.    }
  219.    printf("\n");
  220.    return 0;
  221. }
  222.  
  223. /* Question 8_b */
  224. #include <stdio.h>
  225. #include <stdlib.h>
  226.  
  227. typedef struct {
  228.    int *exponents;
  229.    float *coefficients;
  230.    int terms;
  231. } polynomial;
  232.  
  233. int displayPoly(polynomial *);
  234. int integrate(polynomial **);
  235.  
  236. int main(void) {
  237.    int r = 0;
  238.    polynomial *first = NULL;
  239.    first = (polynomial *)malloc(sizeof(polynomial));
  240.    first->terms = 2;
  241.    first->exponents = (int *)malloc(sizeof(int)*(first->terms));
  242.    first->coefficients = (float *)malloc(sizeof(float)*(first->terms));
  243.    (first->coefficients)[0] = 1.4;
  244.    (first->coefficients)[1] = 2;
  245.    (first->exponents)[0] = 0;
  246.    (first->exponents)[1] = 1;
  247.    r = displayPoly(first);
  248.    printf("Integrating...\n");
  249.    r = integrate(&first);
  250.    if (r == -1) {
  251.       printf("An error occured!");
  252.       return -1;
  253.    }
  254.    r = displayPoly(first);
  255.    return 0;
  256. }
  257.  
  258. int displayPoly(polynomial *x) {
  259.    int i = 0;
  260.    printf("y = ");
  261.    for (i=0; i<x->terms; i++) {
  262.       if (x->exponents[i] == 0) {
  263.          printf("%f", x->coefficients[i]);
  264.       } else if (x->coefficients[i] == 0) {
  265.          continue;
  266.       } else if (x->coefficients[i] == 1) {
  267.          printf("x^%d", x->exponents[i]);
  268.       } else if (x->exponents[i] == 1) {
  269.          printf("%fx", x->coefficients[i]);
  270.       } else {
  271.          printf("%fx^%d", x->coefficients[i], x->exponents[i]);
  272.       }
  273.       if (i+1 == x->terms) {
  274.          break;
  275.       }
  276.       printf(" + ");
  277.    }
  278.    printf("\n");
  279.    return 0;
  280. }
  281.  
  282. int integrate(polynomial **x) {
  283.    int i = 0;
  284.    for (i=0; i<(*x)->terms; i++) {
  285.       (*x)->exponents[i] = (*x)->exponents[i] + 1;
  286.       (*x)->coefficients[i] = (*x)->coefficients[i]*(1/(float)(*x)->exponents[i]);
  287.    }
  288.    return 0;
  289. }
  290.  
  291. /* Question 9 (Should be 4 files) */
  292. #include <stdio.h>
  293. #include <stdlib.h>
  294.  
  295. int fil(float *, int, int, float **);
  296.  
  297. int main(void) {
  298.    float *min;
  299.    float *mout = NULL;
  300.    int rows = 5;
  301.    int cols = 3;
  302.    int i = 0;
  303.    int j = 0;
  304.    int minusfactor = -1;
  305.    int tog = 0;
  306.    min = (float *)malloc(sizeof(float)*rows*cols);
  307.    for (i = 0; i < rows; i++) {
  308.       for (j = 0; j < cols; j++) {
  309.          if (tog == 1) {
  310.             min[i*rows + j] = i*2*minusfactor;
  311.             tog = 0;
  312.          }
  313.          else if (tog == 0) {
  314.             min[i*rows + j] = i*2;
  315.             tog = 1;
  316.          }
  317.       }
  318.    }
  319.    fil(min,rows,cols,&mout);
  320.    printf("matrixIn\n");
  321.    for (i = 0; i < rows; i++) {
  322.       for (j = 0; j < cols; j++) {
  323.          printf("%f ",(min[i*rows + j]));
  324.       }
  325.       printf("\n");
  326.    }
  327.    printf("matrixOut\n");
  328.    for (i = 0; i < rows; i++) {
  329.       for (j = 0; j < cols; j++) {
  330.          printf("%f ",(mout[i*rows + j]));
  331.       }
  332.       printf("\n");
  333.    }
  334.    return 0;
  335. }
  336.  
  337. int fil(float *matrixIn, int rows, int cols, float **matrixOut) {
  338.    int i = 0;
  339.    int j = 0;
  340.    if ((matrixIn == NULL) || (matrixOut == NULL)) {
  341.       return -1;
  342.    }
  343.    if (*matrixOut == NULL) {
  344.       *matrixOut = (float *)malloc(sizeof(float)*rows*cols);
  345.    }
  346.    for (i = 0; i < rows; i++) {
  347.       for (j = 0; j < cols; j++) {
  348.          if (matrixIn[i*rows + j] < 0) {
  349.             (*matrixOut)[i*rows + j] = 0;
  350.          }
  351.          else {
  352.             (*matrixOut)[i*rows + j] = matrixIn[i*rows + j];
  353.          }
  354.       }
  355.    }
  356.    return 0;
  357. }
  358.  
  359. /* Question 10 */
  360. #include <stdlib.h>
  361. #include <stdio.h>
  362.  
  363. int nsum(float *, float *, int, float **);
  364.  
  365. int main(void) {
  366.    float *a = NULL;
  367.    float *b = NULL;
  368.    float *outsum = NULL;
  369.    int i = 0;
  370.    int j = 0;
  371.    int n = 10;
  372.    a = (float *)malloc(sizeof(float)*n);
  373.    b = (float *)malloc(sizeof(float)*n);
  374.    outsum = (float *)malloc(sizeof(float)*n);
  375.    for (i = 0; i < n; i++) {
  376.       a[i] = i;
  377.       b[i] = i*2;
  378.    }
  379.    nsum(a,b,n,&outsum);
  380.    for (j = 0; j < n; j++) {
  381.       printf("%f\n",outsum[j]);
  382.    }
  383.    return 0;
  384. }
  385.  
  386. int nsum(float *a, float *b, int n, float **out) {
  387.    int i = 0;
  388.    if (a == NULL || b == NULL || out == NULL) {
  389.       return -1;
  390.    }
  391.    *out = (float *)malloc(sizeof(float)*n);
  392.    for (i=0; i<n; i++) {
  393.       (*out)[i] = a[i] + b[i];
  394.    }
  395.    return 0;
  396. }
  397.  
  398. /* Question 11 */
  399. #include <stdio.h>
  400.  
  401. int isPrime(int);
  402. int isPrimeProduct(int);
  403.  
  404. int main(void) {
  405.    printf("5 is prime --> %d\n", isPrime(5));
  406.    printf("25 is prime product --> %d\n", isPrimeProduct(25));
  407.    printf("4 is prime --> %d\n", isPrime(4));
  408.    return 0;
  409. }
  410.  
  411. int isPrime(int n) {
  412.   int c = 0;
  413.   for (c=n-1; c>1; c--) {
  414.     if (n % c == 0) {
  415.       return -1;
  416.     }
  417.   }
  418.   return 0;
  419. }
  420.  
  421. int isPrimeProduct(int n) {
  422.   int c = 0;
  423.   for (c=n-1; c>1; c--) {
  424.     if (n % c == 0) {
  425.       if (isPrime(c) == 0) {
  426.         if (isPrime((int)n/c) == 0) {
  427.           return 0;
  428.         }
  429.       }
  430.     }
  431.   }
  432.   return -1;
  433. }
  434.  
  435. /* Question 12 */
  436. #include <stdio.h>
  437. #include <stdlib.h>
  438.  
  439. typedef struct {
  440.   int valid;
  441.   int value;
  442.   int frequency;
  443. } tally;
  444.  
  445. int histogram(int *, tally **, int);
  446.  
  447. int main(void) {
  448.    int vals[12] = {1,4,1,6,34,12,6,7,8,2,34,34};
  449.    int size = 12;
  450.    int r,c = 0;
  451.    tally **l = NULL;
  452.    l = (tally **)malloc(sizeof(tally)*size);
  453.    for (c=0; c<size; c++) {
  454.       l[c] = (tally *)malloc(sizeof(tally));
  455.    }
  456.    r = histogram(vals, l, size);
  457.    if (r == -1) {
  458.       printf("Bad thing happened...");
  459.    }
  460.    for (c=0; c<size; c++) {
  461.       if (l[c]->valid == 1) {
  462.          printf("%d, %d\n", (l[c])->value, (l[c])->frequency);
  463.       }
  464.    }
  465.    return 0;
  466. }
  467.  
  468. int histogram(int *n, tally **m, int s) {
  469.    int c,t = 0;
  470.    if (n == NULL || m == NULL) {
  471.       return -1;
  472.    }
  473.    for (c=0; c<s; c++) {
  474.       for (t=0; t<s; t++) {
  475.          if ((m[t])->valid == 1) {
  476.             if ((m[t])->value == n[c]) {
  477.                (m[t])->frequency = (m[t])->frequency + 1;
  478.                break;
  479.             }
  480.          } else {
  481.             (m[t])->valid = 1;
  482.             (m[t])->value = n[c];
  483.             (m[t])->frequency = 1;
  484.             break;
  485.          }
  486.       }
  487.    }
  488.    return 0;
  489. }
  490.  
  491. /* Question 13 */
  492. #include <stdio.h>
  493. #include <stdlib.h>
  494.  
  495. typedef struct {
  496.   int valid;
  497.   int value;
  498.   int frequency;
  499. } tally;
  500.  
  501. int getmode(tally **, int *);
  502. int histogram(int *, tally **, int);
  503.  
  504. int main(void) {
  505.    int vals[12] = {1,4,1,6,34,12,6,7,8,2,34,34};
  506.    int size = 12;
  507.    int modeOut = 12;
  508.    int r,c = 0;
  509.    tally **l = NULL;
  510.    l = (tally **)malloc(sizeof(tally)*size);
  511.    for (c=0; c<size; c++) {
  512.       l[c] = (tally *)malloc(sizeof(tally));
  513.    }
  514.    r = histogram(vals, l, size);
  515.    if (r == -1) {
  516.       printf("Bad thing happened...");
  517.    }
  518.    for (c=0; c<size; c++) {
  519.       if (l[c]->valid == 1) {
  520.          printf("%d, %d\n", (l[c])->value, (l[c])->frequency);
  521.       }
  522.    }
  523.    r = getmode(l, &modeOut);
  524.    printf("Mode is --> %d\n", modeOut);
  525.    return 0;
  526. }
  527.  
  528. int histogram(int *n, tally **m, int s) {
  529.    int c,t = 0;
  530.    if (n == NULL || m == NULL) {
  531.       return -1;
  532.    }
  533.    for (c=0; c<s; c++) {
  534.       for (t=0; t<s; t++) {
  535.          if ((m[t])->valid == 1) {
  536.             if ((m[t])->value == n[c]) {
  537.                (m[t])->frequency = (m[t])->frequency + 1;
  538.                break;
  539.             }
  540.          } else {
  541.             (m[t])->valid = 1;
  542.             (m[t])->value = n[c];
  543.             (m[t])->frequency = 1;
  544.             break;
  545.          }
  546.       }
  547.    }
  548.    return 0;
  549. }
  550.  
  551. int getmode(tally **m, int *mode) { /* Usage: Takes mode in as length */
  552.    int c,length = 0;
  553.    int ret[2] = {0,0};
  554.    if (m == NULL || mode == NULL) {
  555.       return -1;
  556.    }
  557.    length = *mode;
  558.    for (c=0; c<length; c++) {
  559.       if ((m[c])->valid != 1) {
  560.          continue;
  561.       }
  562.       if ((m[c])->frequency > ret[0]) {
  563.          ret[0] = (m[c])->frequency;
  564.          ret[1] = (m[c])->value;
  565.       }
  566.    }
  567.    *mode = ret[1];
  568.    return 0;
  569. }
  570.  
  571. /* Question 14 */
  572. #include <stdio.h>
  573.  
  574. int fibo(int);
  575.  
  576. int main(void) {
  577.   return 0;
  578. }
  579.  
  580. int fibo(int n) {
  581.   if (n == 0 || n == 1 || n == 2) {
  582.     return 1;
  583.   } else {
  584.     return fibo(n-1) + fibo(n-2) + fibo(n-3);
  585.   }
  586.   return 0;
  587. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement