CS 12 - Arrays
CS 12 Homepage
One of the harder-to-grasp initial concepts in programming is that of the
array. To help you firm up your understanding and skills with arrays,
today we will focus on some simple array exercises.
The Assignment
7 points:
Implement a program that reads in a list of integers (up to 20 of
them), and then performs a sequence of operations on them. The
minimum operations that your program must perform are as follows:
- Sum: print out the sum of the integers in your array.
- Print: print out the array
- Print backward: print out the array in reverse order
- Reverse: reverse the array
- Drop first: remove the first element of the array
Each of the operations must be written as their own function.
.2 points: Find max: write a function to take in an array and
return the value of the largest element in the array
.2 points: Add last two: write a function to take in an array
and return the sum of the last two elements in it.
.2 points: Append: write a function to take in an array and an
integer and append the integer onto the end of the array.
.2 points: What happens when you call the above function 100
times for an array of size 20?
.2 points: Using the two previous functions, and using a
starting array [1, 1], calculate the first 20 numbers in the Fibonacci
sequence.
.2 points: Write a function to calculate the (floating point) average
of an array of integers.
.2 points: Write a function to calculate the standard deviation
of an array of integers. (Check Google if you don't remember what
standard deviation is.)
.2 points: Sort: Write a function to take in an array of
integers and return that array in sorted order (that is, the first
element is the smallest, and each element after the first is greater than or
equal to the element preceding it).
When you are finished, use the CS department Turnin to submit your work.
© 2003 UC Riverside Department of Computer Science &
Engineering. All rights reserved.