CSC 161  Grinnell College  Spring, 2009 
Imperative Problem Solving and Data Structures  
This lab provides experience using arrays in the C programming language.
The following program fragment prints the digits of the number 'x' in base three, but in reverse order:
int x = 22; while (x > 0) { printf("%d", x % 3); x = x / 3; } printf("\n");For example, if x is 22, which is represented as 211 in base 3, the program will output "112". Write a small program that includes this program fragment, and verify that it will print "112" when x is set to 22.
Do you understand the algorithm? If not, consider the same algorithm but replace each '3' with '10'. It will print the digits of x (in base 10) in reverse order. Try it if you are not completely sure, and then see if you understand the base 10 algorithm.
Modify your program from the previous exercise so that it uses an an array to store each digit of the base three representation of the number. Then print out the digits in the correct order. Verify that your program will now print "211" when x is set to 22.
Write a program that prompts for and accepts three numbers from the user, storing them in a 3element array. Your program should then ask the user for an index number, check whether the given index is valid, and either print out the value stored at the given location or print an error message (if the index is invalid).
Once the program is working correctly, modify it so that it does not verify the correctness of the array index. What happens when you enter inappropriate index values? You may want to try reasonably small errors (say index=3) as well as ridiculous indices (say index=123456789). Do you understand the results that you get?
Arrays may be used as parameters to functions, but since arrays in C do not store information about their lengths, you will frequently need to pass the array length as well.
Note that, although all parameters are passed by value in C, when we pass arrays it is actually the address of the array that is passed. Therefore, a function can modify the values stored in the original array argument. (We will study this more carefully in a few days when we consider pointers in C.)
In mathematical computations, arrays can be used to represent vectors in nspace (e.g., a 3element array can represent a 3dimensional vector). The 1norm of a vector is the sum of the absolute values of the vector components.
Write a function named oneNorm that computes the 1norm of a vector having n components. Use the function fabs from the math library to compute absolute values of floatingpoint numbers. The prototype of your function should be
double oneNorm( double v[], int n )where v represents the vector and n represents the number of components (i.e., the dimensionality) of the vector.
Write a main
function that reads in a vector in
R^{5} (use an array of type double), calls your function
oneNorm
, and prints the vector's 1norm.
Don't forget to #include <math.h> and compile with the "lm" flag, and don't forget to test your program.
The Euclidean norm of a vector v is given by the squareroot of the sum of the squares of its components. (So the Euclidean norm is what you may think of as the "length" of a vector. However, all of the vector norms are different definitions of vector length.)
In the same program that you started in the previous exercise, write a function that computes the Euclidean norm (also known as the 2norm) of an given vector.
Of course, being the proud programmer you are, you will test this function thoroughly as well.
One vector can be subtracted from another (of the same dimensionality) by subtracting the corresponding elements. Note that this operation yields a vector.
The distance between two vectors v and w is defined to be the norm of their difference.
Write a C function that computes the distance between two given vectors. One parameter to the function should be the type of norm that should be used to compute the distance. You should make use of the functions prepared in parts a and b. The prototype of your function should be as follows:
double distance( double v[], double w[], int n, int norm )
If norm
has a value other than 1 or 2, return the value
1. (Note that this value can not possibly be a valid distance.)
Modify your main
function from above to test this function.
In C, we can declare a 2dimensional integer array named table
with
int table[rows][cols];where
rows
and cols
hold integer values.
Individual elements in the 2darray are referred to with
table[i][j]where i indicates the row number and j indicates the column number in the array. Beware that, like 1dimensional arrays in C, the array subscripts range from 0 to (rows1), and from 0 to (cols1). Thus, the element in the upperleft corner of the array is
table[0][0]
. It is
not table[1][1]
as you might expect from standard
matrix notation.
We can initialize a 2dimensional array in the declaration statement as follows:
int table[4][3] = { { 1, 2, 3}, { 4, 5, 6}, { 7, 8, 9}, {10,11,12} };The constant initializers do not need to be on separate lines, but this does allow us to visualize the array in 2D.
Write a C program that declares and initializes an array as shown above, and then prints the array elements, in a 4x3 matrix format.
Modify your program from the previous exercise, so that it also prints the sum of each row and the sum of each column in the array. You may use any format for this that you like, as long as it is convenient for checking your results.
This document is available on the World Wide Web as
http://www.walker.cs.grinnell.edu/courses/153.sp09/labs/labarraysc.shtml
created Fall 2001, Fall 2002 by Alan Rosenthal, University of Toronto revised Fall 2003, Fall 2004 by Tom Fairgrieve, University of Toronto revised Fall 2004, Fall 2005 by Marge Coahran, University of Toronto revised (with permission) January 2007March 2008 by Marge Coahran, Grinnell College revised April 2008 by Henry M. Walker last revised 24 January 2009 by Henry M. Walker 

For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. 