Sei sulla pagina 1di 3

CSE 1003 Computer Programming Labsheet 9 Arrays Question 1 Given the initial statements: s1 = [2, 1, 4, 3] s2 = [c, a, b] Show the

he result of evaluating each of the following sequence expressions: i. s1 + s2 ii. 3 * s1 + 2 * s2 iii. s1[1] iv. s1[1:3] v. s1 + s2[-1] Question 2 Write a program that allows input to a list and displays the elements in reverse order. Question 3 Write a function that takes as argument a list and returns the largest and small est element in the list. Question 4 Write a program that accepts a list of student names and their marks in Maths an d Physics. The program must then print all students details along with their aver age marks. Hint: studentNames = [ Jim , Jack , Jill , Jane , ......] marksMaths = [70, 30, 56, 78, ...] marksPhysics = [90, 56, 89, 70, ...] marksMaths[stud] and marksPhysics[stud] refer to the marks of student stud in Ma ths and Physics respectively e.g. Jim, (studentNames[0]) scored 70 in Maths (mar ksMaths[0]) and 90 in Physics (marks_Physics[0]).

Question 5 Modify your program in Question 4 above such that it also stores the students nam e and average mark in a file studaverage.dat. Hence, each line in the file will include a student name and an average mark. Question 6 Write a program that checks whether a list is symmetric. Example of a symmetric list is [a, b, c, d, c, b, a] or [a, b, b, a]. Question 7 There are two ways to display a 2-dimensional list and these are row major and c olumn major. Consider the following 2-dimensional list. If we display in row major, the results will be as follows: a, b, c, d, e, f, g,

h, i, j, k, l. However, if we display in column major, the data will be a, e, i , b, f, j, c, g, k, d, h, l. Write a program which allows data entry to a 2-dime nsional list and displays the elements in row major and column major. Question 8 Write a function which takes as input a 2-dimensional list, and counts and outpu ts the number of perfect squares in the list. Question 9 The Sieve of Eratosthenes is an elegant algorithm for finding all of the prime n umbers up to some limit n. The basic idea is to first create a list of numbers f rom 2 to n. The first number is removed from the list, and announced as a prime number, and all multiples of this number up to n are removed from the list. This process continues until the list is empty. For example, if we wished to find all the primes up to 10, the list would origin ally contain 2, 3, 4, 5, 6, 7, 8, 9, 10. 2 is removed and announced to be prime. Then 4, 6, 8 and 10 are removed, since they are multiples of 2. This leaves num bers 3, 5, 7, 9 in the list. Repeating the process, 3 is announced as prime and removed. 9 is removed because it is a multiple of 3. This leaves numbers 5 and 7 in the list. The algorithm continues by announcing that 5 is prime and removing it from the list. Finally, 7 is announced and removed. Now, we re done. Write a program that prompts a user for n and then uses the sieve algorithm to f ind all the primes less than or equal to n. Question 10 Write the appropriate codes to allow matrix multiplication. Notes: For the multiplication of two matrices A and B to be defined, the two matrices m ust be conformant, i.e. the number of columns in the first one should be equal t o the number of rows in the second one. For this question, simply assume that both matrices are of the same order, i.e. they have the same number of rows and columns. Question 11 Write a program load all existing student records, that include the studentID, n ame, address, programme of study, level of study and current cpa, from the file studentdetails.dat, and store them in an array. The program must then display th e following menu and allow the user to select one among the various options. ************************************ *********** Student System *********** ************************************ 1. Add student record 2. Modify student record 3. Delete student record 4. Print student record 5. Print records of all students 6. Print record of first student in class 7. Save students records 0. Exit ************************************ Please enter your choice: 1. Add student record allows the entry of a student record that includes the

studentID, name, address, programme of study, level of study and current cpa. 2. Modify student record allows the user to type in a studentID and allows th e update of his/her details if the studentID exists. Note however that the stude ntID may not change. 3. Delete student record allows the user to type in a studentID and all the s tudent details are deleted from the list, if the studentID exists. Else, an appr opriate message must be given to inform the user that no record exists with the specified studentID. 4. Print student record prints the students details, for which the studentID i s entered by the user. 5. Print record of first student in class displays details pertaining to the student who came out first, i.e. has obtained the highest cpa. 6. Print records of all students prints details of all students. 7. Save students records saves all changes made to the file studentdetails.dat . 8. Exit exits from the program.

Potrebbero piacerti anche