C#: Create a new array containing the middle elements from the two given arrays of integers, each length 5
C# Sharp Basic Algorithm: Exercise-95 with Solution
Middle Elements from Two Arrays
Write a C# Sharp program to create an array containing the middle elements from the two given arrays of integers, each length 5.
Visual Presentation:
Sample Solution:-
C# Sharp Code:
using System; // Importing the System namespace
namespace exercises // Defining a namespace called 'exercises'
{
class Program // Defining a class named 'Program'
{
static void Main(string[] args) // The entry point of the program
{
// Calling the 'test' method with two integer arrays as arguments and assigning the result to the 'item' array
int[] item = test(new[] { 10, 20, -30, -40, 30 }, new[] { 10, 20, 30, 40, 30 });
// Outputting text to indicate the start of displaying the new array
Console.Write("New array: ");
// Looping through each element of the 'item' array and displaying them
foreach (var i in item)
{
Console.Write(i.ToString() + " "); // Displaying each element of the 'item' array separated by a space
}
}
// Method to create a new array containing the elements at index 2 from two different arrays
public static int[] test(int[] nums1, int[] nums2)
{
// Returning a new array with elements at index 2 from 'nums1' and 'nums2'
return new int[] { nums1[2], nums2[2] };
}
}
}
Sample Output:
New array: -30 30
Flowchart:
C# Sharp Code Editor:
Improve this sample solution and post your code through Disqus
Previous: Write a C# Sharp program to find out the maximum element between the first or last element in a given array of integers ( length 4), replace all elements with maximum element.
Next: Write a C# Sharp program to create a new array taking the first and last elements of a given array of integers and length 1 or more.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/csharp-exercises/basic-algo/csharp-basic-algorithm-exercises-95.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics