SEC S20W02 - Basic Programming Course: Lesson #2 - Variables and Types of Data
This is my homework post for Steemit Engagement Challenge Season 20 Week 2 of Professor @alejos7ven’s class, Basic Programming Course: Lesson #2 - Variables and Types of Data.
Note :
- I performed this task on Windows 10 PC, Google Chrome.
Task 1 – What Are Variables And What Are They Used For?
What Are Variables
According to what I read from Professor @alejos7ven's article, Variables can be likened to “categories of information”, e.g. age
, sex
, favorite_comedy_show
, the_most_boring_movie_one_ever_seen
, which for each category of information can be filled with different values or data depending on the target who fills in the data or values for each category of information. I like the analogy of a box with a sticker that says “Name” and a group of people put a piece of paper with their name on it into the “Name” box. In this analogy, the box is like a Variable in programming, while the pieces of paper containing the names inside the name box are the values or data.
From the simple analogy of the “Name Box”, my imagination grew, although not far enough because I was afraid that if I went too far I would get lost in the wilderness of Programming which I thought was too complicated, but it turned out to be analogized so easily and simply by Professor. Coupled with the results of my search on the internet, I concluded that variables in the world of Programming are places to store data or valuesData and values stored in variables can be used repeatedly in the program according to the needs and characteristics of the program.
What Are Variables Used For?
Variable in Programming is used for collecting and storing data or values. The data and values are to be accessed and used and “manipulated” in the program whenever needed. If I had to describe it, maybe I could mention 3 uses of variables in programming:
- collecting and storing data,
- re-present data when needed, and
- data processing, for example getting the area of a building based on the length and width data inputted.
Task 2 - Assign a type of data to the following variables and explain why: email
, phone
, working_hours
, price_steem
, and age
.
- Variable : email
Type of data : String (Character)
Explanation : In programming, String or Character is a data type that can represent text, numbers, and various characters such as symbols -even in certain cases- including spaces. String or Character considers and treats all the numbers in it as characters and not numeric. - Variable : phone_number
Type of data : String (Character)
Explanation : At first glance, I thought that phone numbers should be stored as integers, because phone numbers are strings of numbers. But after I searched the internet, it turns out that phone numbers are generally stored in the form of String or Character data, with several reasons or considerations, including:- Phone numbers are not used for mathematical operations (addition, subtraction, multiplication, division).
- Phone numbers can contain non-numeric special characters [+].
- In addition, the selection of integer data types to store them in the program instead of real data types is also due to memory saving considerations as mentioned by Professor in his article, that for similar data strings, integer data types take up less memory than real data types.
- Variable : working_hours
Type of data : String (Character) or Integer
Explanation : Working hours can be written in String (Character) or Integer data type, it depends on the nature of the data usage of the working hours.
If the working hours information will only be stored as information that does not involve mathematical operations, e.g. to express the hours of entry and exit (09:00 - 17:00), then it is safe to choose to store it as text (String or Character).
If the working hours information is intended to be used in mathematical operations (e.g. calculating the number of working hours of an employee during a certain period of time) then the data must be written in Integer type. Even in certain cases when the calculation involves decimals, then the appropriate data type to be used is Real. - Variable : price_steem
Type of data : Real
Explanation : STEEM prices will almost always involve decimals, so the correct data type for this type of variable is Real. - Variable : age
Type of data : Integer or Real
Explanation : The most likely to be used in this variable is the Integer data type because a person's age is usually measured in whole numbers. But is is possible of existance of cases where program wants to measure age in decimal numbers, for example someone who is 33 years and 9 months old will be written as 33.75, although this may be very rare, then if this is the case, the correct data type to choose is Real.
Task 3 - Explain how the following code works:
Algorithm names
Define name, last_name as Character;
Print "Enter your name:";
Read name;
Print "Enter your last name:";
Read last_name;
Print "Hello " name " " last_name ", welcome";
EndAlgorithm
The code works to collect data in the form of the user's first and last name (types of data for both is Character (or "String" in some other languages)) to then generate a greeting message that begins with the word Hello
followed by the user's full name
(first name
followed by last name
respectively), ended with the word welcome
. For example, a user fills in his first name Phoebe
and last name Buffay
, then the program will execute and display a message on the screen that reads Hello Phoebe Buffay, welcome
.
Task 4 - Develop a pseudo-code to calculate the value in USD of X STEEM. For this you can rely on this algorithm:
- Declare variables price_steem, total and amount_steem.
- Request the user the necessary data.
- Calculate as follows: total = amount_steem*price_steem.
- Show result on screen.
There are two possible scenarios that I can think of to do this task: the first scenario is that the price_steem
has been determined in the program (algorithm) and the second scenario is that the price_steem
is entered manually by the user. Note: I will name the algorithm calculator_steem
.
For Scenario One, where the STEEM price has been set in the algorithm, the full algorithm is as follows:
Algorithm calculator_steem
// 1. Create variables
Define price_steem, total, amount_steem As Real;
// 2. Define STEEM Price in USD
price_steem = 0.16
// 3. Ask the user for amount of STEEM
Print "Enter Amount of STEEM:";
Read amount_steem
// 4. Calculate total price
total = price_steem * amount_steem
// 5. Show Total Price
Print “Total price for “, amount_steem, “ STEEM is “, total
EndAlgorithm
For Scenario Two, where the STEEM price is asked to the user, I only need to change the second step in the algorithm above, so the complete algorithm becomes:
Algorithm calculator_steem
// 1. Create variables
Define price_steem, total, amount_steem As Real;
// 2. Ask the user for price of STEEM
Print “Enter Price of STEEM:”;
Read price_steem
// 3. Ask the user for amount of STEEM
Print "Enter Amount of STEEM:";
Read amount_steem
// 4. Calculate total price
total = price_steem * amount_steem
// 5. Show Total Price
Print “Total price for “, amount_steem, “ STEEM is “, total
EndAlgorithm
My imagination of how this algorithm will look in the practice area as an applications when execute on a smart phone:
Conclusion
One of the points that I captured from the previous week's lesson is that Psedo-code really helps people understand programming because it becomes a kind of “bridge” between computers and humans to be able to “communicate” with each other, providing a language structure that is more “friendly” to humans, while not destroying the structure of a very complex computer language.
In line with that, in this second week it has increasingly felt true, it can be seen how Professor alejos7ven has succeeded in bringing things that might look complicated to ordinary people but now look easy, even for someone like me who started from having no knowledge whatsoever about programming.
In creating a simple algorithm as emphasized in this week 2 lesson, I draw the conclusion that to successfully write an algorithm, one must understand, at least:
- the structure of the algorithm;
- the types of data and which data is suitable for an algorithm (very much related to the purpose of the algorithm).
Thanks
Thanks Professor @alejos7ven for the lesson. I really appreciate the opportunity.
Pictures Sources
- The editorial picture was created by me.
- Unless otherwise stated, all another pictures were screenshoots and were edited with Adobe Photoshop 2021.
Sources and Reading Suggestion
- https://launchschool.com/books/ruby/read/variables
- https://www.geeksforgeeks.org/variables-programming/
- https://en.wikipedia.org/wiki/Variable_(computer_science)
- https://www.tutorialspoint.com/computer_programming/computer_programming_variables.htm
- https://www.techtarget.com/whatis/definition/variable
- https://www.bbc.co.uk/bitesize/guides/zwmbgk7/revision/2
- https://press.rebus.community/programmingfundamentals/chapter/data-types/
- https://www.indeed.com/career-advice/career-development/data-type-examples
- https://www.studysmarter.co.uk/explanations/computer-science/computer-programming/data-types-in-programming/
- https://www.geeksforgeeks.org/data-types-in-programming/
- https://en.wikipedia.org/wiki/Data_type
https://x.com/aneukpineung78/status/1836993765325906277
@hamzayousafzai, are you participating in this one, Bro?
Reading this article my memory immediately went back 15 years when I was studying further in Semarang. At that time, I often used funding programs like this for my thesis materials
I like how Spreadsheet handles data given to it with commands. I think in a way, that's how programming works.
Good to know. Did it also involved programming skills, Chief?
Hahahaha.. At that time I was really new to Spreadsheets and applying them
Thanks, SS Team.
Your post has been rewarded by the Seven Team.
Support partner witnesses
We are the hope!