Multiple Choices
-
What is the correct way of creating an array of
int
of size 5 namedmyArray
?-
int[] myArray = new int[5];
-
int[] myArray = int[5];
-
int[5] myArray = new int[];
-
int[4] myArray = new int[];
-
int myArray = new int[5];
-
int[] myArray = new int[4];
-
int[] myArray = new int(5);
-
int[] myArray = int[4];
-
-
Consider the following code:
What will it display?
- 5
- Nothing
- 20
- 15
- grades
- grades[2]
- 10
-
In the following statement, the value
5
is called the…string[] names = new string[5];
- allocation limit
- size declarator
- length
- upper bound
-
Each element in an array is assigned a unique number called the…
- subscript
- index
- position
- rank
-
Consider the following code:
Something is wrong with it, can you tell what?
- There will be an “Index was outside the bounds of the array.” error.
- The array is not properly initialized.
- The loop is infinite
-
grades.Length
is not declared.
Exercises
Syntax
-
Write a statement that creates a 10-element
int
array namednumbers
.Solution
-
Write a statement that creates and initializes an array of
double
with the values 12.5, 89.0 and 3.24.Solution
-
In the following, what is the value of the size declarator? What is the value of the index?
Solution
The size declarator is 8, the subscript, or index, is 4.
-
What is “array bounds checking”? When does it happen?
Solution
It is when C# makes sure that you’re not using a subscript outside the allowed range. It happens at run time, so after the program is already compiled, when it is executed.
-
Is there an error with the following code? If you think there is one, explain it, otherwise draw the content of the
myIncomes
array once those statements have been executed.Solution
The subscripts are off. They should go from 0 to 4.
-
What would be the size of the
test
array after the following statement has been executed?int[] test = {3, 5, 7, 0, 9};
Solution
5
-
What is wrong with the following array declaration?
Solution
The size declarator cannot be negative.
-
Suppose we have an array named
temp
that has been declared and initialized. How can we know the number of elements in this array?Solution
By using the
Length
field:temp.Length
is the number of elements in thetemp
array. -
What is the value of
count
and the content ofnumber
once the following has been executed?Solution
The value of
count
is 1,numbers
contains the elements 3, 4, 8. -
Describe what the following code would do.
Solution
This code declares and initializes an
int
array with the values 3, 8, and 11, and then sum those values in anaccumulator
variable.
Displaying Arrays
-
Write code that displays the first and last element of an array.
Solution
-
What will be displayed at the screen by the following code?
Solution
0 2 4 6 8 10
-
Suppose we are given an
int
arraydailyPushUp
with 7 elements. Write a piece of code that displays the value of the elements stored in the arraydailyPushUp
.Solution
-
Write code that displays every element in a
numbers
array of integers.Solution
-
Write code that displays every element in a
numbers
array of integers in reverse order.Solution
-
Write code that displays every other element in a
numbers
array of integers (that is, every even index).Solution
-
Given an array
numbers
and a variablex
, write code that displays every value inmyArray
that is equal to or greater thanx
.Solution
-
Write code that displays every unique value of a sorted array
numbers
. This array could contain, for example, 1, 1, 1, 4, 4, 5, 8, 9, 11: values are increasing and can occur multiple times. In our example, the values 1, 4, 5, 8, 9 and 11.Solution
Filling Arrays
-
Given an array
myArray
and some valuex
, write code that sets the value of every array element tox
.Solution
-
Given some positive number
n
, write code that first declares an array of lengthn
, then sets its contents to sequentially increasing values 1, 2, 3, …, n.Solution
-
Given an array of integers, and two integer variables
oldValue
andnewValue
; write code that replaces every occurrence ofoldValue
in the array withnewValue
.Solution
-
Write code that squares every value in an
myArray
integer array. For example, an array containing 2, 3, 4 would after the program contain 4, 9, 16.Solution
Looking For Values
-
Given an array
myArray
and a valueval
, write code that checks ifmyArray
containsval
. The result should betrue
ifval
occur inmyArray
andfalse
otherwise.Solution
-
Given an array
myArray
and a variablex
, write code that computes the number of timesx
occurs inmyArray
.Solution
-
Given an array
myArray
and two valuesx
andy
, write code that checks ifmyArray
contains eitherx
ory
. The result should betrue
ifx
ory
occur inmyArray
andfalse
otherwise.Solution
-
Given an array
myArray
and two valuesx
andy
, write code that checks ifmyArray
contains both valuesx
andy
. The result should betrue
when both values occur andfalse
otherwise.Solution
-
Given an integer
myArray
and a strictly positive integer valuex
, find an array element whose value is largest while also being strictly less thanx
and display it, or display 0 if there is no such value. For example, in an array containing 1, 2, 6, 7, 3, 9 with x being 8, the solution is 7.Solution
-
Consider an array of
char
. Implement code to check if the array values form a palindrome, i.e., it reads the same way forwards and backwards.Solution
Note that after the code has been executed, both sides of the word have been checked, and
palindromeSoFar
istrue
if both sides are mirrors of each other. -
Assume
arrayEx
is an array ofint
containing the following values:and consider the following algorithm:
Complete the following table, giving the value of
sta
,end
,mid
,fsf
andcur
before thewhile
loop is executed, after it has executed one time, two times, etc. If the value is not set at this point, write “undef”, and if the loop stops before the Xth iteration, simply cross out the Xth iteration. Report the value even if it did not change.Before loop After 1 iteration After 2 iterations After 3 iterations After 4 iterations sta
end
mid
fsf
cur
Solution
The code can easily be edited to display the information we are looking for.
(Download this code){.download-button}
Manipulating Two Arrays
-
Assuming we have two
int
arrays of the same size,firstA
andsecondA
, write a program that copies the content offirstA
intosecondA
.Solution
-
Given two arrays
array1
andarray2
, write a program to determine if there exists a value that occurs in both arrays. If such value exists, the result should betrue
andfalse
otherwise.Solution
-
Write a program that combines two
string
arrays calledarray1
andarray2
into a single array containing first all the elements fromarray1
, then all the elements fromarray2
.Solution
-
Given two arrays
arrayA
andarrayB
, write code to check if every element inarrayB
occurs inarrayA
, then display the result astrue
orfalse
.Solution
Using
while
loops:Using
break
:
Methods
-
Write a static method (header included) that takes as an argument an
int
array, and displays on the screen the value of each element of that array.Solution
-
Write a static method (header included) that takes as an argument an
int
array, and stores the value 10 in each element of that array.Solution
Simple Algorithms
-
Write a program that computes the sum of values stored in a
numbers
array of integers and displays it.Solution
-
Given an array of positive integers, count how many even values occur in that array.
Solution
-
Write a program that computes the average of the elements in a
arrayP
numeric array.Solution
-
Write a program that finds the largest value in an integer array
arrayP
.Solution
-
Write a program that finds the smallest value in an integer array
arrayP
.Solution
-
Write a program that finds the second smallest value in an array of integers
arrayP
.Solution
-
Write code that finds the index of the first occurrence of a value
val
in an arrayarrayP
. If the array does not contain the value, the result should be -1.Solution
-
Write code that finds the index of the last occurrence of a value in an array. If the array does not contain the value, the result should be -1.
Solution
-
Write code to reverse the contents of an array
myArray
. For example, an array containing 1, 4, 3, 2, 5 should contain, after the program was executed, 5, 2, 3, 4, 1.Solution
Wrap-Up Problems
-
Declare and initialize three arrays:
- Choose different data type for each array,
- Make the arrays have different lengths: 3, 5, 10 elements respectively,
- Initialize each array with appropriate values of your choice (depends on the type).
After you have declared and initialized the arrays, display on the screen
- The first value from array 1 (0th index),
- The last value from array 2 (4th index),
- The first three values from array 3 (indexed 0 - 2).
Example Solution
-
Consider this array of words:
Write code to display an answer to the following questions. If the solution requires looping, use foreach loop when possible. Otherwise, use a for loop.
- Does words array contain “engine”? (true/false)
- Does words array contain “day” at least 2 times? (true/false)
- What is the position (index) of the word “society”? If it does not exist answer should be -1. Find the position of the first occurrence in case there are multiple matches.
After you have implemented your code, change the array contents and make sure your code still works and does not crash.
Here is another array of words to test your solution:
Solution