{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture9-feb9

# lecture9-feb9 - Announcements Lecture 9 Assignment 2 Q&A...

This preview shows pages 1–2. Sign up to view the full content.

1 Announcements Lecture 9 Assignment 2 – Q&A Topics for today – more arrays 1D Counters Example #define ARRAY_SIZE 100 . . . int counters [ARRAY_SIZE]; /* declare array of counters */ int number, i; const int SENTINEL = 9999; scanf (“ %d” , & number); while (number ! = SENTINEL ) { /* increment the counter for that number, read the next number*/ counters [number - 1] = counters [number -1] + 1; scanf (“ %d” , & number); } /* output table of how many times each value (1-100) was input */ for (i =1; i <= ARRAY_SIZE ; i++) printf ("the counter for number %d is %d\n”, i, counters [i - 1]); . . . How would we verify the correctness of the input? Partially Filled Array Arrays cannot grow after they have been allocated You can allocate more space (i.e. extra) than you believe your program will need If you guess too low, you will still run out of space and terminate You do not need to use all the elements in an array (but total computer memory is finite) You must keep track of the actual number of elements in a partially filled array Simple Array Algorithms Once you have an array with values in it there are a few common operations (and variants) that are often used in handling arrays Ordered Vs Unordered arrays - it depends int array1 [ ] = {22, 66, 31, 9, 77}; int array2 [ ] = {5, 10, 22, 32, 48, 66, 81, 92}; /* ordered */ Counting the number of values Finding a given value Removing a value Inserting a value Sorting an unordered array into an ordered array Improves search performance Examples use a partially filled array of a class’s hypothetical scores on the first exam each exam score is an integer from 0-100 /* program to find out how many students did not pass exam 1 */ #define CLASS_SIZE 142; /* allows for reuse next year */ . . .

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 4

lecture9-feb9 - Announcements Lecture 9 Assignment 2 Q&A...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online