117k views
3 votes
Write an application that creates a two-dimensional array. Allow the user to input the size of the array (number of rows and number of columns). Fill the array with random numbers between 0 and 100. Search the array for the largest value. Display the array values, numbers aligned, and the indexes where the largest value is stored.

1 Answer

3 votes

Answer:

Step-by-step explanation:

The Program can be computed as follows:

/*********************************************************

*Program that creates a two-dimensional array and takes an*

*Input as a size of an array and generate random elements *

**********************************************************\

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace ConsoleApplication 1

{

class Program

{

static void Main(string[] args)

{

int maxi=0, maxj=0;

//Random for create a random numbers.

Random rnd = new Random();

Console.WriteLine ("Enter the number of +"rows... ");

//Take a row number as a input.

int rows=Convert .Tolnt32 (Console.ReadLine

());

Console .WriteLine ("Enter the number of +

"columns ...");

//Take a column number as a input.

int cols=Convert.Tolnt32 (Console .ReadLine

());

//Declaring a two-dimensional array.

int[,] randarray = new int[rows,cols ];

//Creating an array with the random numbers.

for (int i = 0; i < rows; i++)

{

for (int j = 0; j < cots; j++) {

//Random numbers.

randarray[i, j] = rnd.Next(0, 100);

}

}

//Assign first element of an array to the max

//Variable

int max = randarray[0,0];

//Display the elements in two-dimensional

//array

for (int i = 0; i < rows; i++)

{

for (int j = 0; j < cols; j++)

{

Console.Write('\\[" + i + "," +j + "]"

+"\t");

Console.Write(randarray[i, j]);

}

Console.WriteLine("\\");

}

//Checking the largest element in two-

//dimensional array.

for (int = 0; i < rows; i++)

{

for (int j 0; j < cols; j++)

if (randarray[i, j] > max)

{

max = randarray[i, j];

maxi = i;

maxj = j;

}

}

}

Console.WriteLine("The largest value is stored"

+"at ["+maxi+","+maxj+"]"+max);

Console.Read();

}

}

}

Sample Output:

Enter the number of rows.....

2

Enter the number of columns

6

[0,0] 57

[0,1] 71

[0,2] 61

[0,3] 31

[0,4] 77

[0,5] 35

[1,0] 32

[1,1] 73

[1,2] 61

[1,3] 28

[1,4] 92

[1,5] 98

The largest value is stored at [1,5] 98

User Hhbilly
by
4.6k points