Some operations with arrays are particularly common: searching if it contains a certain data, adding data to the end of the existing data, inserting data between two existing ones, deleting one of the stored data, etc.. So let's see the basic guidelines for performing these operations, and a sample source.
· To see if a data exists, we will have to travel across the array, comparing the value stored with the data being searched. We might just be interested in knowing if the data exists or not (in this case, we might interrupt the search the first time fe find it), or prefer to see which positions it is (for which we would have to go across all the array). If the array were sorted, we could search using a faster method, which we will see later.
· If we want to add data at he end of the existing ones, we need the array not to be completely full, and we must keep a count of how many positions are occupied, in order to save the data in the first free position.
· To insert data in a certain position, those left behind should "move right" to leave hole for it. This movement must start from the end, so that each moving data does not destroy the one following it. We also need to update the counter, to indicate that there is a free position less.
· If we want to delete the data in a certain position, the data after it must move "left" so that there are no gaps. As in the previous case, we must update the counter, but now to indicate that there is a free position less.
Let's see an example:
/*---------------------------*/
/* C# Example #36b: */
/* example36b.cs */
/* */
/* Adding and deleting in */
/* arrays */
/* */
/* Intro to C#, */
/* Nacho Cabanes */
/*---------------------------*/
using System;
public class Ejemplo36b
{
public static void Main()
{
int[] data = {10, 15, 12, 0, 0};
int capacity = 5; // Maximum capacity of the array
int quantity = 3; // Real amount of data
int i; // To visit all the elements
// We display the array
for (i=0; i<quantity; i++)
Console.Write("{0} ",data[i]);
Console.WriteLine();
// We look for data "15"
for (i=0; i<quantity; i++)
if (data[i] == 15)
Console.WriteLine("15 found in position {0} ", i+1);
// We add data at the end
Console.WriteLine("Adding 6 at the end");
if (quantity < capacity)
{
data[quantity] = 6;
quantity++;
}
// And we display the array again
for (i=0; i<quantity; i++)
Console.Write("{0} ",data[i]);
Console.WriteLine();
// We delete sample data
Console.WriteLine("Deleting second data");
int positionToDelete = 1;
for (i=positionToDelete; i<quantity-1; i++)
data[i] = data[i+1];
quantity--;
// And we display the array again
for (i=0; i<quantity; i++)
Console.Write("{0} ",data[i]);
Console.WriteLine();
// We insert data 30 in the third position
if (quantity < capacity)
{
Console.WriteLine("Inserting 30 in position 3");
int positionToInsert = 2;
for (i=quantity; i>positionToInsert; i--)
data[i] = data[i-1];
data[positionToInsert] = 30;
quantity++;
}
// And we display the array again
for (i=0; i<quantity; i++)
Console.Write("{0} ",data[i]);
Console.WriteLine();
}
}
which would display:
10 15 12
15 found in position 2
Adding 6 at the end
10 15 12 6
Deleting second data
10 12 6
Inserting 30 in position 3
10 12 30 6
This program does not warn the user when it is not the data being searched is not found. It can be improved by using a "boolean" variable , so that in the end we can tell if the data did not exist (we cannot use an "else", because with each pass of the "for" loop we do not know if the data does not exist, we just know that it is not at the current position).
Suggested exercises:
· Extend the previous example (36b), so that it tells the user if the searched data does not exist in the array.
· Create a program that prepares space for up to 10 names. It should show the user a menu, allowing him to perform the following operations:
1. Add a record int the end of the existing ones.
2. Insert data in a certain position (as already mentioned, those data left behind should move "right" to leave hole for it; for example, if the array contains "hello", "goodbye" and we tell to insert "good "in the second position, the array will contain" hello "," good "," goodbye ".
3. Erase the record that is in a certain position (as seen, the data behind that one should move "left" so that there are no gaps; for example, if the array contains "hello", "good", "goodbye" and we request to delete the data from the second position, the array will then contain "hello", "goodbye"
4. Display all the data contained in the array.
5. Quit the program.
No comments:
Post a Comment