CMSC 104, Fall 2012
Lab 5
Objectives:
To practice working with arrays, and passing references to arrays into functions.
Tasks:
Task #1:
- Create a new program called "lab5b.c".
- In main(), create an array named
numbers
consisting of 100 integers. - Again, in main(), initialize all the elements to '0', using a 'for' loop
- Using another 'for' loop, print out the values in the array.
Task #2:
Modify lab5b.c, to:- Move the array initialization code into a separate function
called
initArray()
, and have main() call initArray(), passing in the arraynumbers
and it's size as a parameters. (So, obviously, initArray() needs to be specified to have an array and int as formal parameters. - Move the array-printing loop into another function, called
printArray()
, again passing an array and a size
Task #3:
- Add to main() a declaration for another array of 10 ints called
counts
. - Enhance initArray() so that it fills the array passed in with an equal number of 0's, 1's, ... all the way to 9.
- Add a function called countValues() that takes an input array called "values", an int size, and an output array called "counts" (no size needed for this). This function should first initialize all of the elements in "counts" to 0, then build up a count of the numbers seen in "values".