Perform the following tasks: a) declare constant MAX_SIZE array initialize to 10
ID: 3849039 • Letter: P
Question
Perform the following tasks: a) declare constant MAX_SIZE array initialize to 100 b) declare variable my Array which will reference an array with MAX+SIZE elements of type double (initialize all elements to 0). c) assign the value of 2.5 to array with index 4 d) assign the value of 4.50 to array elements with index 6 e) display both array elements f) sum all elements using an iterative structure (for or while loop) Identify, it there is a syntax error each code segment. Briefly explain why? a) ________________ private const int MAX_SIZE = 10: MAX_SIZE = 100: b) ___________________ private int [] array = new int [5]: array[0] = 2.5: array[1] = 1: array[2] = 2.99: array[3] = 2.3: array[4] = 3: c) _________________ private int[] array = new int [5] for(int i = 0: iExplanation / Answer
6.A
Constants in C# are defined using the const keyword using following syntax (declare and initialize)
const <data_type> <contant_name> = value;
NOTE:
Answer:
Const int MAX_SIZE = 100;
6.B
To declare an Array in C# following syntax is used:
data_type [] array_name;
To initialize an Array in C# following syntax is used:
array_name = new <data_type>[size_of_an_array];
OR
above 2 steps can be combined i.e. declare and initialize
<data_type>[] <array_name> = new <data_type>[size_of_an_array];
assigning value to array elements
<array_name>[index] = value;
Note:
Answer:
//(array)variable, datatype = double
double[] myArray =new double[ MAX_SIZE ] ;
/* initialize elements of array with size MAX_SIZE*/
for (int i=0; i< MAX_SIZE; i++)
{
myArray[i] = 0;
}
6.C and 6.D
As discussed above following syntax can be used to assigning value to an array elements
<array_name>[index] = value;
Answer
myArray[4] =2.5;
myArray[6] =4.50;
6.E
To access Array elements following syntx can be used
<data_type> <local_variable> = <array_name>[index];
To display Array elements on console we can use Console.WriteLine() function provided by C#
Answer:
Console.WriteLine("Element at index 4 = {0}", myArray[4]);
Console.WriteLine("Element at index 6 = {0}", myArray[6]);
Note: please mind the double quotes
6.F
Previously we have seen the use of for-loop with array elements , in similar way we can use to find the sum of all the array elements.
And we have also seen how to access array elements.
Answer
//local variable to store sum
double sum = 0;
for (int i=0; i< MAX_SIZE; i++)
{
sum = sum + myArray[i];
}
Console.WriteLine("SUM of Array elements = {0}", sum);
OR Using While loop
int index = 0;
double sum = 0;
while( index < MAX_SIZE ){
sum = sum + myArray[index];
}
Console.WriteLine("SUM of Array elements = {0}", sum);
Answer 6 – consolidated
const int MAX_SIZE = 100;
double[] myArray =new double[ MAX_SIZE ] ;
/* initialize elements of array with size MAX_SIZE*/
for (int i=0; i< MAX_SIZE; i++)
{
myArray[i] = 0;
}
myArray[4] =2.5;
myArray[6] =4.50;
Console.WriteLine("Element at index 4 = {0}", myArray[4]);
Console.WriteLine("Element at index 6 = {0}", myArray[6]);
double sum = 0;
for (int i=0; i< MAX_SIZE; i++)
{
sum = sum + myArray[i];
}
Console.WriteLine("SUM of Array elements = {0}", sum);
Answer( 7.A )
As seen previously the value of const variables are constant and can’t be changed after declaration so below code will throw an error
Answer( 7.B)
It can be seen that here array data type is int ,but they are trying to assign double value (2.5,2.99,2.3). Since C# don’t have any implicit method or function typecast from double to int following line of codes will throw an error
Note: you can try explicit conversions to run the code without any error
Answer( 7.C )
It is seen that first line of code is missing terminator i.e. ( ; ), so it will again throw an error.