//Method: Main //Purpose: Initialize and input array of words to be sorted as per inputs of user //Modified by: John Sniffen //Restrictions: None static void Main(string[] args) { // declare the unsorted and sorted arrays string[] aUnsorted; string[] aSorted; // declare the delegate variable which will point to the function to be called sortingFunction findHiLow; // a label to allow us to easily loop back to the start if there are input issues start: Console.WriteLine("Enter a list of space-separated words"); // read the space-separated string of words string sWords = Console.ReadLine(); // split the string into the an array of strings which are the individual words string[] sWordArray = sWords.Split(' '); // initialize the size of the unsorted array to 0 int nUnsortedLength = 0; // a double used for parsing the current array element string nThisWord; // iterate through the array of strings foreach (string sThisWord in sWordArray) { // if the length of this string is 0 (ie. they typed 2 spaces in a row) if (sThisWord.Length == 0) { // skip it continue; } nUnsortedLength = sWordArray.Length; } // now we know how many unsorted words there are // allocate the size of the unsorted array aUnsorted = new string[nUnsortedLength]; // reset nUnsortedLength back to 0 to use as the index to store the wordss in the unsorted array nUnsortedLength = 0; foreach (string sThisWord in sWordArray) { // still skip the blank strings if (sThisWord.Length == 0) { continue; } // store the value into the array aUnsorted[nUnsortedLength] = sThisWord; // increment the array index nUnsortedLength++; } // allocate the size of the sorted array aSorted = new string[nUnsortedLength]; // prompt for <a>scending or <d>escending Console.Write("Ascending or Descending? "); string sDirection = Console.ReadLine(); if (sDirection.ToLower().StartsWith("a")) { findHiLow = new sortingFunction(FindLowestValue); } else { findHiLow = new sortingFunction(FindHighestValue); } // start the sorted length at 0 to use as sorted index element int nSortedLength = 0; // while there are unsorted values to sort while (aUnsorted.Length > 0) { // store the lowest or highest unsorted value as the next sorted value aSorted[nSortedLength] = findHiLow(aUnsorted); // remove the current sorted value RemoveUnsortedValue(aSorted[nSortedLength], ref aUnsorted); // increment the number of values in the sorted array ++nSortedLength; } // write the sorted array of numbers Console.WriteLine("The sorted list is: "); foreach (string thisWord in aSorted) { Console.Write($"{thisWord} "); } Console.WriteLine(); }
// Method: Main // Purpose: Sort the words in a sentence in either ascending // or descending order depending on the user input // Restrictions: None static void Main(string[] args) { // declare the unsorted and sorted arrays string[] aUnsorted; string[] aSorted; // declare the delegate variable which will point to the function to be called sortingFunction findHiLow; //label to easily loop back to start if there are input issues start: Console.WriteLine("Enter a list of space-seperated words"); //read the string of words string sWords = Console.ReadLine(); //split the string of words string[] sWord = sWords.Split(' '); //initialize the size of unsorted array to 0 int nUnsortedLength = 0; //a string to hold current element in array string nThisWord = null; //iterate through the array of words foreach (string sThisWord in sWord) { //if length is 0 the string is null (typed 2 spaces) if (sThisWord.Length == 0) { //skip it continue; } try { nThisWord = Convert.ToString(sThisWord); ++nUnsortedLength; } catch { //if an error occurs, indicate it is invalid Console.WriteLine($"Word #{nUnsortedLength + 1} is invalid"); goto start; } } //allocate the size of the unsorted array aUnsorted = new string[nUnsortedLength]; //reset unsorted length back to 0 as index to store words in array nUnsortedLength = 0; foreach (string sThisWord in sWord) { //skip blank strings if (sThisWord.Length == 0) { continue; } //assign current word val to n this word nThisWord = sThisWord; //store the value into the array aUnsorted[nUnsortedLength] = nThisWord; //increment array nUnsortedLength++; } //allocate size to sorted array aSorted = new string[nUnsortedLength]; // prompt for <a>scending or <d>escending Console.Write("Ascending or Descending? "); string sDirection = Console.ReadLine(); if (sDirection.ToLower().StartsWith("a")) { findHiLow = new sortingFunction(FindLowestVal); } else { findHiLow = new sortingFunction(FindHighestVal); } //start sorted length at 0 int nSortedLength = 0; //while there are unsorted vals to sort while (aUnsorted.Length > 0) { //store lowest or highest val as next val aSorted[nSortedLength] = findHiLow(aUnsorted); //remove current sorted value RemoveUnsortedVal(aSorted[nSortedLength], ref aUnsorted); //increment num of vals in sorted array ++nSortedLength; } //write the sorted arrray of words Console.WriteLine("The sorted list is: "); foreach (string thisWord in aSorted) { Console.Write($"{thisWord} " ); } Console.WriteLine(); }//end main
static void Main(string[] args) { // declare the unsorted and sorted arrays double[] aUnsorted; double[] aSorted; // declare the delegate variable which will point to the function to be called sortingFunction findHiLow; // a label to allow us to easily loop back to the start if there are input issues start: Console.WriteLine("Enter a list of space-separated numbers"); // read the space-separated string of numbers string sNumbers = Console.ReadLine(); // split the string into the an array of strings which are the individual numbers string[] sNumber = sNumbers.Split(' '); // initialize the size of the unsorted array to 0 int nUnsortedLength = 0; // a double used for parsing the current array element double nThisNumber; // iterate through the array of number strings foreach (string sThisNumber in sNumber) { // if the length of this string is 0 (ie. they typed 2 spaces in a row) if (sThisNumber.Length == 0) { // skip it continue; } try { // try to parse the current string into a double nThisNumber = double.Parse(sThisNumber); // if it's successful, increment the number of unsorted numbers ++nUnsortedLength; } catch { // if an exception occurs // indicate which number is invalid Console.WriteLine($"Number #{nUnsortedLength + 1} is not a valid number."); // loop back to the start goto start; } } // now we know how many unsorted numbers there are // allocate the size of the unsorted array aUnsorted = new double[nUnsortedLength]; // reset nUnsortedLength back to 0 to use as the index to store the numbers in the unsorted array nUnsortedLength = 0; foreach (string sThisNumber in sNumber) { // still skip the blank strings if (sThisNumber.Length == 0) { continue; } // parse it into a double (we know they are all valid now) nThisNumber = double.Parse(sThisNumber); // store the value into the array aUnsorted[nUnsortedLength] = nThisNumber; // increment the array index nUnsortedLength++; } // allocate the size of the sorted array aSorted = new double[nUnsortedLength]; // prompt for <a>scending or <d>escending Console.Write("Ascending or Descending? "); string sDirection = Console.ReadLine(); if (sDirection.ToLower().StartsWith("a")) { findHiLow = new sortingFunction(FindLowestValue); } else { findHiLow = new sortingFunction(FindHighestValue); } // start the sorted length at 0 to use as sorted index element int nSortedLength = 0; // while there are unsorted values to sort while (aUnsorted.Length > 0) { // store the lowest or highest unsorted value as the next sorted value aSorted[nSortedLength] = findHiLow(aUnsorted); // remove the current sorted value RemoveUnsortedValue(aSorted[nSortedLength], ref aUnsorted); // increment the number of values in the sorted array ++nSortedLength; } // write the sorted array of numbers Console.WriteLine("The sorted list is: "); foreach (double thisNum in aSorted) { Console.Write($"{thisNum} "); } Console.WriteLine(); }
static void Main(string[] args) { Console.WriteLine("please enter a sentence."); string input = Console.ReadLine(); string[] unSorted; string[] sorted; int unSortedLength = 0; int sortedLength = 0; sortingFunction findHiLow; string[] splitUp = input.Split(' '); foreach (string sThisString in splitUp) { if (sThisString.Length == 0) { continue; } unSortedLength++; } unSorted = new string[unSortedLength]; unSortedLength = 0; foreach (string sThisString in splitUp) { if (sThisString.Length == 0) { continue; } unSorted[unSortedLength] = sThisString; unSortedLength++; } sorted = new string[unSortedLength]; Console.Write("Ascending or Descending? "); string sDirection = Console.ReadLine(); if (sDirection.ToLower().StartsWith("a")) { findHiLow = new sortingFunction(FindLowestValue); } else { findHiLow = new sortingFunction(FindHighestValue); } sortedLength = 0; while (unSorted.Length > 0) { sorted[sortedLength] = findHiLow(unSorted); RemoveUnsortedValue(sorted[sortedLength], ref unSorted); sortedLength++; } Console.WriteLine("The sorted list is: "); foreach (string thisNum in sorted) { Console.Write($"{thisNum} "); } Console.WriteLine(); }