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

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 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...
1. Create a console program in C#, * Create a class: "Student.cs" * Add 3 variables:...
1. Create a console program in C#, * Create a class: "Student.cs" * Add 3 variables: StudentName (string), SchoolYear (int), YearsUntilGraduation(int) * Method YTK() = 12 - SchoolYear; 2. Main *Enter name *Enter age *You will attend school:____ years before graduating.
The NumberFormat Class 1.Write a Java statement that shows to the console the monetary value formatting...
The NumberFormat Class 1.Write a Java statement that shows to the console the monetary value formatting of the double variable called 'price'. 2.Write a Java statement that shows to the console the percentage formatting of the double variable 'tax'. 3.If you applied the monetary value formatting to the value 18, the result will be: 4.If you applied the percentage formatting to the double 0.068, the result will be: The DecimalFormat Class. 1.Write a Java statement that creates a formatting object...
Write a method in c# to find the summation of which two neighboring digits is equal...
Write a method in c# to find the summation of which two neighboring digits is equal to 8 Ex if N =0176623509808 Output is: 17 ,62 ,35 ,80 , 08
Write a C# console program that fills the right to left diagonal of a square matrix...
Write a C# console program that fills the right to left diagonal of a square matrix with zeros, the lower-right triangle with -1s, and the upper left triangle with +1s. Let the user enter the size of the matrix up to size 21. Use a constant and error check. Output the formatted square matrix with appropriate values. Refer to the sample output below. Sample Run: *** Start of Matrix *** Enter the size of square (<= 21): 5 1 1...
Write a C++ console program that prompts a user to enter information for the college courses...
Write a C++ console program that prompts a user to enter information for the college courses you have completed, planned, or are in progress, and outputs it to a nicely-formatted table. Name the CPP as you wish (only use characters, underscores and number in your file name. DO NOT use blank). Its output should look something like this: Course Year Units Grade ---------- ---- ----- ----- comsc-110 2015 4 A comsc-165 2016 4 ? comsc-200 2016 4 ? comsc-155h 2014...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT