codeasy .net
or Sign Up
C# Elementary > 2 The Mission > Computer hardware introduction

The Arrival

I woke up because of people talking next to my bed. One of them took two steps in my direction.

"Hi, my name is Ritchie. I am the leader of this resistance base. Do you want coffee?"
"Oh yes! I would like some coffee. And a little bit of explanation. Is Noname a program? Did you create it?" I had lots of questions in my head.
"Yes, Noname is a program, and yes, I wrote it," Ritchie answered calmly. "One of Noname's purposes was to bring you here."
"OK, what are our plans then?"
"We need to prepare you for tomorrow's mission." His answer was short and precise, but not informative enough.
"You say mission… In my current state, I'm not up for any missions. Do I have a choice to avoid it?" I asked hopefully.
"Absolutely no chance. The mission starts tomorrow at 11:00 pm," Ritchie said with a note of finality, "and here is your coffee, by the way," he added, passing me a cup.
The coffee was not that good, without milk and probably very old, but it was still coffee. Ritchie continued:
"You need to understand how computers are built to have at least a basic understanding of how programs work. Are you ready to dive in?"
"Give it to me!"

Study Computer Science

Computer Guts

"The main function of any computer (PC, laptop, tablet, smartphone) is to calculate numbers and store the results. It is not much more than that! Calculate and store."
Ritchie was pulling out parts of the computer and showing me one by one.
"Storage devices usually look like this:"

RAM

SSD_HDD

"It all depends on memory and device type. We are not going to dive deeply into this right now, but it is good for you to understand that when you use computer memory — it is one of these pretty devices," explained Ritchie.
"A processor makes the computer's calculations. It looks like this:"

SSD_HDD

You have more than one processor in a computer, but we will mostly work with the CPU, which means Central Processing Unit. Something like “the main guy” in a processor society."
Ritchie continued, "Programming is very abstract, especially in 2113, and I want to make things as clear as I can for you. That’s why I am showing you these things, in order to bind your knowledge to something that you can see. If you enter the pilot's cabin in a plane you will see a lot of different devices, right? But to use them, you need a pilot. In computers, the pilot is an operating system. An operating system is a significant and smart program that combines the work of all the hardware devices and gives you the possibility to work with a computer. The most popular humanly-created operating systems are MacOS, Windows, Linux and Android. Have you heard about them?"
"Who hasn't? Don’t ask me stupid questions, please. Are you sure I need to know this?" I asked skeptically.
"Does a dermatologist need to know the structure and functions of the stomach?" Ritchie inquired.
"Of course! Skin problems could be connected to problems with the stomach," I said.
"It's the same with programming. Everything is connected. By the way, I’ve got news about the police drones. They are scanning that sector less often. You were very “lucky” to meet them! Did you enjoy using C# to reprogram drones?"
"Yes, but there was no other option."
"Hm… You could actually choose any other language… But C# has so many advantages that I totally understand your choice," Ritchie smiled.
"What advantages?" I wondered.
"OK, here are several of C#'s advantages that I would like to mention:" He ticked off a list on his fingers...

  1. Easy to learn and widely used. With C# it is possible to write games, desktop applications, web sites and services.
  2. C# has a big community and helpful documentation.
  3. Has a lot of solutions “out of the box.” No need to solve many tasks that were already solved before you.
  4. Cross-platform — using the technology of .Net Core, C# programs run on various operating systems and even on different devices: desktop computers, laptops, phones, etc. You can write applications for Android, iOS, Windows, Linux; and you can create websites in C#!

"I think I have an image about C# in my database. Oh, yes, here, take a look:"

C# advantages

"Can you give me an example of an “out of the box” solution in C#?" I asked.
"Here you are... In many other programming languages, to concatenate int variables and strings, you need to use a plus sign (+), like this:

int applesCount = 5;
string result = "I have " + applesCount + " apples";

The output is:

I have 5 apples

If you concatenate an int variable and a string — you will get the integer number converted into a string and then concatenated with the whole string.
"Just the same as string concatenation, including the int variable in the string?" I asked.
"Yes. This looks nice, unless you have many variables and a lot of concatenations:

int applesCount = 5;
int orangesCount = 10;
int bananasCount = 3;
string result = "I have " + applesCount + "apples, " + orangesCount + "oranges and also " bananasCount + " bananas.";
Console.WriteLine(result);

Now the output is:

I have 5 apples, 10 oranges and also 3 bananas.

"Do you like how the code looks now?" Ritchie asked.
"More or less, but now it is much harder to visualize the final result, because of the separate chunks of strings joined with plusses," I replied.
"I think so, too! C# gives the option to rewrite that code snippet this way:

int applesCount = 5;
int orangesCount = 10;
int bananasCount = 3;
string result = $"I have {applesCount} apples, {orangesCount} oranges and also {bananasCount} bananas.";
Console.WriteLine(result);

Outputs:

I have 5 apples, 10 oranges and also 3 bananas.

"How do you feel about this code now?" Ritchie asked.
"It is more readable because the concatenations are accomplished with nothing more than curly brackets. I can read it as a sentence!" I exclaimed.
"Exactly. This is one little example where C# has a built-in mechanism to make your life easier! This technique of injecting integers (and strings and other types as well) in strings is called strings interpolation. You can read more about it here.
To use strings interpolation, you should start your string with a dollar sign $ and put every variable that you want to insert into the string in the curly brackets: {variable}.
Now it is time to practice. Convert the caffeine in your blood into code, as many programmers do! Here is your first assignment:"

Create 4 int variables with different names. Assign them these values: 1000, 2000, 3000, 10000. Using string interpolation, insert those int variables into the strings to get such output:
In one year my salary is going to be 1000 USD
In two years my salary is going to be 2000 USD
In three years my salary is going to be 3000 USD
In four years my salary is going to be 10000 USD


Now for a more practical application. The biggest machine known to us is a huge cube, measuring 317 meters in each direction, and it is called “Dominator.” To stop Dominator, we plan to paint it with a machine-destroying paint. Write a program that calculates how much paint we will need to totally paint all six sides of Dominator, if painting 1 square meter takes 20 grams of paint. You have studied geometry, haven't you? Output the result using string interpolation in the format:

I need ** grams of paint.


Here is a final task that may come in handy. Some drones are not so easy to reprogram immediately, so they may continue sending reports to the drone base. To prevent this, you can substitute false reports. You have to output to the screen: "I can see ** people in this square" and then decrement the number by 1. You have to repeat this until the number of people is equal to 0. For example:
I can see 2 people in this square
I can see 1 people in this square
I can see 0 people in this square
Try it yourself, but starting with 5 people at the beginning.
Use int variables and strings interpolation.

Nice work!

C# Elementary > 2 The Mission > Computer hardware introduction

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

Got it!