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.
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...
Write the code in Java: 1. Create a method that displays your name in the console....
Write the code in Java: 1. Create a method that displays your name in the console. This method is void and takes no parameters. Make an app that runs the method in response to a button press. 2. Create a version of the method in #1 that takes the text (String) to be displayed as a parameter. Allow the user to enter the text in a dialog box or text field and display that text in the console. Be sure...
Write a recursive method that displays an integer value reversely on the console using the following...
Write a recursive method that displays an integer value reversely on the console using the following header: public static void reverseDisplay(int value) For example, reverseDisplay(12345) displays 54321. Write a test program that prompts the user to enter an integer, invokes the method above, and displays its reversal.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT