### Introduction to Scientific ProgrammingComputational Problem Solving Using:Maple and CMathematica and C

Author:
Joseph L. Zachary
Online Resources:
Maple/C Version
Mathematica/C Version

# Arrays Tutorial

In this lesson you will do eight exercises designed to give you experience with using loops and arrays.

# Change a While Loop to a For Loop

Copy array1.c into your home directory.

It prints out an array. Change the program so that it uses a for loop instead a while loop.

```Loop Section:
-1, 3, 14, 9, 15,```

The places in the code where code needs to be inserted is marked with comments delimited with `/**` and `**/` that explain what sort of code should be inserted. Your modifications must be limited to replacing the comments with appropriate code. In particular, do not add or remove any printf or scanf statements.

Compile, test, and modify your program until you have it right. If you get hopelessly confused, download a fresh copy and start over.

Compile and test it until you have it right.

If you did something wrong, you will be notified. Otherwise, you will be told that your program was correct.

# Using a While Loop with an Array

Copy array2.c into your home directory.

Modify the program so that it prints out the array "myArray" backwards. So, if your array were

`        1 3 4 3 5`

You would print out

```Loop Section:
5, 3, 4, 3, 1,```

The places in the code where code needs to be inserted is marked with comments delimited with `/**` and `**/` that explain what sort of code should be inserted. Your modifications must be limited to replacing the comments with appropriate code. In particular, do not add or remove any printf or scanf statements.

Compile, test, and modify your program until you have it right. If you get hopelessly confused, download a fresh copy and start over.

Compile and test it until you have it right.

If you did something wrong, you will be notified. Otherwise, you will be told that your program was correct.

# Adding 1 to All Array Elements

Copy array3.c into your home directory.

Modify the code to add 1 to each element of the array myArray.

In this program, you will not use the 0th element of the array at all. Sometimes it is useful to pretend that an array starts at 1 rather than 0. This is because people are used to dealing with lists that start at 1 rather than 0 so it makes the code easier to read. Or, it can be because we are using data from another source to run our program, and the other source considers the data to start at 1, not 0.

In that case, you just ignore the 0th element of the array. But, it also means that your array will need to be 1 element longer than the data to be stored in it, since we not using the 0th array element. If you need an array to hold 4 elements and you are not going to use the 0th element, you need to declare the array to be 5 elements long. The code shows this.

In all calculations you will begin with the 1st element (7) not the 0th element (-1).

Each time through the first loop you will add one to the array element and then print out the value. Then, print the array, again starting with the first element.

For example, if the array were [-1, 5, 6], the program would print out:

```Loop Section:
Element 1, is 6
Element 2, is 7

6,  7,```

The places in the code where code needs to be inserted is marked with comments delimited with `/**` and `**/` that explain what sort of code should be inserted. Your modifications must be limited to replacing the comments with appropriate code. In particular, do not add or remove any printf or scanf statements.

Compile, test, and modify your program until you have it right. If you get hopelessly confused, download a fresh copy and start over.

Compile and test it until you have it right.

If you did something wrong, you will be notified. Otherwise, you will be told that your program was correct.

# Adding to an Array with Stopping Criterion

Copy array4.c into your home directory.

There are times when you want to run your loop until some condition is met. For example, in Newtons's method you want to keep calculating the derivative until your positive and negative guesses are within some distance of each other.

In this program, you will be adding one to each element of the array. You will stop after one of the elements reaches 17. (You will still finish the current iteration, rather than stopping all processing as soon as on element equals 17.) For example, if the array was (13, 14, 4), you would stop when the array was (16, 17, 7). Then the program will print out the array.

The places in the code where code needs to be inserted is marked with comments delimited with `/**` and `**/` that explain what sort of code should be inserted. Your modifications must be limited to replacing the comments with appropriate code. In particular, do not add or remove any printf or scanf statements.

Compile, test, and modify your program until you have it right. If you get hopelessly confused, download a fresh copy and start over.

Compile and test it until you have it right.

If you did something wrong, you will be notified. Otherwise, you will be told that your program was correct.

Copy array5.c into your home directory.

Modify the program to add two arrays together, putting the results into a third array. Then print the third array out. This time, use all the array elements including the 0th one and print all of them out including the 0th one. So, you would add the 0th element of array1 to the 0th element of array2, and put the result into the 0th element of sumArray. Then do the same for each of the other array elements.

The places in the code where code needs to be inserted is marked with comments delimited with `/**` and `**/` that explain what sort of code should be inserted. Your modifications must be limited to replacing the comments with appropriate code. In particular, do not add or remove any printf or scanf statements.

Compile, test, and modify your program until you have it right. If you get hopelessly confused, download a fresh copy and start over.

Compile and test it until you have it right.

If you did something wrong, you will be notified. Otherwise, you will be told that your program was correct.

# Return the Index of an Array Element

Copy array6.c into your home directory.

Modify the program to find the array index of the number 9 in the array.

The places in the code where code needs to be inserted is marked with comments delimited with `/**` and `**/` that explain what sort of code should be inserted. Your modifications must be limited to replacing the comments with appropriate code. In particular, do not add or remove any printf or scanf statements.

Compile, test, and modify your program until you have it right. If you get hopelessly confused, download a fresh copy and start over.

Compile and test it until you have it right.

If you did something wrong, you will be notified. Otherwise, you will be told that your program was correct.

# Return the Index of an Input Array Element

Copy array7.c into your home directory.

Modify the program to find the array index of the number n in the array, where n is input into the program.

The places in the code where code needs to be inserted is marked with comments delimited with `/**` and `**/` that explain what sort of code should be inserted. Your modifications must be limited to replacing the comments with appropriate code. In particular, do not add or remove any printf or scanf statements.

Compile, test, and modify your program until you have it right. If you get hopelessly confused, download a fresh copy and start over.

Compile and test it until you have it right.

If you did something wrong, you will be notified. Otherwise, you will be told that your program was correct.

# Average an Array by 3's

Copy array8.c into your home directory.

Complete the program to find the average of each three consecutive elements in the given array, and then print the averages out. The two end points will stay the same.

So, if the array was [1, 3, 8, 7] the output would look like

```Loop Section:
After averaging element 1, of the new array is 4
After averaging element 2, of the new array is 6

1,  4,  6,  7,```

since the average of 1, 3, and 8 is 4, etc. Don't worry about truncating (the average will be a floating point number between 2 integers and get truncated to the lower one) since the numbers are chosen to come out to an integer.

You will need a second array, or your numbers will be incorrect. Why?

The places in the code where code needs to be inserted is marked with comments delimited with `/**` and `**/` that explain what sort of code should be inserted. Your modifications must be limited to replacing the comments with appropriate code. In particular, do not add or remove any printf or scanf statements.

Compile, test, and modify your program until you have it right. If you get hopelessly confused, download a fresh copy and start over.

Compile and test it until you have it right.