CS 010 - Introduction to Computer Science I
Lab 10. Arrays

Parts 1, 2, 3, and 4 should be completed in the lab period

Parts 5, 6 and 7 are challenge exercises (worth a small bonus!) to be attempted in whatever remaining time you have.

Grading
Attendance & participation: 2 points
Parts 1, 2, 3 & 4: 2 points each
Parts 5, 6 & 7: 0.25 points each

After the first lab period you should be
        Familiar with array declaration
        Able to use an array in a function call


Notes on Arrays

A one-dimensional array is a structured collection of components (often called array elements) that can be accessed individually by specifying the position of a component with a single index value.
You can think of an array as simply an improvement over using a collection of numbered variables to hold related items of data; e.g. you might have a list of three credit card expenses named
debit1, debit2 and debit3.
Using the structure of an array we would re-write this as:
debit[0], debit[1] and debit[2] (note that the indices go from 0 to 2, i.e. we start counting from 0).
The great advantage of this is that we can then use the index to access different expenses: we do this by referring to debit[i], and allowing i to take on the values 0, 1 and 2 as required.
Here is a "syntax template" of a one-dimensional array declaration:

            DataType ArrayName [ConstInt];

In this syntax template, DataType describes what is stored in each "slot" of the array. The "base type" of an array may be any type, but in this lab we limit our discussion to simple data types (e.g. integer, float, double).
ConstInt indicates the size of the array declared, i.e. it specifies the number of elements in the array. It must have a value greater than 0. If the value of the array size is n, the range of the index values is 0 to n-1 (this is because we always start counting at 0 rather than 1).
For example, the declaration

           int score[50];

Creates the array score which has 50 elements, each capable of holding one int value. In other words, the array score has a total of 50 elements, labelled score[0] through score[49], all of type int.


Part 1:

Write a program that declares an integer array with 10 components.
Now use a for (or while) loop to read in 10 integers from user input, and store them to this array.

Then use another loop to print out the 10 elements. For example:
User input: 3 22 45 32 55 90 3333 43 75 20
Your output: 3 22 45 32 55 90 3333 43 75 20


Part 2:

Use the code you wrote in Part 1, and extend it to swap the position of two numbers in the array. The user is asked to input the two indices (i.e. the positions) of the numbers to be swapped.
For example, using the values from the example in Part 1:
User input: 2 4
Your output: 3 22 55 32 45 90 3333 43 75 20

(Note that the number originally in position 2 - i.e. the third element - is now in position 4 - i.e. the fifth element - , and vice versa)


Part 3:

Now re-write the task of Part 2 as a separate function, using the following declaration.

// This function swaps two components of an array
void swap (int a[], int x, int y) ; // x and y are two indexes.

Note that when you pass the array as an argument to a function, it behaves exactly as though it had been passed by reference rather than by value - i.e. the entire original array is always directly affected by any changes made to it by the function.


Part 4:

Find the smallest element in an array. For example: Suppose the values stored in array are:
3 22 45 32 55 90 3333 43 75 20

Your output:
smallest element is 3


Part 5:

Find the next to smallest component in an array. For example: Suppose the values stored in array are:
3 22 45 32 55 90 3333 43 75 20

Your output:
Smallest element is 3
Second smallest element is 20


Part 6:

Combine the code you wrote in Parts 3, 4 and 5 to put the two smallest elements in position 0 and position 1.
For example: Suppose the values stored in array are:
3 22 45 32 55 90 3333 43 75 20

Your output:
3 20 45 32 55 90 3333 43 75 22


Part 7:

Think out how to extend your program to sort all the components in array. For example: Suppose the values stored in array are:
3 22 45 32 55 90 3333 43 75 20

Your output:
3 20 22 32 43 45 55 75 90 3333