Exercise 4 (by Mikael Eriksson, TA HT2015) About lab tomorrow: we are planning to have extra assistant to have time to give help and not just examine. Recap the strange(?) behaviour we saw in the trapezoid calculation of the integral in the last exercise. Actually what was strange was that x sometimes ended exactly on 1.0000. How is that possible?? Solution to the riddle? (Ah, we didn't see the last decimals). Better way to solve it to not get the accumulated rounding problem from many small delta-x:es? (Indeed we already know how many iterations there should be in the loop!) Exercise 4.1, with changed variable names. Draw picture to illustrate what is happening: #include main() { int *p1, *p2; int v1, v2; p1 = &v1; *p1 = 42; p2 = p1; printf("*p1 == %d, *p2 == %d\n", *p1, *p2); *p2 = 53; printf("*p1 == %d, *p2 == %d\n", *p1, *p2); p1 = &v2; *p1 = 88; printf("*p1 == %d, *p2 == %d\n", *p1, *p2); printf("I hope you get the point of this example\n"); } Comment about ”maxnorm” (from lecture slides, show code): Apparently necessary to explicitly send info on the size of the array to the function :O. A pointer in C has no meta-info, so no way of retreaving the size! (Unless you have a 0-terminated array, but that obviously doesn't work for numbers, as 0 would be interpreted as just another number in the array) Exercise 4.2 Comment about error handling in C (https://en.wikibooks.org/wiki/C_Programming/Error_handling): ”””C does not provide direct support for error handling (also known as exception handling). By convention, the programmer is expected to prevent errors from occurring in the first place, and test return values from functions. For example, -1 and NULL are used in several functions such as socket() (Unix socket programming) or malloc() respectively to indicate problems that the programmer should be aware about. “”” /* Ex 4.2 Demonstrates how to "return" values by reference parameters, and how to do typical error handling in C. /miker */ #include #include #define MAXDEGREE 10 typedef struct { int degree; double coeff[MAXDEGREE+1]; } polynomial; double roots(polynomial p, double* r1, double* r2) { /* IN: polynomial p, where the 3 first coefficients are assumed to be for x^2, x^1, x^0 terms; r1, r2 are pointers to the roots. OUT: d double, where d >= 0 if calculation succeeded and d < 0 implies a complex root which this function can not handle. DEPENDS: Nothing EFFECTS: The locations pointed to by r1, r2 will contain the real roots if d >= 0, otherwise no effect. */ double a = p.coeff[0]; double b = p.coeff[1]; double c = p.coeff[2]; double d = b*b - 4*a*c; if (d >= 0) { (*r1) = (-b + sqrt(d))/(2*a); (*r2) = (-b - sqrt(d))/(2*a); } return d; } main() { double a, b, res; // polynomial p = {2, {1, 2, 0}}; polynomial p = {2, {1, 1, -1}}; res = roots(p, &a, &b); if (res >= 0) { printf("The roots are: %f %f\n", a, b); } else { printf("There was a complex root, and I can't handle it :/\n"); } } Exercise 4.4 #include #include #define TRUE 1 #define FALSE 0 int sorted(int length, int int_array[]) { /* IN: length of array and int array OUT: true if array is sorted, otherwise false DEPENDS: Nothing EFFECTS: None */ int v = int_array[0]; int i; for (i = 0; i < length-1; i++) { if (int_array[i] > int_array[i+1]) { return FALSE; } } return TRUE; } main() { int array[5] = {1,1,6, 7, 9}; if (sorted(5, array)) { printf("Sorted\n"); } else { printf("Not sorted\n"); } } Exercise 4.5 #include const int MAX_STR_LEN = 400; void print_vertical(char str[]) { char x, y; int i = 0; for (x = str[i]; x != 0; i++, x = str[i]) { printf("%d: %c\n", i, x); // scanf("%c", &y); } } void cast_test(char c) { int A = 65; printf("%c = %d\n", c, (int) c); printf("%d = %c\n", A, (char) A); } void freq_table(char str[], int table[]) { // int table[256]; // Not good, why?? int i = 0; char x; for (x = str[i]; x != 0; i++, x = str[i]) { table[(int) x] += 1; } } void print_freq_table(int table[]) { int i; for (i = 32; i < 256; i++) { printf("%d %c: %d\n", i, (char) i, table[i]); } } main() { int table[256] = {0}; // we want zeros to start! /* int i; for (i = 0; i < 256; i++) { table[i] = 0; } */ char str[MAX_STR_LEN]; printf("Enter a line of text:\n"); fgets(str, sizeof(str), stdin); printf("You have entered:\n %s", str); freq_table(str, table); print_freq_table(table); } Exercise 4.8 /* Ex 4_8: Let's just take given code from lab 4 and see if we can simplify it. Remove unecessary code for the exercise. */ #include #include struct elementStorage { int dataitem; struct elementStorage * next; } ; typedef struct elementStorage element; int Count(element * adt, int value); // For the two below, just take definitions from lab 4 element * AddItem (element * adt, int data); void Print (element * adt); int main () { element *adt = NULL; adt = AddItem(adt, 1); adt = AddItem(adt, 2); adt = AddItem(adt, 2); adt = AddItem(adt, 3); adt = AddItem(adt, 2); Print(adt); printf("\n"); printf("There are %d nr of %d's\n", Count(adt, 2), 2); }/* main */ int Count(element * adt, int value) { int counter = 0; while (adt != NULL) { if (adt -> dataitem == value) { counter++; } adt = adt -> next; } return counter; }