You are on page 1of 4

Tutorial 10a QUESTIONS

Array

1. For the declaration char grades [5] ; How many memory cells are allocated for data storage? What types of data can be stored there? How does one refer to the initial array element? To the final array element? 2. For the following array, describe what happens when the following statements are executed for i = 3 ;

Array x
0.0 i = 3; printf (%d %.1f , 4, x[4]) ; printf (%d %.1f , i, x[i] ) ; printf (%.1f, x[i] + 1) ; printf (%.1f, x[i] + i) ; printf (%.1f, x[i + 1]) ; printf (%.1f, x[i + i]) ; printf (%.1f, x[2 * i] ) ; printf (%.1f, x[2 * i 3]) ; printf (%.1f, x[ (int)x[4] ]) ; printf (%.1f, x[i ++]) ; printf (%.1f, x[--i]) ; x[i 1] = x[i] ; x[i] = x[i +1] ; x[i] - 1 = x[1] ; 1.0 2.0 8.0 12.0 5.0 14.0 7.0

3. Is the following legal? int x[5] = {1, 2, 3, 4, 5} ; int y[5] ; y = x; 4. Write prototype for the following. Do not write the functions. i) A function called largest that gets an array of integers and returns an integer. ii) A function called price that gets two double array names for input and a double array name for output. The function does not return a value.

5. Combine the following two statements into one statement. db1_arr[i] = data ; ++ i ; 6. We can represent a real polynomial p(x) of degree 3 using an array of the real coefficients a0, a1, a2 and a3. p(x) = a0x3 + a1x2 + a2x + a3 Write a function get_poly that inputs a polynomial of degree 3. It fills the double array of coefficients, coeff [ ], with inputs from the user. Also write a function poly that evaluates the polynomial at a given value of x. Use the following prototypes. void get_poly (double coeff [ ]) ; double poly (double x, double coeff [ ]) ; The above two function can be used in the question to estimate the roots of a cubic polynomial in Tutorial 9. HANDS-ON EXERCISES 7. Write a program to sum an array of integers called number which has 12 elements. Initialize the array such that number[i] = i . 8. Write a program to print the histogram of the values in an array. For example with the following array, int n[10] = { 18, 3, 15, 7, 11, 9, 13, 5, 17, 1 } ; the output is as follows :

Element
0 1 2 3 4 5 6 7 8 9

Value
18 3 15 7 11 9 13 5 17 1

Histogram
****************** *** *************** ******* *********** ********* ************* ***** ***************** *

9. Write a program to find the maximum of all the elements of an int array a[10] = {1, 2, 0, 0, 4, 5, 6, 9, 9, 17}. The main program uses the following two function prototypes to print the array and find the maximum value respectively. void print_array (int a[ ] , int arraysize) ; int max_array (const int a[ ] , int arraysize) ; 10. [*] Write a function called second which takes, as parameters, an array of positive integers and also an integer representing the size of the array. The function returns the value of the second largest integer in the array. If the largest integer in the array repeats, then the second largest integer is also the largest. For example, if we have {1, 2, 3, 4, 5, 5 }, the second largest integer is 5. The function should not change the contents of the array in any way. You can assume that the size of the array is at least two. HINT: Keep track of the largest number as well as the second largest number seen so far at all times.

Tutorial 10b QUESTIONS

Multidimensional Array

1. Write a function print_matrix (int array [3] [3]) that will print the elements of the 3x3 array as follows: array[0][0] array[1][0] array[2][0] array[0][1] array[1][1] array[2][1] array[0][2] array[1][2] array[2][2]

HANDS-ON EXERCISES 2. Write a function that display the values on the diagonal of a functions 10 x 10 array parameter. 3. Write a program that defines 3 matrices p, q, r of size 3x3 with int elements; initialize the first two matrices (p and q) as follows. int p[3][3] = { {1, 3, -4 }, { 1, 1, -2}, { -1, -2, 5} } ; int q[3][3] = { { 8, 3, 0 }, { 3, 10, 2 }, { 0, 2, 6 } }; int r[3][3] ; Compute the matrix addition of p and q and store it in r (i.e. r = p + q) Print all the matrices on the screen. 4. [*] Write a program that definition 3 matrices p, q, r of size 3x3 with int elements; initialize the first two matrices (p and q) as follows. int p[3][3] = { {1, 3, -4 }, { 1, 1, -2}, { -1, -2, 5} } ; int q[3][3] = { { 8, 3, 0 }, { 3, 10, 2 }, { 0, 2, 6 } }; int r[3][3] ; Compute the matrix multiplication of p and q store it in r (i.e. r = p * q) Print all the matrices on the screen.

You might also like