Find the error(s) in the following code. The TwoDArrays method should create a two- dimensional array and initialize all its values to one.


int[,] TwoDArrays()
{
int[,] intArray = new int[ 4, 4 ];

// Assign 1 to all cell values
for ( int intI = 0; intI < 4; intI++ )
{
intArray[ intI, intI ] = 1;
}

return intArray;

} // end method TwoDArrays


To assign each element in a two-dimensional array, nested for loops should be

used. The complete incorrect code reads:



// Exercise 18.15 Solution

// Initialize.cs (Incorrect)



using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;



namespace Initialize

{

///

/// Summary description for FrmInitialize.

///


public class FrmInitialize : System.Windows.Forms.Form

{

private System.Windows.Forms.Label lblArray;

private System.Windows.Forms.ListBox lstArray;

///

/// Required designer variable.

///


private System.ComponentModel.Container components = null;



public FrmInitialize()

{

//

// Required for Windows Form Designer support

//

InitializeComponent();



//

// TODO: Add any constructor code after InitializeComponent

// call

//

}



///

/// Clean up any resources being used.

///


protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}



// Windows Form Designer generated code



///

/// The main entry point for the application.

///


[STAThread]

static void Main()

{

Application.Run( new FrmInitialize() );

}



// calls TwoDArrays and prints the resulting array

private void FrmInitialize_Load(

object sender, System.EventArgs e )

{

int[,] intTempArray = TwoDArrays();

int intUpperBound = intTempArray.GetUpperBound( 0 );



// print each row of the array

for ( int intI = 0; intI <= intUpperBound; intI++ )

{

lstArray.Items.Add(intTempArray[ intI, 0 ] +

"\t" + intTempArray[ intI, 1 ] +

"\t” + intTempArray[ intI, 2 ] +

"\t" + intTempArray[ intI, 3 ] );

}



} // end method FrmInitialize_Load



// initializes a 2D array to contain all 1's

int[,] TwoDArrays()

{

int[,] intArray = new int[ 4, 4 ];



// assign 1 to all cell values

for ( int intI = 0; intI < 4; intI++ )

{

intArray[ intI, intI ] = 1;

}

Needs nested for loops

to access the entire array

return intArray;



} // end method TwoDArrays



} // end class FrmInitialize

}



Computer Science & Information Technology

You might also like to view...

The process of creating a make table query is very similar to which other type of query?

A) Append query B) Delete query C) Find duplicates query D) Update query

Computer Science & Information Technology

You can customize many aspects of Windows Vista, including all of the following EXCEPT:

A) screen savers B) the Shut down menu C) Windows Sidebar D) the desktop

Computer Science & Information Technology

________ an application's icon to open it from the desktop

Fill in the blank(s) with correct word

Computer Science & Information Technology

In Outlook Express, the ________ folder stores messages that you have deleted

A) Recycle Bin B) Outbox C) Deleted Items D) Empty

Computer Science & Information Technology