I need help please for class.
This week, you need to create a console-based application whose Main() method accepts ten integer values from the user and stores them in an array. Next, create a method that determines and displays the smallest and largest of the ten values. Then, pass the array to the method.
Next, create a GUI application whose button's Click() method accepts ten integer values from a TextBox and stores them in an array that is declared above the Click() method. After ten entries have been made, call a method that determines and displays the smallest and largest of the ten values.
What programming language are you using? for C++ int main(){ int number[10]; //an array of 10 int for(int x=0; x<10;x++){ cin >> number[x]; } // do some comparison something like // if number[0] > number[1] then store it in a temp variable. } This should get you started.
// C# language using System; using System.Linq; namespace SmallAndLarge.ConsoleApplication { class Program { private const int NumbersOfElements = 10; // Delimiter char should be any symbol except of a digit, because we expect to receive integer values private const char DelimiterChar = ' '; private const string DelimiterCharDescription = "space"; static void Main() { // Single-dimensional array var intArray = new int[NumbersOfElements]; Console.WriteLine( "Please input {0} integer values:{1}{1}Note: you can use '{2}' ({3}) symbol like delimiter between integer values or input integer values one by one.{1}", NumbersOfElements, Environment.NewLine, DelimiterChar, DelimiterCharDescription); // Index of element in array int currentElementIndex = 0; while (NumbersOfElements > currentElementIndex) { // Get string value from user string inputValues = Console.ReadLine(); // If user does not input anything if (string.IsNullOrWhiteSpace(inputValues)) { Console.WriteLine("Sorry, input please at least one integer value."); } else { // Trim delimiter chars inputValues = inputValues.Trim(DelimiterChar); // If input string contains a few integer values if (inputValues.IndexOf(DelimiterChar) >= 0) { string[] splittedValues = inputValues.Split(DelimiterChar); for (int counter = 0; counter < splittedValues.Length; counter++) { // Trim delimiter chars string value = splittedValues[counter].Trim(DelimiterChar); // If input string contains a few delimiter symbols if (string.IsNullOrWhiteSpace(value)) { continue; } if (StoreValueInArray(intArray, currentElementIndex, value)) { currentElementIndex++; // Check If array stores all required elements if (NumbersOfElements <= currentElementIndex) { // Check if quantity of input values are more than required number if (counter + 1 < splittedValues.Length) { Console.WriteLine( "Note: Quantity of input values are more than required number. Last stored value in array: '{0}'.", intArray[intArray.Length - 1]); } break; } } else if(counter + 1 < splittedValues.Length) { Console.WriteLine("Note: Input values (after last failed one) will not be stored in array."); break; } } } else if (StoreValueInArray(intArray, currentElementIndex, inputValues)) { currentElementIndex++; } } // Check If array stores all required elements if (NumbersOfElements <= currentElementIndex) { // 2 new line symbols before displaying of results Console.WriteLine("{0}{0}", Environment.NewLine); break; } Console.WriteLine("{1}Please input {0} integer value(s):{1}", NumbersOfElements - currentElementIndex, Environment.NewLine); } DisplaySmallAndLargeValues(intArray); // Alternate way to find minimal and maximal values in array using LINQ //DisplaySmallAndLargeValuesLinq(intArray); Console.ReadLine(); } private static bool StoreValueInArray(int[] array, int index, string value) { int elementValue; // Try to parse string to integer if (!int.TryParse(value, out elementValue)) { Console.WriteLine("Input '{0}' value is not a integer.", value); return false; } // Store integer value in array array[index] = elementValue; return true; } private static void DisplaySmallAndLargeValues(int[] array) { Array.Sort(array); Console.WriteLine("Minimal integer value: '{0}', maximal integer value in array: '{1}'.", array[0], array[array.Length - 1]); } private static void DisplaySmallAndLargeValuesLinq(int[] array) { Console.WriteLine("Minimal integer value: '{0}', maximal integer value in array: '{1}'.", array.Min(), array.Max()); } } }
Join our real-time social learning platform and learn together with your friends!