You are on page 1of 2

CSE 1018Y - Computer Programming

Labsheet 5 (Arrays and Functions ): Weeks starting 15.10.2012, 22.10.2012

1. Declare an array of type int, with 5 elements. Withoutinitialising the array, write a program,
including a for loop, that displays the contents of the array. What do you get as answer?
2. Modify the program you wrote for Question 1 so that the array is initialised with the first five
odd numbers. Use a for loop for the initialisation.
3. Modify the program you wrote for Question 1 so that the array is initialisedat declaration with
the first five odd numbers.
4. Declare an array of type float, with 12 elements, to hold monthly sales income in thousands
of pounds. Write a program that allows a user to input values for the array, compute average
sales for the year and then displays the average sales.
5. Write a program that allows a user to input a line of text and displays the number of words in
the text and the number of vowels in the text.
6. Write a program that allows a user to input a number of integers in an array and then displays
the smallest as well as the largest in the list.
7. Write and test a program that maintains a set of student id numbers (integer in the range 701799) and average course mark for each student (floating-point number in the range 0.0 to
100.0). The program will allow the user to input the student id number and the coursework
mark for each student. After all the input has been entered, the program will display the ids of
the students with the highest and the lowest marks. [Hint: Use two one-dimensional arrays].

Arrays and functions


8. Write a program that allows the entry of an integer value n, followed by two sets of n integer
values into arrays A and B. The program should display the sum of corresponding values of A
and B.
9. Write a program that allows the entry of an integer value n, followed by two sets of n integer
values into arrays A and B. The program should use a function to calculate the sum of the
square of corresponding values of A and B. These values should then be displayed.
10. Write a function that takes in as parameters an array x of integer and an integer value n and
returns the largest among the first n elements of array x. Write also a main program where you
initialize the array with some random values and use the function to get the max value among
the first n elements.
11. Write a program that reads in a sequence of characters until the symbol * is encountered. Use a
function that will display the sequence in reverse order. (Make use of arrays).

12. Write a program that contains the following:

(i)
A function to read an NM matrix.
(ii)
A function to display the NXM matrix on the screen.
(iii)
A function to add these two matrices.
(iv)
A function to subtract these two matrices.
(v)
A function to multiply these two matrices.
The program must also contain a main function that will first declare two 3x3 matrices A and
B, allow input of data for A and B (using the function in (i)), andshould continuously display
a menu as follows:
1. Add the matrices
2. Subtract the matrices
3. Multiply the matrices
4. Exit
Please enter your choice(1-4) :
Upon selection of 1 or 2 or 3, the program should display the corresponding result. When
4 is selected, the program should exit.

You might also like