CS
205 - Programming for the Sciences
Spring 2008 - Answers to
In-class Exercise for 01/31/08
Here are answers to the array exercises:
Write a program to declare an array of doubles initialized with 10 random values and display them.
private void start_Click(object sender, EventArgs e)
{
// Declare array with initialization
double[] elements = { 2.1, 34.2, 6.7, 7.5, 52, 63, 41, 2, 16, 3.5 };
int i, numElements
// Display elements
numElements = elements.Length;
for (i = 0; i < numElements; i++)
results.Items.Add(elements[i]);
}
Write a program to declare and create an array of 10 doubles, fill each array element with its index value, and display the array elements.
private void start_Click(object sender, EventArgs e)
{
// Declare array variable
double[] elements;
int i, numElements
// Create array
elements = new double[10];
// Fill array
numElements = elements.Length;
for (i = 0; i < numElements; i++)
elements[i] = i;
// Display elements
for (i = 0; i < numElements; i++)
results.Items.Add(elements[i]);
}
Write a program to declare and create an array of 10 doubles, fill each array element with the square root of its index, and display the array elements.
private void start_Click(object sender, EventArgs e)
{
// Declare array variable
double[] elements;
int i, numElements
// Create array
elements = new double[10];
// Fill array
numElements = elements.Length;
for (i = 0; i < numElements; i++)
elements[i] = Math.Sqrt(i);
// Display elements
for (i = 0; i < numElements; i++)
results.Items.Add(elements[i]);
}
Write a program to declare and create an array of 10 doubles, fill the array so the element values are in reverse order of the indexes of the array, and display the array elements. I.e., the first element of the array will have value 9, the second will have 8, etc., to the last element of the array having value 0.
private void start_Click(object sender, EventArgs e)
{
// Declare array variable
double[] elements;
int i, numElements
// Create array
elements = new double[10];
// Fill array
numElements = elements.Length;
for (i = 0; i < numElements; i++)
elements[i] = numElements - i - 1;
// Display elements
for (i = 0; i < numElements; i++)
results.Items.Add(elements[i]);
}
Write a program to declare an array variable, call the GetArray method to create an array from user input, and display the array elements. The GetArray method uses the algorithm given previously in the FindRoots exercise to convert a string of values separated by commas into an array of double values.
private void start_Click(object sender, EventArgs e)
{
// Declare array variable
double[] elements;
int i, numElements
// Create array
GetArray(out elements);
// Display elements
numElements = elements.Length;
for (i = 0; i < numElements; i++)
results.Items.Add(elements[i]);
}
Write a program to declare an array variable, call the GetArray method to create an array from user input, and iterate through the array displaying only those values that are greater than 10.
private void start_Click(object sender, EventArgs e)
{
// Declare array variable
double[] elements;
int i, numElements
// Create array
GetArray(out elements);
// Filter and display elements
numElements = elements.Length;
for (i = 0; i < numElements; i++)
if (elements[i] > 10)
results.Items.Add(elements[i]);
}
Write a program to declare an array variable, call the GetArray method to create an array from user input, get the value of a search target from the input textbox, and iterate through the array displaying the indexes of the positions that contain the search value.
private void start_Click(object sender, EventArgs e)
{
// Declare array variable
double[] elements;
double target;
int i, numElements
// Create array
GetArray(out elements);
// Get user input for target
target = Convert.ToDouble(input.Text);
// Search array and display index
numElements = elements.Length;
for (i = 0; i < numElements; i++)
if (elements[i] == target)
results.Items.Add(i);
}
02/06/08