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 = [Link](array1,
element => [Link]("car", [Link]));
//
// Find first element of three characters length.
//
string value2 = [Link](array1,
element => [Link] == 3);
//
// Find all elements not greater than four letters long.
//
string[] array2 = [Link](array1,
element => [Link] <= 4);
[Link](value1);
[Link](value2);
[Link]([Link](",", array2));
[Link]();
}
}
Output
carrot
cat
cat,dog,bird
//////////////////////////////////////////////////////////////////////
C# program that uses [Link]
using System;
class Program
{
static void Main()
{
string[] array = { "dot", "net", "yes","perls" };
// Find last string of length 3.
string result = [Link](array, s => [Link] == 3);
[Link](result); }
}
Output
yes
C# program that uses [Link] method
using System;
class Program
{
static void Main()
{
int[] array = { 5, 6, 7, 6 };
// Use FindIndex method with predicate.
int index1 = [Link](array, item => item == 6);
// Use LastFindIndex method with predicate.
int index2 = [Link](array, item => item == 6);
// Write results.
[Link]("{0} = {1}", index1, array[index1]);
[Link]("{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.
[Link]("UNSORTED: " + new string(array));
// Sort the char array.
[Link]<char>(array);
[Link]("SORTED: " + new string(array));
}
}
Output
UNSORTED: zab
SORTED: abz
///////////////////////////////////////////////////////////
C# program that uses [Link]
using System;
class Program
{
static void Main()
{
string[] colors = new string[]
{
"orange",
"blue",
"yellow",
"aqua",
"red"
};
// Call [Link] method.
[Link](colors);
foreach (string color in colors)
{
[Link](color);
}
Output
aqua
blue
orange
red
yellow
C# program that sorts copy
using System;
using [Link];
class Program
{
static void Main()
{
string[] array = { "zebra", "parrot", "ant" };
List<string> copy = new List<string>(array);
[Link]();
[Link]([Link](",", array));
[Link]([Link](",", 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 = [Link](myArray, myString, 0, 6);
[Link]("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 = [Link](myArray, myString, 6, 7);
[Link]("The last occurrence of \"{myString}\" between index 0 and index 6 is at index
{myIndex}.");
[Link]();
}
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];
[Link]("Enter number of elements you want to hold in the array (max3)?");
string s = [Link]();
int x = [Link](s);
[Link]("--------------------------------------------------");
[Link]("\n Enter array elements \n");
[Link]("--------------------------------------------------");
for (int i = 0; i < x; i++)
{
string s1 = [Link]();
a[i] = [Link](s1);
}
[Link]("Enter Search element\n");
[Link]("--------------------------------------------------");
string s3 = [Link]();
int x2 = [Link](s3);
// Sort the values of the Array.
[Link](a);
[Link]("--------------Sorted-------------------------");
for (int i = 0; i < x; i++)
{
[Link]("Element {i + 1} is {a[i]}");
}
// BinarySearch the values of the Array.
int x3 = [Link](a, (Object)x2);
[Link]("--------------------------------------------------");
[Link]("Binary Search: " + x3);
[Link]("Element {x3} is {a[x3]}");
[Link]("--------------------------------------------------");
// Reverse the values of the Array.
[Link](a);
[Link]("-----------Reversed-------------------------------");
[Link]("----------------------------------------------");
for (int i = 0; i < x; i++)
{
[Link]("Element {i + 1} is {a[i]}");
/////////////////////////////////////////////////////////////////////////////////////////////
This all done by c# coding all of coding together ok for help
using System;
using [Link];
using [Link];
using [Link];
using [Link];
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 = [Link](array1,
element => [Link]("car", [Link]));
//
// Find first element of three characters length.
//
string value2 = [Link](array1,
element => [Link] == 3);
//
// Find all elements not greater than four letters long.
//
string[] array2 = [Link](array1,
element => [Link] <= 4);
[Link](value1);
[Link](value2);
[Link]([Link](",", array2));
//////////////////////////////////
//C# program that uses [Link]
{
string[] array = { "dot", "net", "yes", "perls" };
// Find last string of length 3.
string result = [Link](array, s => [Link] == 3);
[Link](result);
}
//////////////////////////////////////////////////////////////
// Use this input array.
{
int[] array = { 5, 6, 7, 6 };
// Use FindIndex method with predicate.
int index1 = [Link](array, item => item == 6);
// Use LastFindIndex method with predicate.
int index2 = [Link](array, item => item == 6);
// Write results.
[Link]("{0} = {1}", index1, array[index1]);
[Link]("{0} = {1}", index2, array[index2]);
}
///////////////////////////////////////////////////////////////
{
char[] array = { 'z', 'a', 'b' };
// Convert array to a string and print it.
[Link]("UNSORTED: " + new string(array));
// Sort the char array.
[Link]<char>(array);
[Link]("SORTED: " + new string(array));
}
////////////////////////////////////////////////////////////////
string[] colors = new string[]
{
"orange",
"blue",
"yellow",
"aqua",
"red"
};
// Call [Link] method.
[Link](colors);
foreach (string color in colors)
{
[Link](color);
}
///////////////////////////////////////////////////////////////////
{
string[] array = { "zebra", "parrot", "ant" };
List<string> copy = new List<string>(array);
[Link]();
[Link]([Link](",", array));
[Link]([Link](",", 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 = [Link](myArray, myString, 0, 6);
[Link]("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 = [Link](myArray, myString, 6, 7);
[Link]("The last occurrence of \"{myString}\" between index 0 and index 6 is at index
{myIndex}.");
///////////////////////////////////////////////////////////////////////////////
{
int[] a = new int[3];
[Link]("Enter number of elements you want to hold in the array (max3)?");
string s = [Link]();
int x = [Link](s);
[Link]("--------------------------------------------------");
[Link]("\n Enter array elements \n");
[Link]("--------------------------------------------------");
for (int i = 0; i < x; i++)
{
string s1 = [Link]();
a[i] = [Link](s1);
}
[Link]("Enter Search element\n");
[Link]("--------------------------------------------------");
string s3 = [Link]();
int x2 = [Link](s3);
// Sort the values of the Array.
[Link](a);
[Link]("--------------Sorted-------------------------");
for (int i = 0; i < x; i++)
{
[Link]("Element {i + 1} is {a[i]}");
}
// BinarySearch the values of the Array.
int x3 = [Link](a, (Object)x2);
[Link]("--------------------------------------------------");
[Link]("Binary Search: " + x3);
[Link]("Element {x3} is {a[x3]}");
[Link]("--------------------------------------------------");
// Reverse the values of the Array.
[Link](a);
[Link]("-----------Reversed-------------------------------");
[Link]("----------------------------------------------");
for (int i = 0; i < x; i++)
{
[Link]("Element {i + 1} is {a[i]}");
}
[Link]();
}
}
}