Question

In: Computer Science

Find MIN. Write a C# console app consisting of class MIN with the method CalculateMin() and...

Find MIN. Write a C# console app consisting of class MIN with the method CalculateMin() and the driver class. Print the MIN and MIN's location. Don't count numbers outside the range [0, 100].

Solutions

Expert Solution

// C# program to create a class MIN with method CalculateMin that returns the index of the minimum element in the input array

using System;

class MIN {
  
   // method that determines and return the index of the minimum element in the input array, arr, if found else returns -1
   // It only considers the numbers in the range [0, 100]
public static int CalculateMin(double[] arr)
{
int minIdx = -1; // set minIdx to -1 i.e no minimum element
       // loop over the array
for(int i=0;i<arr.Length;i++)
{
           // validate ith number is in valid range
if(arr[i] >= 0 && arr[i] <= 100)
// check that if minIdx is not set or ith element < minIdx element, update minIdx to i
               if(minIdx == -1 || arr[i] < arr[minIdx])
minIdx = i;
}
  
// return the index of the minimum element
       return minIdx;
}
}

class MINDriver
{
static void Main() {
  
       // create an array of numbers
double[] arr = {67, 50, 70.5, 12, -3, 102, 45, 16, 12, 66, 23};
  
       // get the index of the minimum element in the above arr
       int idx = MIN.CalculateMin(arr);
  
       // minimum element exists, display the minimum element and its index
if(idx != -1)
Console.WriteLine("Minimum element is "+arr[idx]+" at index: "+idx);
else // no minimum element exists in the valid range
Console.WriteLine("Array does not contain any valid elements");
}
}

//end of program

Output:


Related Solutions

IN C# lang Write a console app (review Module 5 for console app) that stores 3...
IN C# lang Write a console app (review Module 5 for console app) that stores 3 students test stores in 3 separate arrays as integers. Some did not not take all the tests, so they each have a their own length and we use separate arrays. Declare and initialize each array. Use meaningful names. Print each array as follows: Morgan 98, 72, 65 Bowie 15, 47, 35, 92, 94 Ananya 91, 68, 87, 75 Extra credit: Compute the average for...
write a c# console application app that reads all the services in the task manager and...
write a c# console application app that reads all the services in the task manager and automatically saves what was read in a Notepad txt file.  Please make sure that this program runs, also add some comments
Needs to be written in C# Console App! Create a base class BankAccount. Decide what characteristics...
Needs to be written in C# Console App! Create a base class BankAccount. Decide what characteristics are common for checking and saving accounts and include these characteristics in the base class. Define derived classes for checking and savings. In your design , do not allow the banking base account to be instantiated --only the checking and saving classes. Use a Program class to test your design.
write C# console app No arrays Credit card validation Problem. A credit card number must be...
write C# console app No arrays Credit card validation Problem. A credit card number must be between 13 and 16 digits. It must start with: –4 for visa cards –5 for mater cards –37 for American express cards –6 for discover cards •Consider a credit card number 4380556218442727 •Step1: double every second digit from right to left. If doubling of a digit results in a two-digit number, add up the two digits to get a single-digit number. •Step2: now add...
Write a C++ PROGRAM: Add the function min as an abstract function to the class arrayListType...
Write a C++ PROGRAM: Add the function min as an abstract function to the class arrayListType to return the smallest element of the list. Also, write the definition of the function min in the class unorderedArrayListType and write a program to test this function.
Shopping Cart App (C#) Please create a C# Console (.NET Framework) application which reads from the...
Shopping Cart App (C#) Please create a C# Console (.NET Framework) application which reads from the user, the price of items in their shopping "cart". Be sure to tell the user how to stop (or ask them if they want to stop) entering prices. Items can be priced any positive number greater than zero. When the user has entered the prices for all items in their "cart", calculate the subtotal (the sum of all item prices). Next, check if the...
Method calling in c# I need to write methods for calling the min and max numbers...
Method calling in c# I need to write methods for calling the min and max numbers using this code; Console.WriteLine("Calling highest method."); Console.WriteLine("Highest number is: {0}", highest(3)); Console.WriteLine("Calling lowest method."); Console.WriteLine("Lowest number is: {0}", lowest(3));
C# (Tic-Tac-Toe) Create class TicTacToe that will enable you to write a complete app to play...
C# (Tic-Tac-Toe) Create class TicTacToe that will enable you to write a complete app to play the game of Tic-Tac-Toe. The class contains a private 3-by-3 rectangular array of integers. The constructor should initialize the empty board to all 0s. Allow two human players. Wherever the first player moves, place a 1 in the specified square, and place a 2 wherever the second player moves. Each move must be to an empty square. After each move, determine whether the game...
C# Programming Language Write a C# program ( Console or GUI ) that prompts the user...
C# Programming Language Write a C# program ( Console or GUI ) that prompts the user to enter the three examinations ( test 1, test 2, and test 3), homework, and final project grades then calculate and display the overall grade along with a message, using the selection structure (if/else). The message is based on the following criteria: “Excellent” if the overall grade is 90 or more. “Good” if the overall grade is between 80 and 90 ( not including...
The split method in the String class returns an array of strings consisting of the substrings...
The split method in the String class returns an array of strings consisting of the substrings split by the delimiters. However, the delimiters are not returned. Implement the following new method that returns an array of strings consisting of the substrings split by the matching delimiters, including the matching delimiters. public static String[] split(String s, String regex) For example, split("ab#12#453", "#") returns ab, #, 12, #, 453 in an array of String, and split("a?b?gf#e", "[?#]") returns a, ?, b, ?,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT