codeasy .net
or Sign Up

Under the bridge

We all are same

The time when I would come back home into my time was getting closer every day and I decided to meet the guy from the C gang once again. I had a feeling that he still has some knowledge that I can gain.
I found him close to the old bridge, he was coding something on his laptop. As usual... Everyone I know in this world code something.
— Hi, do you remember me? — I started the conversation.
— Yo, hi man! You are that newbie from the resistance base. How is your code going? New bug every day, kid? — and he started laughing.
— Actually, I've learned a lot during last month. I know console input, for and while loops, .Net framework structure... And a lot of other things. I'm not a newbie anymore!
— .Net what? What is this shit? You know, man, I don't care. Just be careful with forbidden knowledge, kid, machines can punish you if they get this info...
— No problem with machines, I have one friend among them!
— What did you say?! — guy closed his laptop and put all his attention on me.
— Did you say "friend among machines"? Are you crazy? Why are you still alive, man? Or, maybe you are a machine spy now? Stay away, kid! I don't want troubles.
— Wow, wow. Easy! This machine is broken, it loves humanity. And it is disconnected from the GMN(Global Machine Network). It is not a dangerous machine.
— Can be... I've heard about such machines, man. Did you check it? You should give an array of numbers to that machine and ask to do some math. If it does — you can believe to this machine. This test was developed back in 2085 at the beginning of the war by a very clever woman Lillian. There is some hidden logic that prevents evil machines from passing this test. But to apply it you need to know arrays. Do you?
— Not at all...
— Yo! Listen, man!

c# arrays joke

Arrays

— When you were studying, did your teacher have a list of all students in the class with their marks? — the guy asked.
— Yes, there was one.
— Yes, man! You got me. In programming, you would need to use array data structure to store multiple variables of the same type. To create an array of integers, you should use the new keyword and specify a type, name, and a size of the array:

int[] studentMarks = new int[10];

— It also works if the array size is a variable.

int studentsCount = 10;
int[] studentMarks = new int[studentsCount];

— Is this a list of 10 integers? — I asked.
— Yes, kid. In memory it will look similar to this:

c# array declaration

— Creating such array is the same as creating 10 int variables, — the guy explained.
— Hm... This is interesting. To create an array I need to declare a type of elements, then add [], then specify a name and assign to it a new array with the size inside. Quite a few things to remember.
— One more, kid. After you create an array — it is filled with a default value of its type. The default value for int is 0, that's why the array is filled with zeros.
— Clear. But how to fill an array with values other than 0? — I asked.
— To access any cell of the array you should specify its index. Indexing of arrays in C# starts with 0 and increases by 1 for each next cell. Here is a code to assign 99 to the first array cell:

int[] studentMarks = new int[10];
studentMarks[0] = 99;

— I marked cells indexes on the picture. Now, the memory will look like this:

c# array declaration

— Do you follow, man? Or they didn't tell you such complicated things in your resistance base?
— I follow you, continue.
— To fill all cells of the array you need to do the following, and the memory changes as well:

int[] studentMarks = new int[10];
studentMarks[0] = 99;
studentMarks[1] = 63;
studentMarks[2] = 54;
studentMarks[3] = 12;
studentMarks[4] = 33;
studentMarks[5] = 88;
studentMarks[6] = 72;
studentMarks[7] = 67;
studentMarks[8] = 94;
studentMarks[9] = 5;

c# array declaration

— There also exist a shortened version to fill the array if you know all the values at the time of its creation. The following line of code does exactly the same as the code in the previous example:

int[] studentMarks = new int[10] { 99, 63, 54, 12, 33, 88, 72, 67, 94, 5 };

— Wow, this looks much shorter! — I said.
— Yes, I like more this one too.
— Can you show me how to read values from the array?
— No problem, man! To read values from the array you should specify the cell index after the array name. Same as you did with writing values to array:

int[] studentMarks = new int[10] { 99, 63, 54, 12, 33, 88, 72, 67, 94, 5 };
Console.WriteLine(studentMarks[1]);
// Outputs: 63
int[] studentMarks = new int[10] { 99, 63, 54, 12, 33, 88, 72, 67, 94, 5 };
int copyOfSecondElement = studentMarks[1];
Console.WriteLine(copyOfSecondElement);
// Outputs: 63

Create an array of integers of size 5. Don't fill the array with any values. Output to console the default values of every cell, each from the new line.

Arrays: one more joke

— Cool kids usually use loops with arrays. Look here, I've written a program that reads the number of students from the console, then creates an array of student marks and pre-fills it with 10, because each student gets 10 points in the beginning. At the end program prints points of every student, — guy showed me the screen of his laptop.

int studentsCount = int.Parse(Console.ReadLine()); 
int[] studentMarks = new int[studentsCount];
            
for(int i = 0; i < studentsCount; ++i)
    studentMarks[i] = 10;
                
for(int i = 0; i < studentsCount; ++i)
    Console.WriteLine(studentMarks[i]);

Read an integer count from the console. Then create an array of integers of size count. Fill the value of every cell of the array with its index value. Output every cell of the array to the screen, each from the new line, starting from the cell with index 0. Example:
>4
0
1
2
3

— Good job, man! You can create arrays of different types and store values inside and read them same as you did with int arrays:

double[] myDoubleArray = new double[5];
myDoubleArray[2] = 2.15;
            
float[] myfloatArray = new float[5];
myfloatArray[2] = 2.15f;

string[] myStringArray = new string[5];
myStringArray[3] = "Hello!";
            
char[] myCharArray = new char[5];
myCharArray[1] = 'a';

To get the length of the array you can read its Length property. For example, if you've created an array myArray of length 5, then myArray.Length will return you a value 5. You can use it if someone else has created an array, and you just got it in your program, and don't really know what was the size when the array was created. I need to go, see ya, man! Solve all these tasks and take care!

double[] myArray = new double[5];
Console.WriteLine(myArray.Length);
// Outputs: 5

Call a predefined method GetNumbersFromConsole that will read from the console a count of double values and return all those values in a double array. Create another array and copy to it all the values from the first one. Output every value from the new line. Example:
>3
>1.12
>-2.333
>399.76
1.12
-2.333
399.76

Call a method GetNumbersFromConsole that will read from the console a count of double values and return all those values in a double array. Find a maximum number in the array and print "Max is: {number}". Example:
>3
>1.12
>-2.333
>399.76
Max is: 399.76

Call a method GetNumbersFromConsole that will read from the console a count of int values and return all those values in an int array. Print all values of the array in one line in reversed order separated by a space. You should NOT have space at the end of the line to complete this task. Example:
>3
>1
>2
>3
3 2 1

Call a method GetNumbersFromConsole that will read from the console a count of int values and return all those values in an int array. Calculate and print the sum of all elements of this array in a format "Sum: {number}". Example:
>3
>1
>3
>2
Sum: 6

Call a method GetNumbersFromConsole that will read from the console a count of int values and return all those values in an int array. Separate big and little integers into two separate arrays. Integer is big if it is bigger than 100. Otherwise, it is little. Then output first big and after — little numbers in two lines in a format: "Big: {numbers}" and "Little: {numbers}". Separate numbers by space, and pay attention to NOT have space at the end of the lines. Example:
>4
>101
>756
>3
>255
Big: 101 756 255
Little: 3

Arrays: one more joke

This site uses cookies. By continuing to browse this site you are agreeing to our use of cookies.

Got it!