Hi guys.
Since many people seem to want to learn coding and are trying their best at it, I thought of a fun little exercise to try in order to improve our understanding of what programming means, how to apply concepts with code examples and how to explain your code to someone else. For me atleast, explaining helps me a lot if I want to understand something.
So, paste here the last piece of code that you have written and try to explain, using your own words, what it actually does. The programming language doesn't really matter, the more examples we receive the better since everyone can try to convert the code in their prefferred language.
I am a beginner myself and my teacher keeps me bussy with fun little exercises that keep my head spinning so, if there's interest for this topic, I will probably post pieces of code and try to explain them.
Without further delay, here is the latest code that I have worked on, written in c#:
Code start:
Random r = new Random();
int[] numbers = new int[10];
int sum = 0;
for (int i = 0; i < numbers.Length; i++)
{
numbers[i] = r.Next(1, 101);
}
foreach (int f in numbers)
{
sum += f;
}
string n = string.Join("+ ", numbers);
MessageBox.Show(n + "is equal to " + sum.ToString());
code end.
Okay, let me try to explain it: First, I declare 3 variables. A random variable an array of integer type and a simple integer. Then, I make a for that checks the array's length in order to be able to assign something to it. In that for, I tell it, by using the random variable, to assign a random number between 1 and 100 to each of the array's elements.
After that, I declare a foreach loop and tell it to add each element to the next one thus allowing me to know the sum of all the 10 random elements declared in the array. Next, for beauty's sake, I make a new string that joins the plus symbol with the array adding it between each number.
Finally, I create a message box and call the show function in which I tell it that the joined string called n is equal to the integer called sum, which stored the final value of the operation.
The result will be 10 numbers with the plus sign between them followed by the message is equal to and the result.
I hope this topic proves to be instructive for all programming newbies and I urge you to try submiting a piece of code and explaining it, it will really help, atleast it does help me understand what's happening behind the scenes in order to learn to think like a programmer.
If any advanced developers want to chime in they are more than welcome to submit and explain their code, as well as correct any mistakes made or provide better explanations.
Stephen King