Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Question 1 */
- #include <stdio.h>
- float arrAvg(float *, int);
- int main(void) {
- int sizeA = 5;
- float a[5] = {2.1,6.3,4.9,8.1,9};
- int sizeB = 8;
- float b[8] = {1.1,-2.2,5,4.2,3.6,8,14,-8};
- float avgA, avgB = 0;
- avgA = arrAvg(a, sizeA);
- avgB = arrAvg(b, sizeB);
- printf("The average of A is %f\n", avgA);
- printf("The average of B is %f\n", avgB);
- return 0;
- }
- float arrAvg(float *in, int size) {
- int c = 0;
- float sum = 0;
- if (in == NULL) {
- return 0; /* No array means average of 0 */
- }
- if (size == 0) {
- return 0; /* No values means average of 0 */
- }
- /* Take the sum and average them */
- for (c=0; c<size; c++) {
- sum = sum + in[c];
- }
- return sum/size;
- }
- /* Question 2 */
- #include <stdio.h>
- struct blah {
- int id;
- int val[4];
- char label[4];
- int *data;
- };
- int main(void) {
- printf("The size of an int is %d\n", sizeof(int));
- printf("The size of an int[4] array is %d\n", sizeof(int)*4);
- printf("The size of a char[4] array is %d\n", sizeof(char)*4);
- printf("The size of an int * is %d\n", sizeof(int *));
- printf("The total theoretical size of blah is %d\n", sizeof(int)*5+sizeof(char)*4+sizeof(int *));
- printf("The size of blah is %d\n", sizeof(struct blah));
- return 0;
- }
- /* Question 3 */
- #include <stdio.h>
- struct llnode {
- int val;
- struct llnode *next;
- };
- typedef struct llnode llnode;
- int main(void) {
- printf("llnode 100 is %d\n", sizeof(llnode) * 100);
- printf("int array of 100 is %d\n", sizeof(int) * 100);
- return 0;
- }
- /* Question 4 */
- #include <stdio.h>
- int f(int);
- int main(void) {
- printf("Result at 5 is %d\n", f(5));
- return 0;
- }
- int f(int x) {
- if (x==-1) {
- return 2;
- } else {
- return x*f(x-2);
- }
- }
- /* Question 5 */
- #include <stdio.h>
- #include <stdlib.h>
- int g(char **, int size);
- int f(void);
- int main(void) {
- int r = 0;
- r = f();
- if (r == -1) {
- printf("An error occured in the allocation of memory");
- return -1;
- }
- printf("Allocated Successfully!\n");
- return 0;
- }
- int f(void) {
- int r = 0;
- char *a = NULL;
- r = g(&a, 10);
- if (r == -1) {
- return -1;
- }
- return 0;
- }
- int g(char **x, int size) {
- if (x == NULL) { return -1; }
- *x = (char *)malloc(sizeof(char)*size);
- return 0;
- }
- /* Question 6 */
- #include <stdio.h>
- #include <stdlib.h>
- struct multivar {
- char vars[4];
- int varTerms[4];
- float **varCoefficients;
- int **varExponents;
- };
- int main(void) {
- struct multivar *x = NULL;
- int c,l = 0;
- x = (struct multivar *)malloc(sizeof(struct multivar));
- x->vars[0] = 'x';
- x->vars[1] = 'y';
- x->vars[2] = 'z';
- x->vars[3] = 't';
- for (c=0; c<4; c++) {
- x->varTerms[c] = 2;
- l = l + 2;
- }
- x->varCoefficients = (float **)malloc(sizeof(float)*4*l);
- x->varExponents = (int **)malloc(sizeof(int)*4*l);
- for (c=0; c<4; c++) {
- x->varCoefficients[c] = (float *)malloc(sizeof(float)*x->varTerms[c]);
- x->varExponents[c] = (int *)malloc(sizeof(int)*x->varTerms[c]);
- for (l=0; l<x->varTerms[c]; l++) {
- x->varCoefficients[c][l] = 3;
- x->varExponents[c][l] = l;
- }}
- return 0;
- }
- /* Question 7 */
- #include <stdio.h>
- int main(void) {
- int *b;
- int **a;
- int c = 10;
- int d = 20;
- int q = 30;
- b=&c;
- a=&b;
- q=**a;
- *b=0;
- d = c + 100;
- q = q + 100;
- printf("a --> %d\n", **a);
- printf("b --> %d\n", *b);
- printf("c --> %d\n", c);
- printf("d --> %d\n", d);
- printf("q --> %d\n", q);
- return 0 ;
- }
- /* Question 8_a */
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct {
- int *exponents;
- float *coefficients;
- int terms;
- } polynomial;
- int main(void) {
- polynomial *first = NULL;
- first = (polynomial *)malloc(sizeof(polynomial));
- first->terms = 2;
- first->exponents = (int *)malloc(sizeof(int)*(first->terms));
- first->coefficients = (float *)malloc(sizeof(float)*(first->terms));
- (first->coefficients)[0] = 1.4;
- (first->coefficients)[1] = 2;
- (first->exponents)[0] = 0;
- (first->exponents)[1] = 1;
- displayPoly(first);
- return 0;
- }
- int displayPoly(polynomial *x) {
- int i = 0;
- printf("y = ");
- for (i=0; i<x->terms; i++) {
- if (x->exponents[i] == 0) {
- printf("%f", x->coefficients[i]);
- } else {
- printf("%fx^%d", x->coefficients[i], x->exponents[i]);
- }
- if (i+1 == x->terms) {
- break;
- }
- printf(" + ");
- }
- printf("\n");
- return 0;
- }
- /* Question 8_b */
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct {
- int *exponents;
- float *coefficients;
- int terms;
- } polynomial;
- int displayPoly(polynomial *);
- int integrate(polynomial **);
- int main(void) {
- int r = 0;
- polynomial *first = NULL;
- first = (polynomial *)malloc(sizeof(polynomial));
- first->terms = 2;
- first->exponents = (int *)malloc(sizeof(int)*(first->terms));
- first->coefficients = (float *)malloc(sizeof(float)*(first->terms));
- (first->coefficients)[0] = 1.4;
- (first->coefficients)[1] = 2;
- (first->exponents)[0] = 0;
- (first->exponents)[1] = 1;
- r = displayPoly(first);
- printf("Integrating...\n");
- r = integrate(&first);
- if (r == -1) {
- printf("An error occured!");
- return -1;
- }
- r = displayPoly(first);
- return 0;
- }
- int displayPoly(polynomial *x) {
- int i = 0;
- printf("y = ");
- for (i=0; i<x->terms; i++) {
- if (x->exponents[i] == 0) {
- printf("%f", x->coefficients[i]);
- } else if (x->coefficients[i] == 0) {
- continue;
- } else if (x->coefficients[i] == 1) {
- printf("x^%d", x->exponents[i]);
- } else if (x->exponents[i] == 1) {
- printf("%fx", x->coefficients[i]);
- } else {
- printf("%fx^%d", x->coefficients[i], x->exponents[i]);
- }
- if (i+1 == x->terms) {
- break;
- }
- printf(" + ");
- }
- printf("\n");
- return 0;
- }
- int integrate(polynomial **x) {
- int i = 0;
- for (i=0; i<(*x)->terms; i++) {
- (*x)->exponents[i] = (*x)->exponents[i] + 1;
- (*x)->coefficients[i] = (*x)->coefficients[i]*(1/(float)(*x)->exponents[i]);
- }
- return 0;
- }
- /* Question 9 (Should be 4 files) */
- #include <stdio.h>
- #include <stdlib.h>
- int fil(float *, int, int, float **);
- int main(void) {
- float *min;
- float *mout = NULL;
- int rows = 5;
- int cols = 3;
- int i = 0;
- int j = 0;
- int minusfactor = -1;
- int tog = 0;
- min = (float *)malloc(sizeof(float)*rows*cols);
- for (i = 0; i < rows; i++) {
- for (j = 0; j < cols; j++) {
- if (tog == 1) {
- min[i*rows + j] = i*2*minusfactor;
- tog = 0;
- }
- else if (tog == 0) {
- min[i*rows + j] = i*2;
- tog = 1;
- }
- }
- }
- fil(min,rows,cols,&mout);
- printf("matrixIn\n");
- for (i = 0; i < rows; i++) {
- for (j = 0; j < cols; j++) {
- printf("%f ",(min[i*rows + j]));
- }
- printf("\n");
- }
- printf("matrixOut\n");
- for (i = 0; i < rows; i++) {
- for (j = 0; j < cols; j++) {
- printf("%f ",(mout[i*rows + j]));
- }
- printf("\n");
- }
- return 0;
- }
- int fil(float *matrixIn, int rows, int cols, float **matrixOut) {
- int i = 0;
- int j = 0;
- if ((matrixIn == NULL) || (matrixOut == NULL)) {
- return -1;
- }
- if (*matrixOut == NULL) {
- *matrixOut = (float *)malloc(sizeof(float)*rows*cols);
- }
- for (i = 0; i < rows; i++) {
- for (j = 0; j < cols; j++) {
- if (matrixIn[i*rows + j] < 0) {
- (*matrixOut)[i*rows + j] = 0;
- }
- else {
- (*matrixOut)[i*rows + j] = matrixIn[i*rows + j];
- }
- }
- }
- return 0;
- }
- /* Question 10 */
- #include <stdlib.h>
- #include <stdio.h>
- int nsum(float *, float *, int, float **);
- int main(void) {
- float *a = NULL;
- float *b = NULL;
- float *outsum = NULL;
- int i = 0;
- int j = 0;
- int n = 10;
- a = (float *)malloc(sizeof(float)*n);
- b = (float *)malloc(sizeof(float)*n);
- outsum = (float *)malloc(sizeof(float)*n);
- for (i = 0; i < n; i++) {
- a[i] = i;
- b[i] = i*2;
- }
- nsum(a,b,n,&outsum);
- for (j = 0; j < n; j++) {
- printf("%f\n",outsum[j]);
- }
- return 0;
- }
- int nsum(float *a, float *b, int n, float **out) {
- int i = 0;
- if (a == NULL || b == NULL || out == NULL) {
- return -1;
- }
- *out = (float *)malloc(sizeof(float)*n);
- for (i=0; i<n; i++) {
- (*out)[i] = a[i] + b[i];
- }
- return 0;
- }
- /* Question 11 */
- #include <stdio.h>
- int isPrime(int);
- int isPrimeProduct(int);
- int main(void) {
- printf("5 is prime --> %d\n", isPrime(5));
- printf("25 is prime product --> %d\n", isPrimeProduct(25));
- printf("4 is prime --> %d\n", isPrime(4));
- return 0;
- }
- int isPrime(int n) {
- int c = 0;
- for (c=n-1; c>1; c--) {
- if (n % c == 0) {
- return -1;
- }
- }
- return 0;
- }
- int isPrimeProduct(int n) {
- int c = 0;
- for (c=n-1; c>1; c--) {
- if (n % c == 0) {
- if (isPrime(c) == 0) {
- if (isPrime((int)n/c) == 0) {
- return 0;
- }
- }
- }
- }
- return -1;
- }
- /* Question 12 */
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct {
- int valid;
- int value;
- int frequency;
- } tally;
- int histogram(int *, tally **, int);
- int main(void) {
- int vals[12] = {1,4,1,6,34,12,6,7,8,2,34,34};
- int size = 12;
- int r,c = 0;
- tally **l = NULL;
- l = (tally **)malloc(sizeof(tally)*size);
- for (c=0; c<size; c++) {
- l[c] = (tally *)malloc(sizeof(tally));
- }
- r = histogram(vals, l, size);
- if (r == -1) {
- printf("Bad thing happened...");
- }
- for (c=0; c<size; c++) {
- if (l[c]->valid == 1) {
- printf("%d, %d\n", (l[c])->value, (l[c])->frequency);
- }
- }
- return 0;
- }
- int histogram(int *n, tally **m, int s) {
- int c,t = 0;
- if (n == NULL || m == NULL) {
- return -1;
- }
- for (c=0; c<s; c++) {
- for (t=0; t<s; t++) {
- if ((m[t])->valid == 1) {
- if ((m[t])->value == n[c]) {
- (m[t])->frequency = (m[t])->frequency + 1;
- break;
- }
- } else {
- (m[t])->valid = 1;
- (m[t])->value = n[c];
- (m[t])->frequency = 1;
- break;
- }
- }
- }
- return 0;
- }
- /* Question 13 */
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct {
- int valid;
- int value;
- int frequency;
- } tally;
- int getmode(tally **, int *);
- int histogram(int *, tally **, int);
- int main(void) {
- int vals[12] = {1,4,1,6,34,12,6,7,8,2,34,34};
- int size = 12;
- int modeOut = 12;
- int r,c = 0;
- tally **l = NULL;
- l = (tally **)malloc(sizeof(tally)*size);
- for (c=0; c<size; c++) {
- l[c] = (tally *)malloc(sizeof(tally));
- }
- r = histogram(vals, l, size);
- if (r == -1) {
- printf("Bad thing happened...");
- }
- for (c=0; c<size; c++) {
- if (l[c]->valid == 1) {
- printf("%d, %d\n", (l[c])->value, (l[c])->frequency);
- }
- }
- r = getmode(l, &modeOut);
- printf("Mode is --> %d\n", modeOut);
- return 0;
- }
- int histogram(int *n, tally **m, int s) {
- int c,t = 0;
- if (n == NULL || m == NULL) {
- return -1;
- }
- for (c=0; c<s; c++) {
- for (t=0; t<s; t++) {
- if ((m[t])->valid == 1) {
- if ((m[t])->value == n[c]) {
- (m[t])->frequency = (m[t])->frequency + 1;
- break;
- }
- } else {
- (m[t])->valid = 1;
- (m[t])->value = n[c];
- (m[t])->frequency = 1;
- break;
- }
- }
- }
- return 0;
- }
- int getmode(tally **m, int *mode) { /* Usage: Takes mode in as length */
- int c,length = 0;
- int ret[2] = {0,0};
- if (m == NULL || mode == NULL) {
- return -1;
- }
- length = *mode;
- for (c=0; c<length; c++) {
- if ((m[c])->valid != 1) {
- continue;
- }
- if ((m[c])->frequency > ret[0]) {
- ret[0] = (m[c])->frequency;
- ret[1] = (m[c])->value;
- }
- }
- *mode = ret[1];
- return 0;
- }
- /* Question 14 */
- #include <stdio.h>
- int fibo(int);
- int main(void) {
- return 0;
- }
- int fibo(int n) {
- if (n == 0 || n == 1 || n == 2) {
- return 1;
- } else {
- return fibo(n-1) + fibo(n-2) + fibo(n-3);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement