I. True/False (20 points). Answer True or False for each of the following.
True
True: one loop for each array index.
False. This was one freebie from the last test.
False. This was the other freebie.
True. We saw an example of this in class.
False
False.
False
False
True
Computer graphics and text terminals; the screen is represented by a 2D array. Matrix math. Keeping student records with one row per student, one column per grade. Games played on a 2D grid, like Chess and The Game of Life. There are other acceptable answers...
You don't always know how big to declare an array. If you overestimate, you waste memory. If you underestimate, your program is out of space and may crash. With malloc(), you can get just as little or as much as you need dynamically (i.e., at run-time). Also, a program compiled to use a huge array may not run on a computer with a small memory. With malloc(), you know at run-time that you're out of memory (malloc() returns NULL) and you can give the user options to lower the memory requirement, use a different algorithm, or just quit.
struct and typedef allow the programmer to mentally group concepts into abstract data types. The implementation of these types can be hidden from the person using the types; they will seem like just another C type with operations (functions) defined on them. This enhances usability and readability of the code.
Binary search can only be used when the data are sorted. Sorting takes more time than searching, so if you're only going to do a few searches in a program, linear search will be the best choice rather than sorting and then using binary search.
#include <stdio.h> typedef tag { int a; char b; float c } stuff; int main () { int i; stuff v[100]; FILE *f; f = fopen (argv[1], "r"); for (i=1; i<=100; i++) fread (&v[i], sizeof (stuff), 1, f); fclose (); for (i=1; i<=100; i++) printf ("%d\t%c\t%f\n", v[i]->c, v[i]->b, v[i]->a); }There are several errors in this program. Briefly describe four errors.
1. 'typedef' should be immediately followed by 'struct', since we are declaring a struct type. 2. 'float c' needs a semicolon after it. 3. 'argv' and 'argc' are not declared as parameters to main(), but they are used in the program. 4. The two 'for' loops should let 'i' take on values from 0..99, not 1..100, since the array 'v' starts at element 0. 5. 'fclose()' is missing its argument. 6. The order of the arguments to 'printf()' is jumbled. 7. 'v[i]->c' etc. should be 'v[i].c' since 'v[i]' is a struct, not a pointer to struct.
float average2d (float v[10][10]) {Note: this function prints nothing. It reads nothing from either the keyboard or a file. It just finds and returns the average of numbers that have already been placed into an array.
float average2d (float v[10][10]) { float sum; int i, j; sum = 0.0; for (i=0; i<10; i++) for (j=0; j<10; j++) sum += v[i][j]; return sum / 100.0; }
open the file called "numbers" for reading count the number of items in the file using the following algorithm: read integers from the file, incrementing a count each time, until end of file is reached. close the file allocate space for an array called 'v' using malloc and the number of items counted above. open the file again for reading read all the numbers into 'v' close the file print 'v' from last element to first element
int main () { FILE *f; int count, i, tmp, *v; /* open the file called "numbers" for reading */ f = fopen ("number", "r"); if (!f) { fprintf (stderr, "hey!\n"); exit (1); } /* count the number of items in the file */ for (count=0; !feof (f); count++) fscanf ("%d", &tmp); count--; /* close the file */ fclose (f); /* allocate space for an array called 'v' */ v = (int *) malloc (sizeof (int) * count); /* open the file again for reading */ f = fopen ("numbers", "r"); /* read all the numbers into 'v' */ for (i=0; i<count; i++) fscanf (f, "%d", &v[i]); /* close the file */ fclose (f); /* print 'v' from last element to first element */ for (i=count-1; i>=0; i--) printf ("%d\n", v[i]); exit (0); }