Question

In: Computer Science

C# Answer in discussion format Why is an array such a useful tool? What is a...

C# Answer in discussion format

  1. Why is an array such a useful tool?
  2. What is a jagged array? Ask your students to describe some circumstances in which jagged arrays might be useful.
  3. the System.Array class contains a variety of useful, built-in methods, describe one of them and how you would use it.
  4. Describe how to resize an array in C# using the new operator.
  5. How would you declare an array if you do not know in advance how many elements you will have in each execution of a program?

Solutions

Expert Solution

1. Why is an array such a useful tool?

An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type. Index associated with variable and they are being used to represent indiviual elements available in the array. All arrays consist of contigous memory locations.

2. What is a jagged array? Ask your students to describe some circumstances in which jagged arrays might be useful.

Jagged array is a array of arrays such that member arrays can be different size. In other words, the length each array index can differ. The elements of jagged array are refrence types and initialized to null by default. Jagged array can also be mixed with multidimensional arrays. Here, the number of rows will be fixed at the declaration time, but you can vary the number of columns.

In Jagged arrays, user has to provide the number of rows only. If the user is also going to provide the number of columns, then this array will be no more Jagged Array.

Syntax:

data_type[ ] [ ] name_of_array = new data_type[rows] [ ]

Jagged arrays are a special type of arrays that can be used to store rows of data of varying lengths to improve performance when working with multi-dimensional arrays.

3. the System.Array class contains a variety of useful, built-in methods, describe one of them and how you would use it.

System. Array is the base class for all arrays in the common language runtime. It has methods for creating, manipulating, searching, and sorting the arrays we have talked about so far. System. Array implements several interfaces, like ICloneable, IList, ICollection, and IEnumerable.

There are few built in methods of System.Array

IndexOf(Array, Object)

Searches for the specified object and returns the index of the first occurrence within the entire one-dimensional Array.

Sort(Array)

Sorts the elements in an entire one-dimensional Array using the IComparable implementation of each element of the Array.

ToString

Returns a string that represents the current object. (Inherited from Object.)

4. Describe how to resize an array in C# using the new operator.

  1. One approach is to use System. Collections. ...
  2. Another (faster) solution is to re-allocate the array with a different size and to copy the contents of the old array to the new array. The generic function resizeArray (below) can be used to do that.

Example of C# program showing how to resize the array.

using System; class Program

{

static void Main()

{

// Step 1: initialize array for example.

char[] array = new char[4];

array[0] = 'p';

array[1] = 'e';

array[2] = 'r';

array[3] = 'l';

for (int i = 0; i < array.Length; i++)

{ Console.Write(array[i]); }

Console.WriteLine();

// Step 2: resize the array from 4 to 2 elements. Array.Resize(ref array, 2);

for (int i = 0; i < array.Length; i++)

{ Console.Write(array[i]); }

Console.WriteLine(); } }

Output perl pe

5. How would you declare an array if you do not know in advance how many elements you will have in each execution of a program?

In c#, Arrays are useful to store multiple elements of the same data type at contiguous memory locations and arrays will allow us to store a fixed number of elements sequentially based on the predefined number of items. An array can start storing the values from index 0. Suppose if we have an array with n elements, then it will start storing the elements from index 0 to n-1.


Related Solutions

In a Financial Statement Analysis Report, what is the most useful analytical tool ? Why? In...
In a Financial Statement Analysis Report, what is the most useful analytical tool ? Why? In what capacity would you use this analytical tool in business career?
Explain why budgeting is such a useful tool in decision-making.
Explain why budgeting is such a useful tool in decision-making.
What is long term care insurance? Do you think this is a useful tool? Why or...
What is long term care insurance? Do you think this is a useful tool? Why or why not? Would you purchase it? Why or why not?
Write your thoughts on this discussion. Hypothesis testing is a statistical tool useful for ascertaining information...
Write your thoughts on this discussion. Hypothesis testing is a statistical tool useful for ascertaining information about a specific conclusion. Specifically, this type of testing can help with understanding a data set (population) and the samples of data used to make assertions about those populations. The general process for testing a hypothesis involves selecting a particular element of a population such as the "mean, proportion, standard deviation, or variance" (Evans, p139) and then looking for a contrasting detail to compare...
A useful tool in financial statement analysis is the common-size financial statement. What does this tool...
A useful tool in financial statement analysis is the common-size financial statement. What does this tool enable the financial analyst to do? Evaluate financial statements of companies within a given industry of approximately the same value. Determine which companies in the same industry are at approximately the same stage of development. Compare the mix of assets, liabilities, capital, revenue, and expenses within a company over time or between companies within a given industry without respect to relative size. Ascertain the...
Why is being able to measure density precisely a useful tool in Biology? - In your...
Why is being able to measure density precisely a useful tool in Biology? - In your first paragraph describe how you could measure viral or bacterial density . - In the following paragraph, describe why it is useful to measure bacterial or viral density accurately and precisely . - Finally, how does measuring density precisely and accurately help your inference about the relation between density and diversity of the COVID-19 virus for example? . (In this case. Does it refer...
Explain why the Pyramid of Needs by Abraham Maslow is a useful tool for entrepreneurs. (7...
Explain why the Pyramid of Needs by Abraham Maslow is a useful tool for entrepreneurs.
Do you think the Phillips curve is a useful tool for analyzing the economy today? Why...
Do you think the Phillips curve is a useful tool for analyzing the economy today? Why or why not? What is the government’s key priority these days- fight unemployment or fight inflation? Give your opinion from a personal level.
1. a)Why is the Statement of Cash Flows the most useful tool for analyzing highly leveraged...
1. a)Why is the Statement of Cash Flows the most useful tool for analyzing highly leveraged companies? (b) What is the risk of heavy reliance on external financing for rapid growth? 2. a)Explain what analyst evaluating the Leveraged Buyout (LBO) would miss if they focused on earnings instead of cash flows? (b) Given that expectations about future cash flows are not realized, and investors find themselves lacking the cash required to meet interest expense and the scheduled principal payments, discuss...
What are wrapper classes and why are they useful for ArrayLists? In your answer, include examples...
What are wrapper classes and why are they useful for ArrayLists? In your answer, include examples of autoboxing and unboxing. Your Discussion should be at least 250 words in length, but not more than 750 words. Once you’ve completed your initial post, be sure to respond to the posts of at least 3 of your classmates.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT