diff --git a/source/exercises/w_sol/collections/1darrays.md b/source/exercises/w_sol/collections/1darrays.md index a4357383..a342cea5 100644 --- a/source/exercises/w_sol/collections/1darrays.md +++ b/source/exercises/w_sol/collections/1darrays.md @@ -54,33 +54,33 @@ tags: - [ ] `grades.Length` is not declared. -## Warm-up Exercises +## Exercises ### Syntax -#. Write a statement that creates a 10-element **int** array named `numbers`. +#. Write a statement that creates a 10-element `int` array named `numbers`.
Solution - ```cs + ``` int[] numbers = new int[10]; ```
-#. Write a statement that creates and initializes a double array with the values 12.5, 89.0 and 3.24. +#. Write a statement that creates and initializes an array of `double` with the values 12.5, 89.0 and 3.24.
Solution - ```cs + ``` double[] question = {12.5, 89.0, 3.24}; ```
#. In the following, what is the value of the size declarator? What is the value of the index? - ```cs + ``` int[] numbers; numbers = new int[8]; numbers[4] = 9; @@ -95,12 +95,12 @@ tags:
Solution - It is when C# makes sure that you're not using a subscript outside the allowed range. It happens at run time. + 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. +#. 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. - ```cs + ``` double[] myIncomes = new double[5]; myIncomes[1] = 3.5; // No income on day two. @@ -114,7 +114,7 @@ tags: 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? +#. What would be the size of the `test` array after the following statement has been executed? `int[] test = {3, 5, 7, 0, 9};` @@ -125,7 +125,7 @@ tags: #. What is wrong with the following array declaration? - ```cs + ``` int[] books = new int[-1]; ```
@@ -137,12 +137,12 @@ tags:
Solution - By using the `Length` field: `temp.Length` + By using the `Length` field: `temp.Length` is the number of elements in the `temp` array.
#. What is the value of `count` and the content of `number` once the following has been executed? - ```cs + ``` int count=2; int[] number={3, 5, 7}; number[count--] = 8; @@ -151,22 +151,22 @@ tags:
Solution - `count` is 1. `numbers` is 3, 4, 8. + The value of `count` is 1, `numbers` contains the elements 3, 4, 8.
#. Describe what the following code would do. - ```cs + ``` int[] record = { 3, 8, 11 }; int accumulator = 0; foreach (int i in record) - accumulator += i; + accumulator += i; ```
Solution - Declare and initialize an int array with the values 3, 8, and 11, and then sum those values in an accumulator variable. + This code declares and initializes an `int` array with the values 3, 8, and 11, and then sum those values in an `accumulator` variable.
### Displaying Arrays @@ -176,7 +176,7 @@ tags:
Solution - ```cs + ``` int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; Console.WriteLine($"First element: {numbers[0]}"); @@ -186,7 +186,7 @@ tags: #. What will be displayed at the screen by the following code? - ```cs + ``` int[] values = new int[6]; for (int i = 0 ; i < 6 ; i++) values[i] = (i * 2 ); @@ -205,12 +205,12 @@ tags: 10
-#. Suppose we are given an **int** array `dailyPushUp` with 7 elements. Write a piece of code that displays the value of the elements stored in the array `dailyPushUp`. +#. Suppose we are given an `int` array `dailyPushUp` with 7 elements. Write a piece of code that displays the value of the elements stored in the array `dailyPushUp`.
Solution - ```cs + ``` for (int j = 0; j < 7; j++) Console.WriteLine(dailyPushUp[j]); ``` @@ -221,8 +221,8 @@ tags:
Solution - ```cs - for ( int i = 0; i < numbers.Length; i++ ) + ``` + for (int i = 0; i < numbers.Length; i++) { Console.WriteLine(numbers[i]); } @@ -234,8 +234,8 @@ tags:
Solution - ```cs - for ( int i = (numbers.Length - 1) ; i >= 0 ; i-- ) + ``` + for (int i = (numbers.Length - 1) ; i >= 0 ; i-- ) { Console.WriteLine(numbers[i]); } @@ -247,7 +247,7 @@ tags:
Solution - ```cs + ``` for (int i = 0; i < numbers.Length; i += 2) { Console.WriteLine(numbers[i]); @@ -260,7 +260,7 @@ tags:
Solution - ```cs + ``` for (int i = 0; i < numbers.Length; i++) { if (numbers[i] >= x) @@ -271,29 +271,30 @@ tags: ```
-### Filling Arrays - -#. Given some positive number `n`, write code that first declares an array of length `n`, then sets its contents to sequentially increasing values 1, 2, 3, ..., n. - +#. 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 - - ```cs - int[] nums = new int[n]; - - for ( int i = 0 ; i < n ; i++ ) + + ``` + for (int i = 0 ; i < numbers.Length ; i++) { - nums[i] = i + 1; + if (i > 0 && numbers[i] != numbers[i - 1]) + { + Console.WriteLine(numbers[i]); + } } ``` -
- +
+ +### Filling Arrays + #. Given an array `myArray` and some value `x`, write code that sets the value of every array element to `x`.
Solution - ```cs + ``` for (int i = 0; i < myArray.Length; i++) { myArray[i] = x; @@ -301,50 +302,28 @@ tags: ```
-#. Write code that displays every unique value of a sorted array. This array could be, for example, 1 1 1 4 4 5 8 9 11 such that values are increasing but value can occur multiple times. +#. Given some positive number `n`, write code that first declares an array of length `n`, then sets its contents to sequentially increasing values 1, 2, 3, ..., n.
Solution - ```cs - int[] numbers = {1, 1, 1, 4, 4, 5, 8, 9, 11}; + ``` + int[] nums = new int[n]; - for (int i = 0 ; i < numbers.Length ; i++) + for (int i = 0 ; i < n ; i++) { - if (i > 0 && numbers[i] != numbers[i - 1]) - { - Console.WriteLine(numbers[i]); - } + nums[i] = i + 1; } ``` -
+
-## Warm-up Exercises -- Calculating With Arrays +#. Given an array of integers, and two integer variables `oldValue` and `newValue`; write code that replaces every occurrence of `oldValue` in the array with `newValue`. -#. Write a program that computes the sum of values stored in a `numbers` array of integers -
Solution - - ```cs - int sum = 0; - - for (int i = 0; i < numbers.Length; i++) - { - sum += numbers[i]; - } - Console.WriteLine($"The sum is {sum}."); ``` -
- -#. Given an array of integers, and two integer variables `oldValue` and `newValue`; write code that replaces every occurrence of `oldValue` in the array with `newValue`. - -
- Solution - - ```cs - for (int i = 0; i < myArray.Length; i++ ) + for (int i = 0; i < myArray.Length; i++) { if (myArray[i] == oldValue) { @@ -355,12 +334,12 @@ tags:
#. 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 - - ```cs - for ( int i = 0; i < myArray.Length; i++ ) + + ``` + for (int i = 0; i < myArray.Length; i++) { myArray[i] *= myArray[i]; } @@ -368,14 +347,14 @@ tags:
-## Warm-Up Exercise -- Looking For Values +### Looking For Values #. Given an array `myArray` and a value `val`, write code that checks if `myArray` contains `val`. The result should be `true` if `val` occur in `myArray` and `false` otherwise.
Solution - ```cs + ``` bool valOccurs = false; for (int i = 0; i < myArray.Length; i++) @@ -388,12 +367,29 @@ tags: ```
+#. Given an array `myArray` and a variable `x`, write code that computes the number of times `x` occurs in `myArray`. + +
+ Solution + + ``` + int count = 0; + for (int i = 0; i < array.Length; i++) + { + if (x == array[i]) + { + count++; + } + } + ``` +
+ #. Given an array `myArray` and two values `x` and `y`, write code that checks if `myArray` contains *either* `x` or `y`. The result should be `true` if `x` or `y` occur in `myArray` and `false` otherwise.
Solution - ```cs + ``` bool eitherOccurs = false; for (int i = 0; i < array.Length; i++) @@ -411,7 +407,7 @@ tags:
Solution - ```cs + ``` bool xOccurs = false, yOccurs = false, bothOccur = false; for (int i = 0; i < myArray.Length; i++) @@ -440,7 +436,7 @@ For example, in an array containing 1, 2, 6, 7, 3, 9 with x being 8, the solutio
Solution - ```cs + ``` int largest = 0; for (int i = 0; i < myArray.Length; i++) { @@ -459,7 +455,7 @@ For example, in an array containing 1, 2, 6, 7, 3, 9 with x being 8, the solutio
Solution - ```cs + ``` bool palindromeSoFar = true; int n = myArray.Length; for (int i = 0; i < (n / 2); i++) @@ -468,19 +464,33 @@ For example, in an array containing 1, 2, 6, 7, 3, 9 with x being 8, the solutio { palindromeSoFar = false; } - } // Both sides of the word have been checked and are mirrors of each other. + } ``` + + Note that after the code has been executed, both sides of the word have been checked, and `palindromeSoFar` is `true` if both sides are mirrors of each other.
-## Warm-up Exercises -- Manipulating Two Arrays +### Manipulating Two Arrays + +#. Assuming we have two `int` arrays of the same size, `firstA` and `secondA`, write a program that copies the content of `firstA` into `secondA`. + +
+ Solution + + ``` + for (int k =0; k < firstA.Length; k++) + secondA[k] = firstA[k]; + ``` +
+ #. Given two arrays `array1` and `array2`, write a program to determine if there exists a value that occurs in both arrays. If such value exists, the result should be `true` and `false` otherwise.
Solution - ```cs + ``` bool valueInCommon = false; for (int i = 0; i < array1.Length; i++) @@ -496,13 +506,34 @@ For example, in an array containing 1, 2, 6, 7, 3, 9 with x being 8, the solutio ```
+ +#. Write a program that combines two `string` arrays called `array1` and `array2` into a single array containing first all the elements from `array1`, then all the elements from `array2`. + +
+ Solution + + ``` + string[] combined = new string[array1.Length + array2.Length]; + + for (int i = 0; i < array1.Length; i++) + { + combined[i] = array1[i]; + } + + for (int j = 0; j < array2.Length; j++) + { + combined[array1.Length + j] = array2[j]; + } + ``` +
+ #. Given two arrays `arrayA` and `arrayB`, write code to check if every element in `arrayB` occurs in `arrayA`, then display the result as `true` or `false`.
Solution Using `while` loops: - ```cs + ``` int i = 0; int j; bool containsB = true; @@ -521,7 +552,6 @@ For example, in an array containing 1, 2, 6, 7, 3, 9 with x being 8, the solutio } if (!containCurrentVal) { - Console.WriteLine(arrayB[i] + " does not occur."); containsB = false; } else @@ -534,7 +564,7 @@ For example, in an array containing 1, 2, 6, 7, 3, 9 with x being 8, the solutio Using `break`: - ```cs + ``` bool containsB = false; for (int i = 0; i < arrayB.Length; i++) @@ -555,295 +585,227 @@ For example, in an array containing 1, 2, 6, 7, 3, 9 with x being 8, the solutio break; } } - - Console.WriteLine(containsB); ```
- - - -## Questions (harder) - - -#. Given an array of positive integers, count how many even values occur in that array. - +### 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 - - ```cs - int count = 0; - - for (int i = 0; i < myArray.Length; i++) - { - if (myArray[i] % 2 == 0) - { - count++; - } + + ``` + public static void displayArray(int[] arrayP){ + foreach (int element in arrayP) + Console.WriteLine(element); } - return count; ```
- -#. Given an array and some value x, write code that computes the number of times x occurs in the array. - + +#. 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 - - ```cs - int count = 0; - for (int i = 0; i < array.Length; i++) - { - if (x == array[i]) - { - count++; - } + + ``` + public static void fillArray(int[] arrayP){ + for (int j = 0; j < arrayP.Length; j++) + arrayP[j] = 10; } ```
+ + +## Simple Algorithms -#. Write a program that computes the average of values of a numeric array. +#. Write a program that computes the sum of values stored in a `numbers` array of integers and displays it.
Solution - ```cs - int ArrayAverage(int[] a) - { - int sum = 0; - - for (int i = 0; i < a.Length; i++) - { - sum += a[i]; - } + ``` + int sum = 0; - return sum / array.Length; + for (int i = 0; i < numbers.Length; i++) + { + sum += numbers[i]; } + + Console.WriteLine($"The sum is {sum}."); ```
-#. Write a program that finds the largest value in an integer array. +#. Given an array of positive integers, count how many even values occur in that array.
Solution - ```cs - int LargestValue(int[] a) + ``` + int count = 0; + + for (int i = 0; i < myArray.Length; i++) { - if (a.Length == 1) - { - return a[0]; - } - else + if (myArray[i] % 2 == 0) { - int largest = a[0]; - - foreach (int i in a) - { - if (i > largest) - { - largest = i; - } - } - - return largest; + count++; } } ``` -
+
-#. Write a program that finds the smallest value in an integer array. +#. Write a program that computes the average of the elements in a `arrayP` numeric array.
Solution - ```cs - int SmallestValue(int[] a) - { - if (a.Length == 1) - { - return a[0]; - } - else - { - int smallest = a[0]; - - foreach (int i in a) - { - if (i < smallest) - { - smallest = i; - } - } + ``` + int sum = 0; - return smallest; - } + for (int i = 0; i < arrayP.Length; i++) + { + sum += arrayP[i]; } + + double average = (double)sum / arrayP.Length; ```
-#. Write a program that finds second smallest value in an array of integers. +#. Write a program that finds the largest value in an integer array `arrayP`.
Solution - ```cs - int SecondSmallestValue(int[] a) + ``` + int maxSoFar = arrayP[0]; + foreach (int element in arrayP) { - int smallest = a[0]; - int secondSmallest = a[1]; - - if (smallest > secondSmallest) + if (element > maxSoFar) { - int temp = smallest; - smallest = secondSmallest; - secondSmallest = temp; + maxSoFar = element; } - - for (int i = 2; i < a.Length; i++) - { - if (a[i] < smallest) - { - secondSmallest = smallest; - smallest = a[i]; - } - else if (a[i] < secondSmallest && a[i] > smallest) - { - secondSmallest = a[i]; - } - } - - return secondSmallest; } ```
-#. Write code that finds the index of the first occurrence of a value in an array. If the array does not contain the value, the result should be -1. +#. Write a program that finds the smallest value in an integer array `arrayP`.
Solution - ```cs - int FirstOccurrence(int[] a, int v) + ``` + int minSoFar = arrayP[0]; + foreach (int element in arrayP) { - for (int i = 0; i < a.Length; i++) + if (element < minSoFar) { - if (a[i] == v) - { - return i; - } + minSoFar = element; } - return -1; } ```
-#. 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. +#. Write a program that finds the *second* smallest value in an array of integers `arrayP`.
Solution - ```cs - int LastOccurrence(int[] a, int v) + ``` + int smallest = arrayP[0]; + int secondSmallest = arrayP[1]; + + if (smallest > secondSmallest) + { + int temp = smallest; + smallest = secondSmallest; + secondSmallest = temp; + } + + for (int i = 2; i < arrayP.Length; i++) { - for (int i = a.Length - 1; i > 0; i--) + if (arrayP[i] < smallest) { - if (a[i] == v) - { - return i; - } + secondSmallest = smallest; + smallest = arrayP[i]; + } + else if (arrayP[i] < secondSmallest && arrayP[i] > smallest) + { + secondSmallest = arrayP[i]; } - return -1; } ```
-#. Write code that counts the number of occurrences of a specified value in an array. +#. Write code that finds the index of the first occurrence of a value `val` in an array `arrayP`. If the array does not contain the value, the result should be -1.
Solution - ```cs - int NumberOfOccurrences(int[] a, int v) + ``` + int index = -1; + + for (int i = arrayP.Length -1; i >= 0; i--) { - int count = 0; - foreach (int i in a) + if (arrayP[i] == val) { - if (i == v) - { - count++; - } + index = i; } - return count; } ```
-#. Write code to reverse the contents of an array. -Example: Array [1,4,3,2,5] should be [5,2,3,4,1] after being reversed +#. 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 - ```cs - void ReverseArray(int[] array) + ``` + int index = -1; + + for (int i = 0; i < arrayP.Length; i++) { - for (int i = 0, j = array.Length - 1; i < j; i++, j--) + if (arrayP[i] == val) { - int temp = array[i]; - array[i] = array[j]; - array[j] = temp; + index = i; } } - ``` + ```
-#. Write a program that combines two arrays of same data type into a single array. No values should be lost during the merge. + +#. 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 - ```cs - string[] CombineArray(string[] array1, string[] array2) + ``` + for (int i = 0, j = myArray.Length - 1; i < j; i++, j--) { - string[] combined = new string[array1.Length + array2.Length]; - - int i = 0; - - for (; i < array1.Length; i++) - { - combined[i] = array1[i]; - } - - for (int j = 0; j < array2.Length; j++) - { - combined[i + j] = array2[j]; - } - - return combined; + int temp = myArray[i]; + myArray[i] = myArray[j]; + myArray[j] = temp; } ```
- -## Problems +## 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) + - 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) + - 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 - ```cs + ``` // Initialize arrays string[] names = { "Alice", Bob", "Charlie" }; float[] tenths = { 0.1f, 0.2f, 0.3f, 0.4f, 0.5f }; @@ -866,7 +828,7 @@ Example: Array [1,4,3,2,5] should be [5,2,3,4,1] after being reversed #. 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. + #. 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. @@ -879,7 +841,7 @@ Example: Array [1,4,3,2,5] should be [5,2,3,4,1] after being reversed
Solution - ```cs + ``` // Code for finding the word "engine" in the array bool foundEngine = false; diff --git a/source/exercises/w_sol/collections/2darrays.md b/source/exercises/w_sol/collections/2darrays.md new file mode 100644 index 00000000..df453951 --- /dev/null +++ b/source/exercises/w_sol/collections/2darrays.md @@ -0,0 +1,44 @@ +--- +tags: + - datatypes/collections +--- + +# Two-Dimensional Arrays + +## Exercises + +#. Write a statement that creates a 2-dimensional rectangular array of `int` with 5 rows and 3 columns. + +
+ Solution + + ``` + int[,] matrix = new int[5, 3]; + ``` +
+ +#. Write a statement that creates a 2-dimensional jagged array of `int` with 2 rows. The first row should contain an array containing 1, the second row should contain an array containing 2, 3. + +
+ Solution + + ``` + int[][] jaggedArray = new int[2][]; + jaggedArray[0] = new int[1] { 1 }; + jaggedArray[1] = new int [2]{ 2, 3}; + ``` + +#. Suppose we have a 2-dimensional rectangular array named `temp` that has been declared and initialized. How can we know the number of rows in this array? + +
+ Solution + By using the `GetLength` method: `temp.GetLength(0)` is the number of rows in the `temp` array. +
+ +#. Suppose we have a 2-dimensional rectangular 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 the `temp` array. + We can also compute the product of `temp.GetLength(0)` and `temp.GetLength(1)`. +
diff --git a/source/exercises/w_sol/io/index.md b/source/exercises/w_sol/io/index.md index 083ca106..7bdb1cca 100644 --- a/source/exercises/w_sol/io/index.md +++ b/source/exercises/w_sol/io/index.md @@ -1,3 +1,3 @@ --- -title: IO ---- \ No newline at end of file +title: input / output +--- diff --git a/source/exercises/w_sol/past/mcq.md b/source/exercises/w_sol/past/mcq.md index 450e3235..336783a7 100644 --- a/source/exercises/w_sol/past/mcq.md +++ b/source/exercises/w_sol/past/mcq.md @@ -242,7 +242,6 @@ title: "Multiple Choice Questions (with solutions)" - [ ] Any number between 10 and 100 (both excluded) - [ ] Any number not between 10 and 100 (both excluded) - #. What will be displayed by the following code? ``` diff --git a/source/exercises/w_sol/past/sortme.md b/source/exercises/w_sol/past/sortme.md index f37fc712..8fde4431 100644 --- a/source/exercises/w_sol/past/sortme.md +++ b/source/exercises/w_sol/past/sortme.md @@ -277,41 +277,7 @@ else {Console.WriteLie(“No”);} -#. Assuming we have two **int** arrays of the same size, `firstA` and `secondA`, write a program that copies the content of `firstA` into `secondA`. -
- Solution - - ```cs - for (int k =0; k < firstA.Length; k++) - secondA[k] = firstA[k]; - ``` -
- -#. Write a static method (header included) that takes as an argument an **int** array, and display on the screen the value of each element of that array. - -
- Solution - - ```cs - public static void p(int[] a){ - foreach (int k in a) Console.WriteLine(k); - } - ``` -
- -#. 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 - - ```cs - public static void z(int[] a){ - for (int j = 0; j < a.Length; j++) a[j] = 10; - } - ``` -
- #. What sequence will appear on the output of this C# code? Which parameter of `SD(int[] A, int B)` method is passed by value? diff --git a/source/exercises/wo_sol/collections/1darrays.md b/source/exercises/wo_sol/collections/1darrays.md new file mode 100644 index 00000000..14aa3753 --- /dev/null +++ b/source/exercises/wo_sol/collections/1darrays.md @@ -0,0 +1,296 @@ + + +--- +tags: + - datatypes/collections +--- + +# One-Dimensional Arrays + +## Multiple Choices + +#. What is the correct way of creating an array of `int` of size 5 named `myArray`? + + - [ ] `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: + + ``` + int[] grades = {10, 20, 5, 15}; + Console.WriteLine(grades[2]); + ``` + + What will it display? + + - [ ] 5 + - [ ] Nothing + - [ ] 20 + - [ ] 15 + - [ ] grades + - [ ] grades[2] + - [ ] 10 + +#. Consider the following code: + + ``` + char[] grades = {'A', 'B', 'C', 'D', 'F'}; + int i = 0; + while(i < grades.Length){ + i++; + Console.WriteLine(grades[i]); + } + ``` + + 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 named `numbers`. + + +#. Write a statement that creates and initializes an array of `double` with the values 12.5, 89.0 and 3.24. + + +#. In the following, what is the value of the size declarator? What is the value of the index? + + ``` + int[] numbers; + numbers = new int[8]; + numbers[4] = 9; + ``` + + +#. What is "array bounds checking"? When does it happen? + + +#. 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. + + ``` + double[] myIncomes = new double[5]; + myIncomes[1] = 3.5; + // No income on day two. + myIncomes[3] = 5.8; + myIncomes[4] = 0.5; + myIncomes[5] = 1.5; + ``` + + +#. What would be the size of the `test` array after the following statement has been executed? + + `int[] test = {3, 5, 7, 0, 9};` + + +#. What is wrong with the following array declaration? + + ``` + int[] books = new int[-1]; + ``` + +#. Suppose we have an array named `temp` that has been declared and initialized. How can we know the number of elements in this array? + + +#. What is the value of `count` and the content of `number` once the following has been executed? + + ``` + int count=2; + int[] number={3, 5, 7}; + number[count--] = 8; + number[count]--; + ``` + + + +#. Describe what the following code would do. + + ``` + int[] record = { 3, 8, 11 }; + int accumulator = 0; + foreach (int i in record) + accumulator += i; + ``` + + +### Displaying Arrays + +#. Write code that displays the first and last element of an array. + + +#. What will be displayed at the screen by the following code? + + ``` + int[] values = new int[6]; + for (int i = 0 ; i < 6 ; i++) + values[i] = (i * 2 ); + + foreach (int j in values) + Console.WriteLine(j); + ``` + + +#. Suppose we are given an `int` array `dailyPushUp` with 7 elements. Write a piece of code that displays the value of the elements stored in the array `dailyPushUp`. + + +#. Write code that displays every element in a `numbers` array of integers. + + +#. Write code that displays every element in a `numbers` array of integers in reverse order. + + +#. Write code that displays every other element in a `numbers` array of integers (that is, every even index). + + +#. Given an array `numbers` and a variable `x`, write code that displays every value in `myArray` that is equal to or greater than `x`. + + +#. 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. + + +### Filling Arrays + +#. Given an array `myArray` and some value `x`, write code that sets the value of every array element to `x`. + + +#. Given some positive number `n`, write code that first declares an array of length `n`, then sets its contents to sequentially increasing values 1, 2, 3, ..., n. + + +#. Given an array of integers, and two integer variables `oldValue` and `newValue`; write code that replaces every occurrence of `oldValue` in the array with `newValue`. + + +#. 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. + + + +### Looking For Values + +#. Given an array `myArray` and a value `val`, write code that checks if `myArray` contains `val`. The result should be `true` if `val` occur in `myArray` and `false` otherwise. + + +#. Given an array `myArray` and a variable `x`, write code that computes the number of times `x` occurs in `myArray`. + + +#. Given an array `myArray` and two values `x` and `y`, write code that checks if `myArray` contains *either* `x` or `y`. The result should be `true` if `x` or `y` occur in `myArray` and `false` otherwise. + + +#. Given an array `myArray` and two values `x` and `y`, write code that checks if `myArray` contains *both* values `x` and `y`. The result should be `true` when both values occur and `false` otherwise. + + +#. Given an integer `myArray` and a strictly positive integer value `x`, find an array element whose value is largest while also being strictly less than `x` 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. + + +#. 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. + + + +### Manipulating Two Arrays + +#. Assuming we have two `int` arrays of the same size, `firstA` and `secondA`, write a program that copies the content of `firstA` into `secondA`. + + + +#. Given two arrays `array1` and `array2`, write a program to determine if there exists a value that occurs in both arrays. If such value exists, the result should be `true` and `false` otherwise. + + + +#. Write a program that combines two `string` arrays called `array1` and `array2` into a single array containing first all the elements from `array1`, then all the elements from `array2`. + + +#. Given two arrays `arrayA` and `arrayB`, write code to check if every element in `arrayB` occurs in `arrayA`, then display the result as `true` or `false`. + + +### 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. + + +#. 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. + + + +## Simple Algorithms + +#. Write a program that computes the sum of values stored in a `numbers` array of integers and displays it. + + +#. Given an array of positive integers, count how many even values occur in that array. + + +#. Write a program that computes the average of the elements in a `arrayP` numeric array. + + +#. Write a program that finds the largest value in an integer array `arrayP`. + + +#. Write a program that finds the smallest value in an integer array `arrayP`. + + +#. Write a program that finds the *second* smallest value in an array of integers `arrayP`. + + +#. Write code that finds the index of the first occurrence of a value `val` in an array `arrayP`. If the array does not contain the value, the result should be -1. + + +#. 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. + + + +#. 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. + + +## 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). + + +#. Consider this array of words: + + ``` + string[] words = {"voice", "effect", "day", "orange", "appliance", "fly", "cloud", "degree", "engine", "society"}; + ``` + + 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: + + ``` + string[] words = {"addition", "day", "start", "dock", "fowl", "fish", "seat", "day"}; + ``` + diff --git a/source/exercises/wo_sol/collections/2darrays.md b/source/exercises/wo_sol/collections/2darrays.md new file mode 100644 index 00000000..b3db20e3 --- /dev/null +++ b/source/exercises/wo_sol/collections/2darrays.md @@ -0,0 +1,26 @@ + + +--- +tags: + - datatypes/collections +--- + +# Two-Dimensional Arrays + +## Exercises + +#. Write a statement that creates a 2-dimensional rectangular array of `int` with 5 rows and 3 columns. + + +#. Write a statement that creates a 2-dimensional jagged array of `int` with 2 rows. The first row should contain an array containing 1, the second row should contain an array containing 2, 3. + + +#. Suppose we have a 2-dimensional rectangular array named `temp` that has been declared and initialized. How can we know the number of elements in this array? + diff --git a/source/exercises/wo_sol/datatypes/index.md b/source/exercises/wo_sol/datatypes/index.md new file mode 100644 index 00000000..022c8d31 --- /dev/null +++ b/source/exercises/wo_sol/datatypes/index.md @@ -0,0 +1,12 @@ + + +--- +title: Datatypes +--- \ No newline at end of file diff --git a/source/exercises/wo_sol/flow/index.md b/source/exercises/wo_sol/flow/index.md new file mode 100644 index 00000000..27f21559 --- /dev/null +++ b/source/exercises/wo_sol/flow/index.md @@ -0,0 +1,12 @@ + + +--- +title: Control Structures +--- \ No newline at end of file diff --git a/source/exercises/wo_sol/io/index.md b/source/exercises/wo_sol/io/index.md new file mode 100644 index 00000000..4ffb24ee --- /dev/null +++ b/source/exercises/wo_sol/io/index.md @@ -0,0 +1,12 @@ + + +--- +title: input / output +--- diff --git a/source/exercises/wo_sol/oop/index.md b/source/exercises/wo_sol/oop/index.md new file mode 100644 index 00000000..999a85c8 --- /dev/null +++ b/source/exercises/wo_sol/oop/index.md @@ -0,0 +1,12 @@ + + +--- +title: OOP +--- \ No newline at end of file diff --git a/source/exercises/wo_sol/past/mcq.md b/source/exercises/wo_sol/past/mcq.md index 2fab4f66..9b08dede 100644 --- a/source/exercises/wo_sol/past/mcq.md +++ b/source/exercises/wo_sol/past/mcq.md @@ -250,52 +250,6 @@ title: "Multiple Choice Questions" - [ ] Any number between 10 and 100 (both included) - [ ] Any number between 10 and 100 (both excluded) - [ ] Any number not between 10 and 100 (both excluded) - -#. What is the correct way of creating an array of `int` of size 5 named `myArray`? - - - [ ] `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: - - ``` - int[] grades = {10, 20, 5, 15}; - Console.WriteLine(grades[2]); - ``` - - What will it display? - - - [ ] 5 - - [ ] Nothing - - [ ] 20 - - [ ] 15 - - [ ] grades - - [ ] grades[2] - - [ ] 10 - -#. Consider the following code: - - ``` - char[] grades = {'A', 'B', 'C', 'D', 'F'}; - int i = 0; - while(i < grades.Length){ - i++; - Console.WriteLine(grades[i]); - } - ``` - - 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. #. What will be displayed by the following code? diff --git a/source/exercises/wo_sol/past/sortme.md b/source/exercises/wo_sol/past/sortme.md new file mode 100644 index 00000000..99dbcde4 --- /dev/null +++ b/source/exercises/wo_sol/past/sortme.md @@ -0,0 +1,326 @@ + + +--- +tags: + - SORTME +--- + +## Warm-up Exercises + +## Questions +#. Give two examples of instant messaging applications. + +#. How many bits are in one byte? + +#. Why are "ancient" programming languages like COBOL and FORTRAN still in use? + +#: The least significant (rightmost) bit of even numbers in binary representation is 1. ________ + +#: Commands for managing the IDE and for developing, maintaining and executing programs are contained in the menus, which are located on the menu bar. ________ + +#: Is the following statement true: 010000 < 001111? ________ + +#: You can browse the web from within the Visual Studio Community environment. ________ + +#: Visual Studio Enterprise can be used to create apps only in C#. ________ + +#: High-level computer languages are easily understood by a computer without any need of translation. ________ + +#: An assembler converts assembly language programs into machine language. ________ + +#: Is the role of CPU to govern computing devices and processes? ________ + +#: Was Windows system developed in sixties? ________ + +#: Do console applications provide visual environment? ________ + +#. Computers process data, using sets of instructions called … + +- [ ] specifications +- [ ] computer programs +- [ ] recipes +- [ ] hardware + +#. ______ Law states that every year or two, the computing power of computers doubles without any increase in price. + +- [ ] Gate's +- [ ] Moore's +- [ ] Henderson's +- [ ] None of the above. + +#. The main purpose of the AUL unit is: ______. + +- [ ] to store permanent data +- [ ] to store temporary data +- [ ] to cool the computer down and prevent overheating + - [ ] to perform calculations and logical comparisons for the computer + +#. Binary code is: ______. + +- [ ] a complex, but easy to use, modern programming language +- [ ] a series of 0s and 1s +- [ ] high-level machine language instructions +- [ ] a series of characters representing the numbers 0 to 9 + +#. The order of simplicity (easiest to hardest) to a human of the three basic types of languages is: ______. + +- [ ] high-level, assembly, machine +- [ ] assembly, machine, high-level +- [ ] machine, high-level, assembly +- [ ] machine, assembly, high-level + +#. Which of the following is *true*: C# ______. + +- [ ] is object oriented +- [ ] contains a powerful class library +- [ ] is not limited to web-based applications +- [ ] all of the above + +#. Visual C# programs are created using Microsoft's Visual Studio--a collection of software tools called a(n) ______. + +- [ ] operating system +- [ ] Integrated Programming Environment +- [ ] Integrated Development Environment +- [ ] Class Library + +#. The purpose of the Visual Studio Enterprise is to ______. + +- [ ] create a program +- [ ] run a program +- [ ] debug a program +- [ ] all of the above + +#. This menu contains commands for opening projects, closing projects, printing project data, etc. + +- [ ] View menu +- [ ] Edit menu +- [ ] Tools menu +- [ ] File menu + +#. A single line comment in C# begins with which double symbol? + + - [ ] `**` + - [ ] `//` + - [ ] `##` + - [ ] `$$` + +#. Which method is the starting point of any C# program? + + - [ ] Open + - [ ] Main + - [ ] Start + - [ ] none of these. + +#. All statements in C# end with: + - [ ] semicolon + - [ ] colon + - [ ] comma + - [ ] full stop + +#. Violations of language rules are referred to as: + - [ ] semantic errors + - [ ] syntax errors + - [ ] run-time errors + - [ ] none of these + +#. What operator is used to denote remainder (modulo) operation? + - [ ] `&` + - [ ] `%` + - [ ] `#` + - [ ] `@` + +#. Which of the following is in highest-to-lowest order of operator precedence? + - [ ] multiplication, division, parentheses + - [ ] addition, subtraction, division + - [ ] parentheses, multiplication, addition + - [ ] none of these + +#. In C# code: `Console.WriteLine($"Initial value is: {myAccount.GetNumber()}");` which method is called (executed) first? + - [ ] WriteLine() + - [ ] GetNumber() + - [ ] they are called at the same time + - [ ] none of these + +#. Which of these are binary operators? + - [ ] / (division) + - [ ] * (multiplication) + - [ ] + (addition) + - [ ] all of these + +#. You can declare the same variable twice in a C# code. + + - [ ] Yes + - [ ] No + +#. It is good to initialize all variables upon their creation (declaration). + + - [ ] Yes + - [ ] No + +#. Is = the equality operator in C#? + + - [ ] Yes + - [ ] No + +#. Visual C# programs are created using Microsoft’s Visual Studio—a collection of software tools called a(n) _____. + +- [ ] Operating system +- [ ] Integrated Programming Environment +- [ ] Integrated Development Environment +- [ ] Class Library. + +#. The starting point of a C# application is the +- [ ] Main +- [ ] Start +- [ ] Open +- [ ] None of the above. + +#. C# statements in C# usually end with: +- [ ] `:` +- [ ] `;` +- [ ] `#` +- [ ] `.` + +#. _____ are violations of language rules. +- [ ] Logic errors +- [ ] Syntax errors +- [ ] Run-time errors +- [ ] None of the above. + +#. A variable is: +- [ ] An instruction for the compiler +- [ ] A location in memory where a value is stored +- [ ] A description of a method call (including the argument list) +- [ ] None of the above. + +#. What is an algorithm? +- [ ] The actions (and their order) that solve a particular problem +- [ ] An English description of a problem to be solved +- [ ] The declaration of an object. +- [ ] None of the above. + +#. The order of simplicity (easiest to hardest) to a human of the three basic types of languages is: +- [ ] High-level, machine, assembly +- [ ] Machine, high-level, assembly +- [ ] Assembly, machine, high-level +- [ ] High-level, assembly, machine. + +#. Which of these are binary operators? +- [ ] * (multiplication) +- [ ] + (addition/concatenation) +- [ ] / (division) +- [ ] All of these. + +#. What is the correct order of actions in developing software? +- [ ] Define the problem, develop an algorithm, code a C# program, run tests +- [ ] Code a C# program, develop an algorithm, run tests +- [ ] Code a C# program, run tests, develop an algorithm. +- [ ] Define the problem, run tests, code a C# program. + +#. What is the resulting value of c at the end of the following code segment? +```cs +int c = 5; +c *= --c; +``` +- [ ] 25 +- [ ] 15 +- [ ] 20 +- [ ] None of the above + +#. What value will be printed on the output: +```cs +int A = 16; int B = 3; +Console.WriteLine(A/B); +``` +- [ ] 1 +- [ ] 5.33 +- [ ] -1 +- [ ] 5 + +#. Some compilers will automatically remove body statements from loops that do not need to be executed multiple times through a process known as _____. +- [ ] Classification +- [ ] Optimization +- [ ] Interpretation +- [ ] None of the above. + +## Problems + +#. Find 3 syntax errors in this short C# code. +```cs +int num11 =1; +int num2 ==2; +if num11>-num2) {Console.WriteLine(“Yes”);} +else {Console.WriteLie(“No”);} +``` + + + + + +#. What sequence will appear on the output of this C# code? Which parameter of `SD(int[] A, int B)` method is passed by value? + + ```cs + using System; + class Program + { + static void SD(int[] A, int B) + { + A[2] += A[2]; + B /= B; + } + static void Main(string[] args) + { + int[] A = { 0, 1, 2, 3 }; + S(A, A[2]); + Console.Write($"[{A[0]},{A[1]},{A[2]},{A[3]}]"); + } + } + ``` + + + + +#. Consider the following code: + +```cs +for (int y = 1; y <= 3; y++) +{ + for (int z = 1; z < 5; z++) + Console.Write("Scene " + y + ", take " + z + ". " ); + Console.WriteLine(); +} +``` +- How many times does the outer loop iterate (i.e., how many scenes are shot)? +- How many times does the inner loop iterates (i.e., how many takes for each scene)? +- Finally, what is the total number of iteration of the nested loops (i.e., how many takes are made, total)? + + +#. Mark the pretest loops: + + - [ ] do while + - [ ] switch + - [ ] while + - [ ] for + - [ ] if-else-if + + +#. Which of the following correctly declares and creates a two-dimensional rectangular array of integers? + + - [ ] int[,] sum = new int[10, 40]; + - [ ] int[][] sum = new int[25, 43]; + - [ ] int sum[] = new int[20, 20]; + - [ ] None of the above. + +#. C# supports two types of two-dimensional arrays: + + - [ ] quadrilateral and isosceles + - [ ] jagged and rectangular + - [ ] jagged and round + - [ ] None of the above. diff --git a/source/index.md b/source/index.md index fc0f5a20..7a1eb883 100644 --- a/source/index.md +++ b/source/index.md @@ -6,12 +6,11 @@ description: Course resources website to teach principles of computer programmin Welcome to the course resources website to teach principles of computer programming using C#. You can either: -- navigate on this website, by - - heading towards our [documentation](./docs/), [lecture notes](./lectures/) or [labs](./labs/), +- Navigate on this website, by + - heading towards our [documentation](./docs/), [lecture notes](./lectures/), [labs](./labs/), [exercises](./exercises/) or [projects](./projects/), - searching using our search bar, - - (desktop only) use the menu on your left, - - noting that hovering over most links gives a preview of the page content, -- download our book containing some documentation and all of our lecture notes in multiple formats: + - (desktop only) use the menu on your left, noting that hovering over most links gives a preview of the page content. +- Download our book containing some documentation and all of our lecture notes in multiple formats: - [HTML](./book.html) - to read from your browser, convenient for phones as well, and lightweight. - [PDF](./book.pdf) - to print and archive. - [ODT](./book.odt) - to edit (using e.g., [libreoffice](https://www.libreoffice.org/)) and archive. diff --git a/source/lectures/arrays/loop_length.md b/source/lectures/arrays/loop_length.md index e09620ee..a4c4dc4c 100644 --- a/source/lectures/arrays/loop_length.md +++ b/source/lectures/arrays/loop_length.md @@ -1,3 +1,8 @@ +--- +tags: +- datatypes/collections +--- + # Simple Loops and Length ## Custom Size and Loops diff --git a/source/order b/source/order index 4cf3bd9c..122696c8 100644 --- a/source/order +++ b/source/order @@ -59,6 +59,9 @@ ./labs/ ./exercises/ ./exercises/wo_sol/ +./exercises/wo_sol/collections/ +./exercises/wo_sol/collections/1darrays.md +./exercises/wo_sol/collections/2darrays.md ./exercises/wo_sol/oop ./exercises/wo_sol/oop/objects.md ./exercises/wo_sol/oop/oop.md @@ -69,7 +72,6 @@ ./exercises/wo_sol/past/practice_final.md ./exercises/wo_sol/past/mcq.md ./exercises/wo_sol/datatypes -./exercises/wo_sol/datatypes/collections.md ./exercises/wo_sol/datatypes/numerical.md ./exercises/wo_sol/datatypes/datatypes.md ./exercises/wo_sol/datatypes/strings.md @@ -79,6 +81,27 @@ ./exercises/wo_sol/io ./exercises/wo_sol/io/IO.md ./exercises/w_sol/ +./exercises/w_sol/collections/ +./exercises/w_sol/collections/1darrays.md +./exercises/w_sol/collections/2darrays.md +./exercises/w_sol/oop +./exercises/w_sol/oop/objects.md +./exercises/w_sol/oop/oop.md +./exercises/w_sol/oop/operations.md +./exercises/w_sol/oop/structures.md +./exercises/w_sol/past +./exercises/w_sol/past/exercises.md +./exercises/w_sol/past/practice_final.md +./exercises/w_sol/past/mcq.md +./exercises/w_sol/datatypes +./exercises/w_sol/datatypes/numerical.md +./exercises/w_sol/datatypes/datatypes.md +./exercises/w_sol/datatypes/strings.md +./exercises/w_sol/flow +./exercises/w_sol/flow/iteratives.md +./exercises/w_sol/flow/conditionals.md +./exercises/w_sol/io +./exercises/w_sol/io/IO.md ./projects/ ./projects/submission.md ./projects/character_creator/