November 14, 2020
Remember to prepare for our final exam! For that, you can use this “definitive” study guide or this list of additional problems. The fifth problem is asking you to generate random numbers, a topic that will not be on the exam, but is a useful knowledge that was introduced in one of the tip for the second project.
This exercise is to practise for loops if you feel the need to.
Write a program that asks the user to enter a positive integer, and then uses a for loop to compute the sum of all the integers between 1 and the integer given by the user. For instance, if the user enters 5, your program should display 15 at the screen (i.e., 1 + 2 + 3 + 4 + 5 = 15).
Then, answer the following questions:
while loop?You can modify your program to check your answers to the previous questions. Once you are done, modify your original program with two respects:
This problem is not easy, and require to generate random numbers, something that was sketched in the first hint of the third problem of the second project on this page.
Write a program that
int of size 8,An example of execution of this program would display:
0 8 L
5 3 W
3 3 T
1 2 L
3 1 W
9 0 W
9 0 W
1 5 L
Where the first array contains “0 5 3 1 3 9 9 1” and the second, “8 3 3 2 1 0 0 5”.
For this exercise:
ArrayLib.cs and Program.cs.Program.cs: this is a test program that you should not modify. It will be useful to test the methods that you will be writting in the ArrayLib.cs class file. For each method, this program display the expected value, and what is actually returned. As you can see, only the Display method seems to be always correct.ArrayLib.cs. Every method listed in Program.cs has a header, but all the bodies are returning “default” values or do nothing, at the exception of Display. This last method was written for you.Your goal is to write the body of the methods in the ArrayLib class. You should not change their headers. Modify only their bodies, so that they return the “right” values, according to their description (in comment after their headers) and the test given in Program.cs. You can chage their order, or write them in any order: some of them are actually easier to write, and they are not the first ones: can you find a method that seems easy enough to start your project?
If you have the time and interrest, have a look at the challenges offered at the end of the ArrayLib.cs file.
Find at in this project a possible solution to the questions asked.
For this lab, we will introduce only one notion, but a crucial one: the difference between value and reference types. This topic is introduced in your textbook (“Value Types vs. Reference Types”, Chapter 7.17), and will be studied in CSCI 1302. You can have a look at https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/data-types/value-types-and-reference-types: this page is not so complex, a short (and excellent) read.
Let us motivate why this notion is so critical with an example:
int[] arrayA = { 1, 2, 3, 4, 5 }; // Let me declare a simple array of integer
// I'd like to make a copy of that array. Let me try the following:
int[] arrayCopyWrong = arrayA;
foreach (int i in arrayCopyWrong)
Console.Write(i + " ");
Console.WriteLine();
// It seems to be working! Except that if we change a value in our copy:
arrayCopyWrong[0] = 6;
// It also changes the value in our original array!
foreach (int i in arrayA)
Console.Write(i + " ");
Console.WriteLine();What happened is that we copied the reference to the array, and not the array itself. We now have two ways of accessing our array, using arrayA or arrayCopyWrong, but still only one array.
To perform a copy of the array, we need to do something like the following:
int[] arrayB = { 1, 2, 3, 4, 5 };
int[] arrayCopyRight = new int[arrayB.Length];
// We copy each value, one by one:
for(int i = 0 ; i < arrayB.Length; i++)
arrayCopyRight[i] = arrayB[i];
// If we change a value in our copy:
arrayCopyRight[0] = 6;
// It changes the value only in that copy:
foreach (int i in arrayB)
Console.Write(i + " ");
Console.WriteLine();
foreach (int i in arrayCopyRight)
Console.Write(i + " "); }Array is actually a class (cf. https://msdn.microsoft.com/en-us/library/system.array(v=vs.110).aspx), and as such provides several methods. For x an int, array1 and array2 two arrays containing the same type of values and of size at least x, you can copy the first x values of array1 into array2 using Array.Copy(array1, array2, x);. Try to use this method with the previous example.