arrays Flashcards
(10 cards)
Define an array in programming.
An array is a variable that can store multiple values of the same data type, identified by an index or subscript.
What is meant by the term ‘index’ in arrays?
The index or subscript is the zero-based position reference of elements in an array.
Declare an array called marks to hold marks for 10 students.
int[] marks = new int[10];
Assign the following values to an array: 58, 72, 77, 83, 90, 64, 76, 80, 69, 71
Use marks[0] = 58;, marks[1] = 72; … until marks[9] = 71;
Write a code segment to calculate the sum of an array using a loop.
int sum = 0;
for(int k=0; k<marks.Length; k++) {
sum += marks[k];
}
What does marks.Length return?
It returns the number of elements in the array — in this case, 10.
Declare and initialize an array with the first four months of the year.
string[] months = { “January”, “February”, “March”, “April” };
What error exists in the loop: for (int k = 0; k <= arrValues.Length; k–)?
k– causes an infinite loop. It should be k++ and condition should be k < arrValues.Length.
Identify and fix this line: sum + arrValues[k];
Should be sum += arrValues[k];
What does String[] arrValues = st.Split(‘,’); do?
Splits the string st into an array using , as the delimiter.