Array Exercises (part 2) for CISC105, CISC181
- Write a function
void sortAscending(int array[], int size);
that will use only one or more for loop(s), and one or more calls to a function
putMaxAtEnd(int array[], int size);
to put an array in ascending order.
- Write a function
int readTilEOF(FILE *infile, int array[], int size);
that will read integers from infile into the array until EOF is reached, or until size elements have been read. Return the number of elements read. Be sure your function works on an empty file (i.e. one where the initial call to fscanf returns EOF). Assume the variable infile was already opened successfully for reading.
- Write a function
void readFromKeyboard(int array[], int count);
that will read exactly count integers from the keyboard (i.e. from standard input). Don't include any prompts in your function; assume that the prompts have already been done in the main program.
-
Write a function
void sortDescending(int nums[], int n);
that uses only a for loop and calls to
void putMinAtEnd(int array[], int size);
to sort the array nums in order from largest to smallest.
- Assume that the function
void putMinAtStartOfRange(int nums[], int s, int e);
takes the minimum element from array elements in positions s through e and places it in position nums[s]. (For example, if s = 1 and e = 3, find the minimum element among nums[1], nums[2], and nums[3], and swap that element with the one in position nums[1]). Your job is to write a function
void sortAscending (int a[], int size);
using only a for loop and a call to putMinAtStartOfRange.
- Assume that the function
void putMaxAtStartOfRange(int nums[], int first, int last);
takes the maximum element from array elements in positions first through last and places it in position nums[first]. (For example, if first = 3 and last = 5, find the maximum element among nums[3], nums[4], and nums[5], and swap that element with the one in position nums[3]). Your job is to write a function
void sortDescending (int count, int array[]);
using only a for loop and a call to putMaxAtStartOfRange, and sorts the numbers in array in descending order (from largest to smallest.)
- Write a function
int countElemsBiggerThanX(int x, int array[], int numElems);
to count how many elements in an array of integers are larger than the value x. Pass in the array, the number of elements in the array, and the value x. Return the count.
- Solve exercises 1 through 7 for arrays of double instead of int. Note that the parameters which specify an array size or index (e.g. count, size, start, end) should all be int parameters, however the array itself should be of type
double [] or double *. (In exercise 7, should x be int or double? Should the return value be int or double?)
Phillip T Conrad
Last modified: Mon Nov 22 09:43:31 EST 2004