Question

In: Computer Science

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));

Solutions

Expert Solution

Program1:

//Finding the highest and lowest number from an array with given size 3

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MethodsDemo
{
class Program
{
public static int highest(int num)
{
int[] arr = new int[num];
  
Console.WriteLine("Enter 3 numbers: ");
for (int i = 0; i <= 2; i++)
{
arr[i]=Convert.ToInt32(Console.ReadLine());
}
int high = arr[0];
for (int i=0;i<=2;i++)
{
if (high < arr[i])
{
high = arr[i];
}
}
return high;
}
public static int lowest(int num)
{
int[] arr = new int[num];
  
Console.WriteLine("\nEnter 3 numbers: ");
for (int i = 0; i <= 2; i++)
{
arr[i] = Convert.ToInt32(Console.ReadLine());
}
int low = arr[0];
for (int i = 0; i <= 2; i++)
{
if (low > arr[i])
{
low = arr[i];
}
}
return low;
}

static void Main(string[] args)
{
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));
Console.ReadKey();
}
}
}

Program2:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MethodsDemo
{
class Program
{
public static int highest(int high)
{
return high;
}
public static int lowest(int low)
{
return low;
}

static void Main(string[] args)
{
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));
Console.ReadKey();
}
}
}

Note:

In the given question you didn't mention what we have to do so i am going with 2 programs with given contents.

I hope that will be helpful.


Related Solutions

Using the ListNode file. Write methods called min and max that return the smallest and largest...
Using the ListNode file. Write methods called min and max that return the smallest and largest values in the linked list. These methods will be added to your ListNode class. For example if a variable called list stores {11, -7, 3, 42, 0, 14], the call of list.min() should return -7 and the call of list.max() should return 42. If the list is empty, return -1. Print the returned value. Write a method called insertNode that inserts a new node...
Using the ListNode file. Write methods called min and max that return the smallest and largest...
Using the ListNode file. Write methods called min and max that return the smallest and largest values in the linked list. These methods will be added to your ListNode class. For example if a variable called list stores {11, -7, 3, 42, 0, 14], the call of list.min() should return -7 and the call of list.max() should return 42. If the list is empty, return -1. Print the returned value. Write a method called insertNode that inserts a new node...
Write a C program/code that prompts the user for a minimum min and a maximum max....
Write a C program/code that prompts the user for a minimum min and a maximum max. Then use these values to print the squares of all even numbers between the min and max variables. For example if the user enters 6 as the minimum and 200 as the maximum, the program/code should print the following. Enter limit on minimum square: 6 Enter limit on maximum square: 200 36 64 100 144 196
c++ Write a program that print stars, Max and Min values. It should use the following...
c++ Write a program that print stars, Max and Min values. It should use the following functions: (2 pts) int getNum ( ) should ask the user for a number and return the number. This function should be called by main once for each number to be entered. Input Validation: Do not accept numbers less than -100. (2 pts) void printStars ( int n ) should print n number of stars. If n is less than 0, display "Invalid" message...
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].
Exercise 9.2 (c++) (max, min, average, and median code) Write a program that asks users to...
Exercise 9.2 (c++) (max, min, average, and median code) Write a program that asks users to input up to 20 integers, and then finds the maximum, minimum, average, and median of the numbers that were entered. Use the following information to write your program. The median is the number that appears in the middle of the sorted list of numbers. If the array has an odd number of elements, median is a single number in the middle of the list...
I need it in java. Write a program that will print if n numbers that the...
I need it in java. Write a program that will print if n numbers that the user will input are or not within a range of numbers. For that, your program needs to ask first for an integer number called N that will represent the number of times that will ask for other integer numbers. Right after, it should ask for two numbers that will represent the Min and Max for a range. Lastly. it will iterate N number times...
Write a program/code that prompts the user for a minimum min and a maximum max. Then...
Write a program/code that prompts the user for a minimum min and a maximum max. Then use these values to print the squares of all even numbers between the min and max variables. (WRITTEN IN C) For example if the user enters 6 as the minimum and 200 as the maximum, the program/code should print the following. Enter limit on minimum square: 6 Enter limit on maximum square: 200 36 64 100 144 196
In MIPS assembly language, write a function that will display the max and min value in...
In MIPS assembly language, write a function that will display the max and min value in an array. Then write a function to calculate and display the average of all values in an array. This must be done in MIPS assembly language.
Java Special Methods: A) Write a method that finds the maximum of two numbers. You should...
Java Special Methods: A) Write a method that finds the maximum of two numbers. You should not use if-else or any other comparison operator. B) Write methods to implement the multiply, subtract and divide operations for integers. The results of all of these are integers. Use only the add operator.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT