CS 33: Lab #08

# CS 33: Computer Organization

## LAB 08: Pointers, Arrays, and Strings

Due 11:59pm Wednesday, November 19.

The program handin33 will only submit files in the cs33/lab/08 directory. (You should run update33 first to set up the directory and create any necessary files.)

Remember: You are encouraged to work with a partner.

Each program must follow these following guidelines:

• the file should be named using the name provided,
• your program should follow all input and output guidelines exactly, and
• your program should be compiled via a Makefile.

1. In this first program (called maxptr.c) you will write two functions whose prototypes are shown here:
```int *max(int *a, int *b);
int *max3(int *a, int *b, int *c);
```
(These functions should be familiar to you -- we discussed something very similar in class on Tuesday.)

The max function has two pointers to integers as parameters and returns the pointer that points to the larger integer.

The max3 function has three pointers to integers as parameters and returns the pointer that points to the largest integer. The max3 function should make use of the max function in computing its answer.

In main, ask the user to enter three values, then increment the largest value, and print all three values back to the user.

Here is sample output (underlined text is text entered by the user):

```Enter integer values for x, y, and z: 3 8 5
The values after incrementing the largest value are:
x = 3, y = 9, z = 5
```

2. Write a program called polar.c which will assist in converting between Polar and Cartesian coordinates. If you have forgotten what these coordinate systems are, you may want to read this article to give yourself quick refresher.

You may want to refer to the text, section 19.2, for a discussion and examples of structures.

This question is broken down into a number of parts.

1. Use typedef to create a new type called Cartesian. The type Cartesian is struct with two floating point values, x and y, which represents an (x, y) point in the Cartesian coordinate system.
2. Use typedef to create a new type called Polar. The type Polar is struct with two floating point values, r and theta, which represents an (r, θ) point in the Polar coordinate system.
3. Write the following three functions specified by their prototypes:
```float sqr(float n);
Polar toPolar(Cartesian c);
Cartesian toCartesian(Polar p);
```
• The function sqr(n) returns n squared
• The function toPolar(c) converts a Cartesian point (x, y) into a Polar point (r, θ) using:
```r = sqrt( x2 + y2 )   /* use your sqr function */
θ = atan2f( y, x )    /* see note below        */
```
(Note: atan2f and sqrt are part of math.h and requires you compile your code with the -lm flag.)
• The function toCartesian(p) converts a Polar point (r, θ) into a Cartesian point (x, y) using:
```x = r * cos(θ)
y = r * sin(θ)
```
4. Into main, add some simple test code that allows users to enter Cartesian and Polar points and shows their conversion. Below, underlined text is text entered by the user:
```Enter a Cartesian point: 1 1
Cartesian (1.000000, 1.000000) == Polar (1.414214, 0.785398)
Enter a Polar point: 5 1.5708
Polar (5.000000, 1.570800) == Cartesian (-0.000018, 5.000000)
```

3. In this program (called selection.c) you will write a program that sorts an array of integers using Selection Sort. In Selection Sort, the values of an array are sorted (from smallest to largest) by using the following algorithm:
• Set i = 0
• Find the smallest element in the portion of the list from i to the end.
• Swap this smallest element with the element in position i.
• Increment i
• Repeat until i is the index of the last element of the list.

1. Use #define to set ARRAYSIZE to 25.
2. In main, create an array of integers with space to hold ARRAYSIZE values.
3. Write a function called populateArray which populates an array of integers with random values between 0 and highVal (inclusive). If highVal ≤ 0, all elements are set to 0. See lectures/11-06-Th/dieroll.c for reference on how to create random values and on how to initialize the random number generator. The prototype for this function is:
```void populateArray(int *array, int size, int highVal);
```
4. Write a function called isSorted that returns TRUE if the array is in sorted order (from smallest to largest); otherwise, it returns FALSE. Both TRUE and FALSE should be defined using the #define syntax, and the type Boolean should be created used typedef. The prototype for this function is shown below:
```Boolean isSorted(int *array, int size);
```
5. Write the swap and selectionSort functions according to the prototypes shown here:
```void swap(int *a, int *b);
void selectionSort(int *array, int size);
```
6. In main, write some test code that allows the user to enter the value of highVal, prints out the array before it's sorted and the value of isSorted, then prints out the array after it's been sorted and, again, the value of isSorted.