CSE110 Summer Session I

Homework #5: Arrays

Due Tuesday, June 26 2001

NOTE: Please remember to provide relevant comments on all programs
Also, name your files hw5p1a.c, hw5p1b.c, hw5p2a.c, hw5p2b.c, hw5p2c, hw5p2d

Further demonstrations of the desired output are available by running the following files:
/html/courses/cse110/hw5p1a
/html/courses/cse110/hw5p1b
/html/courses/cse110/hw5p2a
/html/courses/cse110/hw5p2b
/html/courses/cse110/hw5p2c
/html/courses/cse110/hw5p2d


1. a) [10 Points] The dot product of two n-dimensional vectors A and B is an integer defined as the sum of A[i] * B[i] for 1 <= i <= n. Write a function int DotProduct(int A[], int B[], int n) that returns the dot product of arrays A and B of length n. Write a main function to test your DotProduct function. You may use GetIntegerArray( ) from the notes or write your own procedure for getting the input arrays. Assume that the maximum size of the array is some constant MAXSIZE, which should be set to 100 with #define MAXSIZE 100

% a.out
enter an integer n: 3
enter A[0] of vector A: 1
enter A[1] of vector A: 2
enter A[2] of vector A: 3
enter B[0] of vector B: 4
enter B[1] of vector B: 5
enter B[2] of vector B: 6
the dot product is 32
(You don't have to test if n is more than MAXSIZE )

b) [30 Points] The multiplication of two n by n matrices A and B results in an n by n matrix C whose entries are defined as C[i][j] = DotProduct(row i of A, column j of B). Write a method void MatrixMult(int A[][MAXSIZE], int B[][MAXSIZE],int n) that prints out the result of multiplying n by n matrices A and B. Write a main function that reads in two n by n matrices and prints out the product.

(You don't have to test if n is more than MAXSIZE )

% a.out
enter an integer n: 2
enter A[0][0] of matrix A: 1
enter A[0][1] of matrix A: 2
enter A[1][0] of matrix A: 3
enter A[1][1] of matrix A: 4
enter B[0][0] of matrix B: 5
enter B[0][1] of matrix B: 6
enter B[1][0] of matrix B: 7
enter B[1][1] of matrix B: 8

19 22 
43 50


2. a) [10 Points] Write a function void GetWord(char word[]) that reads in a word by getting a char at a time and storing it into the word[ ] array until a newline character is encountered. After reading in the word, put a null character '\0' in the array to mark the end of the word. Assume a maximum word length of 20 characters (including the null character). Write a main function to test it. Tip: terminating a char array with a null character is the common way strings are handled in C. You can print out a null terminated char array with something like the following: printf("word is %s", word).

% a.out
enter a word: foot
word is foot

b) [10 Points] Write a function void Reverse(char word[], char rev[]) that reverses the order of the chars in word[ ] and places it in rev[ ]. Write a main function to test it.

% a.out
enter a word: stressed
reverse is desserts

c) [20 Points] Write a function bool LexOrder(char wordA[], char wordB[]) that returns true if either wordA comes before wordB in the dictionary or if wordA is equal to wordB and returns false otherwise. You may assume all letters are lowercase. Write a main function to test it.

% a.out
enter a word: cheese
enter a word: fish
cheese comes before fish

% a.out
enter a word: football
enter a word: foot
foot comes before football

d) [20 Points] Write a function bool IsPalindrome(char word[]) that returns true if the word is a palindrome, i.e. it is spelled the same forwards and backwards. Hint: this is a very simple function to write if you make use of LexOrder and Reverse. Write a main function to test it.

% a.out
enter a word: kayak
kayak is a palindrome
% a.out
enter a word: canoe
canoe is not a palindrome