0% found this document useful (0 votes)
116 views

Array Project PDF

The document contains multiple C# code examples that demonstrate the use of various array methods, including: 1) Array.Find, Array.FindAll, and Array.FindLast to search arrays and return matching elements. 2) Array.FindIndex and Array.LastIndexOf to return the index of matching elements. 3) Array.Sort to sort arrays in ascending order. 4) BinarySearch to search sorted arrays and return the index of matching elements. 5) Reverse to reverse the order of elements in an array. The examples show calling these methods on different array types like strings, characters, and integers to perform common array operations like searching, sorting, and reversing.

Uploaded by

Mubin Tunio
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
116 views

Array Project PDF

The document contains multiple C# code examples that demonstrate the use of various array methods, including: 1) Array.Find, Array.FindAll, and Array.FindLast to search arrays and return matching elements. 2) Array.FindIndex and Array.LastIndexOf to return the index of matching elements. 3) Array.Sort to sort arrays in ascending order. 4) BinarySearch to search sorted arrays and return the index of matching elements. 5) Reverse to reverse the order of elements in an array. The examples show calling these methods on different array types like strings, characters, and integers to perform common array operations like searching, sorting, and reversing.

Uploaded by

Mubin Tunio
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

C# program that uses Array.

Find static method


using System;

class Program
{
static void Main(string[] args)
{
//
// Use this array of string references.
//
string[] array1 = { "cat", "dog", "carrot", "bird" };
//
// Find first element starting with substring.
//
string value1 = Array.Find(array1,
element => element.StartsWith("car", StringComparison.Ordinal));
//
// Find first element of three characters length.
//
string value2 = Array.Find(array1,
element => element.Length == 3);
//
// Find all elements not greater than four letters long.
//
string[] array2 = Array.FindAll(array1,
element => element.Length <= 4);

Console.WriteLine(value1);
Console.WriteLine(value2);
Console.WriteLine(string.Join(",", array2));
Console.ReadLine();
}
}

Output

carrot
cat
cat,dog,bird
//////////////////////////////////////////////////////////////////////
C# program that uses Array.FindLast
using System;

class Program
{
static void Main()
{
string[] array = { "dot", "net", "yes","perls" };
// Find last string of length 3.
string result = Array.FindLast(array, s => s.Length == 3);
Console.WriteLine(result); }
}

Output
yes
C# program that uses Array.FindIndex method
using System;

class Program
{
static void Main()
{
int[] array = { 5, 6, 7, 6 };

// Use FindIndex method with predicate.


int index1 = Array.FindIndex(array, item => item == 6);
// Use LastFindIndex method with predicate.
int index2 = Array.FindLastIndex(array, item => item == 6);

// Write results.
Console.WriteLine("{0} = {1}", index1, array[index1]);
Console.WriteLine("{0} = {1}", index2, array[index2]);
}}

Output

1 = 6
3 = 6

C# program that sorts character array


using System;

class Program
{
static void Main()
{
char[] array = { 'z', 'a', 'b' };
// Convert array to a string and print it.
Console.WriteLine("UNSORTED: " + new string(array));

// Sort the char array.


Array.Sort<char>(array);
Console.WriteLine("SORTED: " + new string(array));
}
}

Output

UNSORTED: zab
SORTED: abz
///////////////////////////////////////////////////////////
C# program that uses Array.Sort

using System;

class Program
{
static void Main()
{
string[] colors = new string[]
{
"orange",
"blue",
"yellow",
"aqua",
"red"
};
// Call Array.Sort method.
Array.Sort(colors);
foreach (string color in colors)
{
Console.WriteLine(color);
}

Output

aqua
blue
orange
red
yellow
C# program that sorts copy
using System;
using System.Collections.Generic;

class Program
{
static void Main()
{
string[] array = { "zebra", "parrot", "ant" };

List<string> copy = new List<string>(array);


copy.Sort();

Console.WriteLine(string.Join(",", array));
Console.WriteLine(string.Join(",", copy));
}
}

Output

zebra,parrot,ant
ant,parrot,zebra
///////////////////////////////////////////////////////////
You can search for the occurrence of a value in an array with the IndexOf and LastIndexOf member
functions. IndexOf starts the search from a lower subscript and moves forward, and LastIndexOf
starts the search from an upper subscript and moves backwards. Both functions achieve a linear
search, visiting each element sequentially until they find the match forward or backward.

Listing below illustrates a linear search through an array using the IndexOf and LastIndexOf
methods.

Listing: Array Linear Search


// Linear Search

using System;

public class LinearSearcher

public static void Main()

String[] myArray = new String[7] { "kama", "dama", "lama", "yama", "pama", "rama", "lama" };

String myString = "lama";


Int32 myIndex;

// Search for the first occurrence of the duplicated value in a section of the
myIndex = Array.IndexOf(myArray, myString, 0, 6);
Console.WriteLine("The first occurrence of \"{myString}\" between index 0 and index 6 is at index
{myIndex}.");

// Search for the last occurrence of the duplicated value in a section of the
myIndex = Array.LastIndexOf(myArray, myString, 6, 7);

Console.WriteLine("The last occurrence of \"{myString}\" between index 0 and index 6 is at index


{myIndex}.");

Console.ReadLine();

}
The String class provides methods for sorting, searching, and reversing that are easy to use. Note that Sort,
BinarySearch, and Reverse are all static functions and are used for single-dimensional arrays.

Listing below illustrates usage of the Sort, BinarySearch, and Reverse functions.

Listing: Array Sort, Binarysearch, and Reverse Examples


// Binary Search

// Note an EXCEPTION occurs if the search element is not in the list

// We leave adding this functionality as homework!

using System;

class linSearch

public static void Main()

int[] a = new int[3];


Console.WriteLine("Enter number of elements you want to hold in the array (max3)?");
string s = Console.ReadLine();
int x = Int32.Parse(s);
Console.WriteLine("--------------------------------------------------");
Console.WriteLine("\n Enter array elements \n");
Console.WriteLine("--------------------------------------------------");

for (int i = 0; i < x; i++)


{
string s1 = Console.ReadLine();
a[i] = Int32.Parse(s1);
}

Console.WriteLine("Enter Search element\n");


Console.WriteLine("--------------------------------------------------");
string s3 = Console.ReadLine();
int x2 = Int32.Parse(s3);
// Sort the values of the Array.
Array.Sort(a);
Console.WriteLine("--------------Sorted-------------------------");
for (int i = 0; i < x; i++)
{

Console.WriteLine("Element {i + 1} is {a[i]}");
}

// BinarySearch the values of the Array.

int x3 = Array.BinarySearch(a, (Object)x2);


Console.WriteLine("--------------------------------------------------");
Console.WriteLine("Binary Search: " + x3);
Console.WriteLine("Element {x3} is {a[x3]}");
Console.WriteLine("--------------------------------------------------");

// Reverse the values of the Array.

Array.Reverse(a);
Console.WriteLine("-----------Reversed-------------------------------");
Console.WriteLine("----------------------------------------------");
for (int i = 0; i < x; i++)
{

Console.WriteLine("Element {i + 1} is {a[i]}");

/////////////////////////////////////////////////////////////////////////////////////////////

This all done by c# coding all of coding together ok for help

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplicationarray
{
class Program
{
static void Main(string[] args)
{

//
// Use this array of string references.
//
string[] array1 = { "cat", "dog", "carrot", "bird" };
//

// Find first element starting with substring.


//
string value1 = Array.Find(array1,
element => element.StartsWith("car", StringComparison.Ordinal));
//

// Find first element of three characters length.


//
string value2 = Array.Find(array1,
element => element.Length == 3);
//

// Find all elements not greater than four letters long.


//
string[] array2 = Array.FindAll(array1,
element => element.Length <= 4);

Console.WriteLine(value1);
Console.WriteLine(value2);
Console.WriteLine(string.Join(",", array2));

//////////////////////////////////
//C# program that uses Array.FindLast

{
string[] array = { "dot", "net", "yes", "perls" };
// Find last string of length 3.
string result = Array.FindLast(array, s => s.Length == 3);
Console.WriteLine(result);
}

//////////////////////////////////////////////////////////////

// Use this input array.

{
int[] array = { 5, 6, 7, 6 };

// Use FindIndex method with predicate.


int index1 = Array.FindIndex(array, item => item == 6);
// Use LastFindIndex method with predicate.
int index2 = Array.FindLastIndex(array, item => item == 6);

// Write results.
Console.WriteLine("{0} = {1}", index1, array[index1]);
Console.WriteLine("{0} = {1}", index2, array[index2]);
}

///////////////////////////////////////////////////////////////
{
char[] array = { 'z', 'a', 'b' };
// Convert array to a string and print it.
Console.WriteLine("UNSORTED: " + new string(array));

// Sort the char array.


Array.Sort<char>(array);
Console.WriteLine("SORTED: " + new string(array));
}

////////////////////////////////////////////////////////////////
string[] colors = new string[]
{
"orange",
"blue",
"yellow",
"aqua",
"red"
};
// Call Array.Sort method.
Array.Sort(colors);
foreach (string color in colors)
{
Console.WriteLine(color);
}

///////////////////////////////////////////////////////////////////
{
string[] array = { "zebra", "parrot", "ant" };

List<string> copy = new List<string>(array);


copy.Sort();

Console.WriteLine(string.Join(",", array));
Console.WriteLine(string.Join(",", copy));
}

/////////////////////////////////////////////////////////////////
String[] myArray = new String[7] { "kama", "dama", "lama", "yama", "pama", "rama", "lama" };
String myString = "lama";
Int32 myIndex;

// Search for the first occurrence of the duplicated value in a section of the
myIndex = Array.IndexOf(myArray, myString, 0, 6);
Console.WriteLine("The first occurrence of \"{myString}\" between index 0 and index 6 is at index
{myIndex}.");

// Search for the last occurrence of the duplicated value in a section of the
myIndex = Array.LastIndexOf(myArray, myString, 6, 7);
Console.WriteLine("The last occurrence of \"{myString}\" between index 0 and index 6 is at index
{myIndex}.");

///////////////////////////////////////////////////////////////////////////////
{
int[] a = new int[3];
Console.WriteLine("Enter number of elements you want to hold in the array (max3)?");
string s = Console.ReadLine();
int x = Int32.Parse(s);
Console.WriteLine("--------------------------------------------------");
Console.WriteLine("\n Enter array elements \n");
Console.WriteLine("--------------------------------------------------");

for (int i = 0; i < x; i++)


{
string s1 = Console.ReadLine();
a[i] = Int32.Parse(s1);
}

Console.WriteLine("Enter Search element\n");


Console.WriteLine("--------------------------------------------------");
string s3 = Console.ReadLine();
int x2 = Int32.Parse(s3);

// Sort the values of the Array.


Array.Sort(a);
Console.WriteLine("--------------Sorted-------------------------");
for (int i = 0; i < x; i++)
{

Console.WriteLine("Element {i + 1} is {a[i]}");
}

// BinarySearch the values of the Array.


int x3 = Array.BinarySearch(a, (Object)x2);
Console.WriteLine("--------------------------------------------------");
Console.WriteLine("Binary Search: " + x3);
Console.WriteLine("Element {x3} is {a[x3]}");
Console.WriteLine("--------------------------------------------------");

// Reverse the values of the Array.


Array.Reverse(a);
Console.WriteLine("-----------Reversed-------------------------------");
Console.WriteLine("----------------------------------------------");
for (int i = 0; i < x; i++)
{

Console.WriteLine("Element {i + 1} is {a[i]}");
}

Console.ReadLine();
}

}
}

You might also like