SEC S20W03 || Basic Programming Course: Lesson #3 Operations

in hive-103393 •  last month  (edited)

This is my homework post for Steemit Engagement Challenge Season 20 Week 3 assignment of Professor @alejos7ven’s class, Basic Programming Course: Lesson #3 Operations.

secs20w03-ale7-cvr.png

Note :

  • I performed this task on Windows 10 PC, Google Chrome.

Task 1 - Give a brief summary of what arithmetic, comparison and logical operations are for.

Arithmetic Operation

Arithmetic - in terms of terms - is arguably the most fundamental branch in mathematics. Arithmetic covers all the basic operations on numbers, so it would not be wrong to say that it is the foundation of all mathematical operations. These operations are: 1) addition; 2) subtraction; 3) multiplication; 4) division, and 5) advanced operations (e.g. scaling, modulus, and floor).

It can be said that it is impossible for a human being to escape arithmetic in his life. Examples of human actions that fall into the category of arithmetic are: calculating the number of hours worked, multiplying the number of hours worked into the total salary to be paid, calculating the remaining debt according to the initial amount of debt and the amount that has been paid and the interest charged, organizing work schedules (for example allocating the use of daily time to achieve targets at the end of the week).

Likewise, in programming, arithmetic is used for the same purpose, which is to perform basic math operations. The user will enter numbers and the program will perform the calculations. This was briefly discussed in week two's lesson when we calculated the price of a certain amount of STEEM in USD. At that time we used the multiplication function of arithmetic.

Below is an example of using arithmetic in programming, covering almost all simple arithmetic operations. In this example I get creative by going a little different with the example given by Professor Alejos7ven in his lectur:

  • I change the variables n and n2 with x and y, respectively;
  • I change some display messages;
  • in his lecture, Professor defined the values of the numbers by default in the algorithm, then in this example I let the users specify the numbers they want;
  • I add exponential operation in this example;
  • I put all these 2-variable arithmetic operations into one algorithm, so that for any two numbers entered, there will be results for 6 arithmetic operations all togehter.
Algorithm Arithmetic_Ops

    // 1 - Declare variables;
    Define x, y, addition, subtraction, multiplication, division, module  As Real;
    
    // 2 – Ask user for input values
    Print "Enter the value of x: "
    Read x
    Print "Enter the value of y: "
    Read y
    
    // 3 – Addition
    addition = x + y;
    Print "The result of “ x “ + “ y “ is " addition;
    
    // 2 - Subtraction
    subtraction = x - y;
    Print "The result of “ x “ - “ y “ is " subtraction;

    // 3 - Multiplication
    multiplication = x * y;
    Print "The result of “ x “ * “ y “ is " multiplication;

    // 4 - Division
    division = x / y;
        If y <> 0 Then
    Priint "The result of “ x “ / “ y “ is " division;
        Else
    Print "Division by zero is not allowed."
        EndIf
    
    // 5 - Modulus (remainder)
    module = x % y;
    Print " The remainder of dividing “ x  “ with “  y “  is " module;
    
    // 6 - Exponentiation
    exponent = x ^ y;
    Print "The result of “ x “ raised to the power of “ y “ is “ exponent; 
EndAlgorithm

Through my internet search I found that, in line with the law of arithmetic that numbers are not divisible by 0, it is emphasized in the algorithm by inserting the function

        If y <> 0 Then
    Print "The result of “ x “ / “ y “ is " division;
        Else
    Print “Division by zero is not allowed.”
        EndIf


This function serves as a command for the program to check whether the divisor (y) has a value equal to 0, if the divisor (y) does not have a value equal to 0, then the division is performed and the result is displayed, if the divisor (y) is 0 then the message Division by zero is not allowed. is displayed. The command to end If command is EndIf.

When a user inputs data x is 78 and y is 4, then the program will display result as follow:

The result of 78 + 4 is 82;
The result of 78 - 4 is 74;
The result of 78 * 4 is 312;
The result of 78 / 4 is 19.5;
The remainder of dividing 74 with 4  is 2;
The result of 74 raised to the power of 4 is 37,015,056; 

Comparison Operation

As the name implies, the Comparison Operation is an operation that compares two values and returns a boolean result (TRUE or FALSE). This operation is often used in conditioning, repetition and logic. Professor Alejos7ven has talked about this quite a bit in his lectures, but I'll go back and note some of the commonly used types of comparison operations, which are:

  • Equal to (==), this operation checks if two numbers have the same value. For example x is 78 and y is 78, then x == y will return TRUE. But if y is 47, then the result of this operation is FALSE.
  • Different from (!=), this operation checks whether two variables have different values, if they are, then the result is TRUE, and FALSE will be the result if the values of the two variables being compared are the same.
  • Greater than (>), this operation checks whether the value to the left of the > sign is greater than the value to the right of it. Example: the operation 78 > 47 will result in TRUE.
  • Less than (<), this operation is the opposite of the greater than operation, TRUE will result if the number to the left of the < sign is smaller than the value to its right. For example: 78 < 47 will result in FALSE while 47 < 78 will result in TRUE.
  • Greater or equal to (>=) will return the result TRUE when the value to the left of the >= sign is greater than or equal to the value to the right of it. Example: 78 >= 47 will return TRUE in result, while 47 >= 78 will return FALSE in result.
  • Smaller or equal to (<=), works the opposite of the above comparison operation. It will return a TRUE result when the value to the left of the <= sign is smaller or equal to the value to the right of it. The example 47 <= 78 will return the result TRUE, as will 47 <= 47, but the result FALSE will be returned to 87 <= 47.

In a scenario where the values of x, y, and z are 47, 78, and 47 respectively, the algorithm for the comparison operation for the data is :

Algorithm Comparision_Operation
    // 1 - Declare variables;
    Define x, y, z As Integer;
    x=47;
    y=78;
    z=47;

    // 2 – Equal to
    Print x “is equal to “ y “ ? ” x==y;
    Print x “is equal to “ z “ ? ” x==z;

    // 3 – Different from
    Print x “is different from“ y “ ? ” x!=y;
    Print x “is different from“ z “ ? ” x!=z;

    // 4 – Greater than
    Print x “is greater than“ y “ ? ” x>y;
    Print x “is greater than“ z “ ? ” x>z;

    // 5 – Less than
    Print x “is less than“ y “ ? ” x<y;
    Print x “is less than“ z “ ? ” x<z;

    // 6 – Greater or equal to
    Print x “is greater or equal to “ y “ ? ” x>=y;
    Print x “is greater or equal to “ z “ ? ” x>=z;

    // 7 – Smaller or equal to
    Print x “is smaller or equal to “ y “ ? ” x<=y;
    Print x “is smaller or equal to “ z “ ? ” x<=z;
EndAlgorithm

Yes, I realize that in the lecture, Professor has chosen the data type Real for all the data in the comparison operation example, but here I have chosen the data type Integer, hopefully Professor will make a special note of this, as he did in grading the previous weeks' work.

If the above algorithm is executed, then the result is:

47 is equal to 78? FALSE
47 is equal to 47? TRUE
47 is different from 78? TRUE
47 is different from 47? FALSE
47 is greater than 78? FALSE
47 is greater than 47? FALSE
47 is less than 78? TRUE
47 is less than 47? FALSE
47 is greater or equal to 78? FALSE
47 is greater or equal to 47? TRUE
47 is smaller or equal to 78? TRUE
47 is smaller or equal to 47? TRUE

Logical Operation

According to what I read from Professor's lecture, coupled with my findings on the internet, I concluded that logical operations are operations used to evaluate 2 comparative operations or boolean statements or expressions (expressions that produce a value of TRUE or FALSE), logical operations will produce a value of TRUE or FALSE based on the evaluation. Logical operations use logical operators, namely:

  • AND (&& or and). In this operation, if both statements are TRUE then the result returned is TRUE, if any or all statements's value is FALSE, then this operation will return the result FALSE.
  • OR (|| or or). This operator requires one of the statements to be TRUE for the operation to return a TRUE result. If all statements are FALSE, then the operation will return a FALSE result.
  • There are some other logical operators like NOT and XOR.

Logical operations are used as one of the guidelines for decision-making. Case in point: to play level 78 in a game, there are two variables that are determining factors: 1) level 47 completion; 2) gamer’s sexuality (gender). With these two variables, then:

  • AND logical operation will require both statements to have the value TRUE so that the logical operation will return the value TRUE, meaning that a gamer can only play level 78 (TRUE) if he is a male (TRUE) and has completed level 47 (TRUE). If one or both of the statements’ value is (are) FALSE, for example a gamer is a woman and/or has not completed level 47, then the logical operation will return the value FALSE (they cannot play level 78).
  • The logical operation OR requires at least one of the statements to have the value TRUE for the logical operation to return the value TRUE, meaning that if a gamer has completed level 47 then they can play level 78 (regardless of his gender) or if he is a male gamer then he can play level 78 without having to complete level 47 first. Only in the case where both statements are FALSE (a gamer is not male and has not completed level 47) will the logical operation OR return the value FALSE (she cannot play level 78).

An example program to check the values of the two operations AND and OR based on the sample data implemented into a pseudo-code algorithm is as follows:

Algorithm PlayLevel78
    // 1 - Declare variables;
    Define gender As Character;
    Define level47Completed As Character;
    Define is_male, is_completed As Logical;

    // 2 - Ask for input from the user;
    Print "Enter your gender (male, female): "
    Read gender;
    Print "Have you completed level 47? (yes/no): ";
    Read level47Completed;
    
    is_male = gender==male;
    is_completed = level44Completed==yes;

    // 3 - AND
    Print "Does the user meet both conditions? " is_male and is_completed;

    // 4 - OR
    Print "Does the user meet at least one condition? " is_male or is_completed;
EndAlgorithm


When a user fills in the gender data male and no in the level47Completed variable, the program will display the following message:

Enter your gender (male, female): 
> male
Have you completed level 47?:
> no
Does the user meet both conditions? FALSE
Does the user meet at least one condition? TRUE


If the data entered is female and no, then:

Enter your gender (male, female): 
> female
Have you completed level 47?:
> no
Does the user meet both conditions? FALSE
Does the user meet at least one condition? FALSE

dividerSECS-s20.gif

Task 2 - Make a program that asks the user for 2 numbers and evaluates if both numbers are the same.

Algorithm Check_numbers_equality
    // 1 -  Declare variables;
    Define x, y As Integer;
    
    // 2 - Ask user to fill in the numbers;
    Print "Enter a number: ";
    Read x;
    Print "Enter another number: ";
    Read y;

    Print "Is " x " equal to " y "? " x==y;
EndAlgorithm


If a user enters the numbers 47 and 78, the application will look like this:

Enter a number:
> 47
Enter another number:
> 78
Is 47 equals to 78? FALSE


When the user enters the numbers 78 and 78, then:

Enter a number:
> 78
Enter another number:
> 78
Is 78 equals to 78? TRUE

dividerSECS-s20.gif

Task 3 - Transform the following mathematical expressions into arithmetic expressions on your computer. Evaluate if the results of the 3 operations are greater than or equal to 0 (>=) and show it on the screen.

y.jpeg


In arithmetic form, the mathematical equation in the figure above becomes like this:

y = (8-2)*(5+4)
x = (8/2) + (5/5)
z = (((4+8)*3)/2)/3


When adapted into a program algorithm, pseudo-code can have this anatomy:

Algorithm Calculate_and_Evaluate
    // 1 -  Declare variables;
    Define y, x, z As Real;
    
    // 2 – The calculation;
    Print “Calculate y: ” 
    Read y;
    Print “Calculate x: ” 
    Read x;
    Print “Calculate z: ” 
    Read z;

    // 3 - The results; 
    Print "The value of y is: ", y;
    Print "The value of x is: ", x;
    Print "The value of z is: ", z;

    // 4 – Evaluation
    Print "Is " y " greater than or equal to 0? " y>=0;
    Print "Is " x " greater than or equal to 0? " x>=0;
    Print "Is " z " greater than or equal to 0? " z>=0;
EndAlgorithm


The result that will be achieved on the program interface when executed is:

Calculate y:
> y = (8-2)*(5+4)
Calculate x:
> x = (8/2) + (5/5)
Calculate z:
> z = (((4+8)*3)/2)/3
The value of y is: 54
The value of x is: 5
The value of z is: 6
Is 54 greater than or equal to 0? TRUE
Is 5 greater than or equal to 0? TRUE
Is 6 greater than or equal to 0? TRUE

dividerSECS-s20.gif

Conclusion

Math has never been my favorite subject, and after reading some literature on the internet, I now know that Math and Programming are inseparable. The position of Math is important for several reasons, including:

  • Math trains logical and analytical thinking which is needed by Programming to solve problems;
  • Number theories, geometry, and algebra are often used to optimize algorithms for searching, sorting, or analyzing data;
  • Mathematical equations that are capable of performing calculations to determine the position, scale, rotation of objects in space, and so on, are what support rendering in 3-dimensional programs.
  • Statistics, calculus, and linear algebra are the most important foundations in the development of Artificial Intelligence and Machine Learning.
  • Computer system security takes the form of cryptography on number theory and other mathematical concepts.

dividerSECS-s20.gif

Thanks

Even though Week 3 was starting to feel like the basic programming theory lessons were starting to “sting”, I found myself getting more and more interested. Programming turns out to require strong logic and analytical skills. And, there is Math. I started to think that maybe in 3 weeks (season 20 ends), I will grow some love for Math. It really has been an adventure of sorts, and Season 20 of SEC might have been the best season I've ever participated in. Thank you Professor Alejos7ven.

dividerSECS-s20.gif

Sources and Reading Suggestion


My Introductory Post | Artikel Perkenalan Saya.


Picture created by @aneukpineung78


Thanks for stopping by.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Belajar... Belajar... Belajar....

Iya, ayok ikutan belajar, Bang @lord-geraldi. Biarkan RSUD itu dengan kelakuannya, sudah nasib kita Bansa Teuleubeh Ateuh Rueng Donya. Hahah.

Loading...

Thank you very much for publishing your post in Steem SEA Community. We encourage you to keep posting your quality content and support each other in the community

DescriptionInformation
Verified User
Plagiarism Free
#steemexclusive
Bot Free
BeneficiaryNo
burnsteem25No
Status ClubClub75
AI Article✅ Original (Human text!)
I invite you to support @pennsif.witness to grow across the whole platform through robust communication at all levels and targeted high-yield developments with the resources available.

Click Here

Thank you.

TEAM 4

Congratulations! Your post has been upvoted through steemcurator06.
Good post here should be . . .

Curated by : @𝗁𝖾𝗋𝗂𝖺𝖽𝗂

Terimakasih banyak. Saya sangat menghargai apa yang Anda kerjakan.