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...
Create a console app with C# that uses a while loop to calculate the average of...
Create a console app with C# that uses a while loop to calculate the average of 3 test scores. Input integers, but use the appropriate average data type. Generally, you use a while to do something while a condition is true. You can use a while loop to execute a certain number of times. *While* this is not the best use of the while loop and another loop is generally more appropriate, it follows the pattern below set a counter...
Create a console app c#. Using a List, ask the user to enter all of their...
Create a console app c#. Using a List, ask the user to enter all of their favorite things. Once they are done, randomly pick a value from the list and display it.
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.
In C# using a Console App, create an array that stores 20 integer values. Use the...
In C# using a Console App, create an array that stores 20 integer values. Use the random number generator to select an integer value between 0 and 10. Store the 20 random integers in the array. Once the array has been created: Print the values in the array. Print the values in the array in reverse order. Sort and print the values in the array in ascending order. Sort and print the values in the array in descending order. Count...
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));
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT