C# homework
COMP2240 Programming II. Fall 2015
Department of Computer Science
Lab 8 Manual
Objectives
After completing this lab, you will:
· Be able to insert an element
· Be able to delete an element
· Be able to search for an element
· Be able to edit an element
Pre-requisite:
· Lab 7
Lab activity description
There are total of six (6) exercises in this lab session. The level of difficulty is from low to high.
Estimated time to complete this lab: 90 minutes
Exercise 1:
Insert
|
Tasks |
Detailed steps |
|
Create an array of integer int[] a = new int[SIZE];
|
Note that you will need to create an array that has extra space for newly added element. Use an integer variable (call it currSize) to keep track of the number of elements currently in the array. This is number should less than the allocated size of the array. Make currentSize global (static) static int SIZE = 100; static int currSize;
|
|
Fill in the array from keyboard, then print the content of the array. Note, you need to implement the 2 methods: Input(a, currSize), and Output(a, currSize) Refer to exercise 4 of Lab 7. |
Console.Write("Enter number of elements) currSize = int.Parse(Console.ReadLine()); Input(a, currSize); Console.WriteLine("You entered the following array: "); Output(a, currSize);
|
|
Prompt for the number to be inserted and the location (index) where the new number to be placed. Then do the insertion and show the array after the operation. The Insert method is given below: static void Insert(int[] a, int n, int p) { for (int i = currSize; i > p; i--) a[i] = a[i - 1];
a[p] = n; currSize++; }
|
Console.Write("Enter a number to insert: "); int x = int.Parse(Console.ReadLine()); Console.Write("Enter the position to insert: "); int pos = int.Parse(Console.ReadLine()); Insert(a, x, pos); Output(a, currSize);
On a piece of paper draw the array before and after the insertion. |
|
Copy and paste your code below. |
Build and run the program. |
Exercise 2:
Delete
|
Tasks |
Detailed steps |
|
Prompt for the index where the element is to be deleted. Then do the deletion and show the array after the operation. The Remove method is somewhat similar to the Insert method. You implement this method by yourself.
|
Console.Write("Enter the position to delete: "); int pos2 = int.Parse(Console.ReadLine()); Remove(a, pos2); Output(a, currSize);
|
|
Copy and paste your code below. |
Build and run the program. |
Exercise 3:
Search
|
Tasks |
Detailed steps |
|
In this exercise, you are going to search for a number in the array. Note, The method SearchOne only finds the first matching element, if there are more than one.
|
Console.Write("Enter the target to search for: "); int target = int.Parse(Console.ReadLine()); int ret = SearchOne(a, target); if(ret == -1) Console.WriteLine("Not found!"); else Console.WriteLine("Target found at {0}", ret);
|
|
Implement the SearchOne(a, target) method. |
Use loop to compare each element with the search target. |
|
Copy and paste your code below. Make sure you search an existing number as well as a non existing number. |
Build and run the program. |
Exercise 4:
Edit (Update, Replace)
|
Tasks |
Detailed steps |
|
(a)Define a static method that can be used to replace the number in a specified location with another number |
Type: void Parameters: an array of int; two int parameters, one for the index; the other for the new value Prompt for the index where the number is to be replaced; Prompt for the new number; Call the method to do the update
|
|
(b)Define a static method that can be used to replace the number with specified value. Again, this method only replace the first matching number. |
Type: void Parameters: an array of int; two int parameters, one for the old value; the other for the new value The method calls the SerachOne method to find the index of the number to be replaced, then calls the method defined in (a)
|
|
Copy and paste your code below. Make sure you test both methods defined in (a) and (b). |
Build and run the program. |
Exercise 5:
Integration
|
Tasks |
Detailed steps |
|
You may use a menu to allow user to test your methods. |
Set up a menu method that displays the testing options corresponding to the methods of exercise 1-4. The menu method returns user selection. In the testing method (Main) branch off the operations according to the user selection returned from the menu method. Allow multiple testing. |
|
Copy and paste your code below. |
Build and run the program. |
Exercise 6:
Bonus
|
Tasks |
Detailed steps |
|
In this exercise, you are going to search for a number in the array. Note, The method finds all matching elements, if there are more than one.
|
Need to create another array to store all the indices of the matching elements. The size of this array should be currSize, in case all elements are identical and match the target.
|
|
You may use a menu to allow user to test your methods. |
Set up a menu method that displays the testing options corresponding to the methods of exercise 4. The menu method returns user selection. In the testing method (Main) branch off the operations according to the user selection returned from the menu method. Allow multiple testing. |
|
Copy and paste your code below. |
Build and run the program. |