Need Help with C# I need help completing an assignment 1.Create a 2d array of 5
ID: 3666043 • Letter: N
Question
Need Help with C#
I need help completing an assignment
1.Create a 2d array of 5 rows and 6 columns
2.Write a method , that would work with any 2d array, to fill it with random ints [1,15]
3.Write a method, that would work with any 2d array, that finds the largest value and returns it for the client to print.
4.Print the sum of the 3 largest values.
5.Write a method, that would work with any 2d array, that returns the sum of all the values for the client to print
6.Write a method, that would work with any 2d array, that returns the number of times the value 6 occurs in the 2d array
7.Print the 2d array
8.Create a new 2D array with 7 rows and 6 columns
9.Reuse the method from step 2 to fill it with values [-100,100]
10.Write a method that returns for printing in main, the sum of all the negative, even values.
11.Print the 2d array
class Program
{
public static void Main(string[] args)
{
int[,] myArray = new int[5, 6];
FillArray(myArray);
LargestValue(myArray);
int[,] myArray2 = new int[7, 6];
}
//Fills the array with random values 1-15
public static void FillArray(int[,] array)
{
Random rnd = new Random();
int[,] Array = new int[,] { };
for (int i = 0; i < Array.GetLength(0); i++)
{
for (int j = 0; j < Array.GetLength(1); j++)
{
Array[i, j] = rnd.Next(1, 16);
}
}
}
//finds the largest value in the array
public static void LargestValue(int[,] array)
{
FillArray(array);
IEnumerable<int> allValues = array.Cast<int>();
int max = allValues.Max();
Console.WriteLine("Max value: " + max);
}
//finds the sum of all values
public int SumArray(int[,] array)
{
int total = 0;
// Iterate through the first dimension of the array
for (int i = 0; i < array.GetLength(0); i++)
{
// Iterate through the second dimension
for (int j = 0; j < array.GetLength(1); j++)
{
// Add the value at this location to the total
// (+= is shorthand for saying total = total + <something>)
total += array[i, j];
}
}
return total;
}
}
Explanation / Answer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XMLTesting;
namespace XMLTest
{
class Program
{
static void Main(string[] args)
{
int[,] myArray = new int[5, 6];
FillArray(myArray,1,15);
int sumLargestThree = LargestThreeSum(myArray);
Console.WriteLine("Sum of three largest in array is " + sumLargestThree);
PrintArray(myArray);
int[,] myArray2 = new int[7, 6];
FillArray(myArray2,-100,100);
PrintArray(myArray2);
}
//Fills the array with random values rangeStart and rangeEnd
public static void FillArray(int[,] array,int rangeStart,int rangeEnd)
{
Random rnd = new Random();
for (int i = 0; i < array.GetLength(0); i++)
{
for (int j = 0; j < array.GetLength(1); j++)
{
array[i, j] = rnd.Next(rangeStart, rangeEnd);
}
}
}
//finds the largest value in the array
public static int LargestValue(int[,] array)
{
IEnumerable<int> allValues = array.Cast<int>();
int max = allValues.Max();
return max;
}
//finds the sum of all values
public static int SumArray(int[,] array)
{
int total = 0;
// Iterate through the first dimension of the array
for (int i = 0; i < array.GetLength(0); i++)
{
// Iterate through the second dimension
for (int j = 0; j < array.GetLength(1); j++)
{
// Add the value at this location to the total
// (+= is shorthand for saying total = total + <something>)
total += array[i, j];
}
}
return total;
}
//For finding the sum of largest three in the 2d array
public static int LargestThreeSum(int[,] array)
{
int sum = array.Cast<int>().OrderByDescending(i => i).Take(3).Sum();
return sum;
}
//returns the number of times six occurs in the array
public static int FrequencySix(int[,] array)
{
int count = 0;
for (int i = 0; i < array.GetLength(0); i++)
{
// Iterate through the second dimension
for (int j = 0; j < array.GetLength(1); j++)
{
if (array[i, j] == 6)
{
count++;
}
}
}
return count;
}
//return the sum of all -ve and even numbers in the array
public static int SumNegativeEven(int[,] array)
{
int sum = 0;
for (int i = 0; i < array.GetLength(0); i++)
{
// Iterate through the second dimension
for (int j = 0; j < array.GetLength(1); j++)
{
if (array[i, j]<0 && array[i,j]%2==0)
{
sum += array[i, j];
}
}
}
return sum;
}
//function for printing 2D array
public static void PrintArray(int[,] array)
{
for (int i = 0; i < array.GetLength(0); i++)
{
// Iterate through the second dimension
for (int j = 0; j < array.GetLength(1); j++)
{
Console.Write(array[i, j]);
Console.Write(" ");
}
Console.WriteLine();
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.