Directions: Fill in answers on the pages below. Don't spend too much time on any one problem. Even if you have forgotten some things, fake it as well as you can.
Write a C function to_lower that will take a C string as input parameter, and will return a new malloced string that is a copy of the input parameter except that any uppercase letter has been converted to lowercase.
#include <stdio.h> /* prototype here */ void main(void) { char *sins[] = {"ENVY", "SLOTH", "GLUTTONY", "WRATH", "PRIDE", "LUST", "GREED", NULL}; char *laws[] = {"Trustworthy", "Loyal", "Helpful", "Friendly", "Courteous", "Kind", "Obedient", "Cheerful", "Thrifty", "Brave", "Clean", "Reverent", NULL}; /* 2 calls to the function here */ } /* code for the function here */ |
void f(int m, int n) { double r[m][n]; /* other stuff */ } |
For the above reasons, it is not straightforward to write a function in C that will take an arbitrary 2-dimensional array as input parameter. Instead, one must resort to trickery, as in the following examples:
#include <stdio.h> void readarray(double *r, int m, int n); void writearray(double *r, int m, int n); int main() { int m, n; /* m-by-n arrays */ double *r; scanf("%i %i", &m, &n); r = (double *)malloc(sizeof(double)*m*n); readarray(r, m, n); writearray(r, m, n); } void readarray(double *r, int m, int n) { int i, j; for (i = 0; i < m; i++) for (j = 0; j < n; j++) { scanf("%lf", &r[index(i, j, n)]); } } void writearray(double *r, int m, int n) { int i, j; for (i = 0; i < m; i++) { for (j = 0; j < n; j++) printf("%6.1f", r[index(i, j, n)]); printf("\n"); } } /* DEFINITION FOR FUNCTION "index" HERE */ | #include <stdio.h> /* DEFINITION FOR MACRO "R" HERE */ void readarray(double *r, int m, int n); void writearray(double *r, int m, int n); int main() { int m, n; /* m-by-n arrays */ double *r; scanf("%i %i", &m, &n); r = (double *)malloc(sizeof(double)*m*n); readarray(r, m, n); writearray(r, m, n); } void readarray(double *r, int m, int n) { int i, j; for (i = 0; i < m; i++) for (j = 0; j < n; j++) { scanf("%lf", &R(i, j, n)); } } void writearray(double *r, int m, int n) { int i, j; for (i = 0; i < m; i++) { for (j = 0; j < n; j++) printf("%6.1f", R(i, j, n)); printf("\n"); } } |
#include <stdio.h> #include <stdlib.h> #define N 5 struct gnode { int v; int dist; struct gnode *next; }; void adjlist(struct gnode *g[]); void main(void) { struct gnode *g[N]; int i; for (i = 0; i < N; i++) g[i] = NULL; adjlist(g); } void adjlist(struct gnode *g[]) { /* POSSIBLE ADDITIONAL DECLARATION(S) HERE */ FILE *edges; int i; int v1, v2, d; for (i = 0; i < N; i++) g[i] = NULL; if ((edges = fopen("edges.dat", "r")) == NULL) { printf("Can't open file: edges.dat\n"); exit(1); } while (fscanf(edges, "%i %i %i", &v1, &v2, &d) != EOF) { /* PUT PART OF YOUR ANSWER HERE */ } } /* POSSIBLE ADDITIONAL FUNCTION(S) HERE */ |
0 1 10 0 3 5 1 2 1 1 3 2 2 4 4 3 1 3 3 2 9 3 4 2 4 0 7 4 2 6 |