Question

Create in C# Write a method called MaximumDiffrence that accepts an integer array as a parameter...

Create in C#

Write a method called MaximumDiffrence that accepts an integer array as a parameter and return the maximum difference between adjacent values in the array, where the gap is defined as the absolute value of the difference between the 2 adjacent values.

Example: if the array contains {5, 7, 4, 9, 6, 12, 8} so

The first gap is (5,7)=-2 and the absolute value is 2

The second gap is (7,4)=3

Third gap is (4,9) = -5 its absolute value is 5

Fourth gap is (9,6) = 3

Fifth gap is (6,12) = 6

Sixth gap is (12, 8) = 4

The method should return 6, the max absolute difference If the array has just 2 or less elements, then it should return 0

0 0
Add a comment Improve this question Transcribed image text
Answer #1

using System.IO;
using System;

class Program
{
public static int maxDiff(int[] Array, int arraySize)
{
int maxDiff = Array[1] - Array[0];
int i, j, diff;
if( arraySize<=2) // checking if elements are equal to or less than 2.
{
return 0;
}
for (i = 0; i < arraySize; i++)
{
j=i+1;
diff = Math.Abs(Array[j] - Array[i]);
if (diff > maxDiff)
maxDiff = diff;
if (i!=arraySize-1) {
Console.Write(" {0} gap difference ({1},{2}): {3}", i+1, Array[i], Array[j], diff);
}
}
Console.Write(" ");
  
return maxDiff;
}
  
static void Main()
{
int[] Array = new int[100];
int n,i;
Console.WriteLine("Enter the number of elements: ");
n = Convert.ToInt32(Console.ReadLine());
for(i=0; i<n; i++)
{
   Console.Write("Element {0} : ",i+1);
   Array[i] = Convert.ToInt32(Console.ReadLine());        
}
  
Console.Write(" Elements in array are: ");
Console.Write(" ");
for(i=0; i<n; i++)
{
Console.Write("{0} ", Array[i]);
}
  
Console.Write(" Maximum difference is " +
maxDiff(Array, n));
  
}
}

Output

1. Elements less than or equal to 2

2. More than 2 elements

Add a comment
Know the answer?
Add Answer to:
Create in C# Write a method called MaximumDiffrence that accepts an integer array as a parameter...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a C# program using WINDOWS FORM App in Visual Studio. THE ANSWER MUST BE IN...

    Create a C# program using WINDOWS FORM App in Visual Studio. THE ANSWER MUST BE IN C#, IN WINDOW FORM APP, IN VISUAL STUDIOS. Write the code to: Write a method called MaximumDiffrence that accepts an integer array as a parameter and return the maximum difference between adjacent values in the array, where the gap is defined as the absolute value of the difference between the 2 adjacent values. Example: if the array contains {5, 7, 4, 9, 6, 12,...

  • Create 3 user-defined methods called add(). 1 accepts a single integer array parameter; this method should...

    Create 3 user-defined methods called add(). 1 accepts a single integer array parameter; this method should loop the array adding the values and return the summation (use 4, 1, 4, 6, 10, 15, 32, 79, 18 as the values in the array) 1 accepts 2 integer parameters; this method should add them and return the value 1 accepts 3 integer parameters; this method should add them all and return the value In main, define the parameters above (you may use...

  • Method createNewArray that accepts one integer parameter for sizing the integer array the method will return....

    Method createNewArray that accepts one integer parameter for sizing the integer array the method will return. The method will create a new integer array of the size specified by the parameter, fill it with random values from 1 thru 100 (inclusive) and return the new array.

  • Write a method named printReverse() that accepts an int array as its parameter and prints that...

    Write a method named printReverse() that accepts an int array as its parameter and prints that array in reverse order. This method should not have a return statement (void). - All elements should be printed on the same line, separated by a space - A new line should be printed after the entire array prints - If the array passed in held the values: {3,2,1} - Output: 1 2 3 //newline printed here

  • Write a C++ program that does the following : Accepts a positive integer ( n )...

    Write a C++ program that does the following : Accepts a positive integer ( n ) from the keyboard . Create an character array of size n. Using a random number generator, populate the array with characters between 33 – 126. Create 7 individual functions and perform the following 1. In the first function: display elements of the array. Display the first 20 elements If the size is > 20 2. In the second function : Using recursion, Search for...

  • In Java Write a method factorial that accepts an integer parameter n and that uses recursion...

    In Java Write a method factorial that accepts an integer parameter n and that uses recursion to compute and return the value of n factorial (also known as n!). Your method should throw an IllegalArgumentException if n is negative. Several calls and their return values are shown below. Call Output factorial(0); 1 factorial(1); 1 factorial(3); 6 factorial(5); 120 factorial(10); 3628800 factorial(-4); IllegalArgumentException

  • java /* Q2 (10 pts): Write a method called method that accepts an integer parameter *...

    java /* Q2 (10 pts): Write a method called method that accepts an integer parameter * * * * and returns a sum of the first n terms of the sequence. * In other words, the method should generate the following sequence: 1 + 1/2 + 1/3 + 1/4 + ... 1/n * For example, method2(2) will return 1.5 since 1+1/2 = 1.5 * method2 (15) will return 3.3182289932289937 * You may assume that the parameter n is nonnegative. */...

  • ****WRITE A JAVA PROGRAM THAT : Write a method named stretch that accepts an array of...

    ****WRITE A JAVA PROGRAM THAT : Write a method named stretch that accepts an array of integers (that the user inputs) as a parameter and returns a new array twice as large as the original, replacing every integer from the original array with a pair of integers, each half the original. If a number in the original array is odd, then the first number in the new pair should be one higher than the second so that the sum equals...

  • In java please: Write a method that accepts as a parameter a queue of integers that...

    In java please: Write a method that accepts as a parameter a queue of integers that are already sorted by absolute value, and modifies it so that the integers are sorted normally. Only use a single stack as auxiliary storage. For example, if a queue variable named q stores the following elements: front {1, -2, 4, 5, -7, -9, -12, 28, -34} back Then the call of reorder(q); should modify it to store the following values: front {-34, -12, -9,...

  • 1. Write a static method named mode that takes an array of integers as a parameter...

    1. Write a static method named mode that takes an array of integers as a parameter and that returns the value that occurs most frequently in the array. Assume that the integers in the array appear in sorted order. For example, if a variable called list stores the following values: ist -3, 1, 4, 4, 4, 6, 7,8, 8, 8, 8, 9, 11, 11, 11, 12, 14, int 141i Then the call of mode (li array, appearing four times. st,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT