{"id":731,"date":"2017-08-12T15:52:14","date_gmt":"2017-08-12T15:52:14","guid":{"rendered":"http:\/\/people.utm.my\/kohmenghock\/?page_id=731"},"modified":"2017-08-13T14:13:44","modified_gmt":"2017-08-13T14:13:44","slug":"codeblocks-simple-guide-simple-mathematical-operations","status":"publish","type":"page","link":"https:\/\/people.utm.my\/kohmenghock\/codeblocks-simple-guide-simple-mathematical-operations\/","title":{"rendered":"Code::Blocks simple guide &#8211; simple mathematical operations I"},"content":{"rendered":"<p style=\"text-align: justify\"><span style=\"font-size: 12pt;font-family: arial,helvetica,sans-serif\"><br \/>\nIn this discussion, we will look at performing simple mathematical operations using Fortran programming language. We will create a new project for this exercise. You may want to revisit our previous discussion on creating new project in Code::Blocks <a href=\"https:\/\/people.utm.my\/kohmenghock\/1-0-creating-new-project\/\" target=\"_blank\" rel=\"noopener\">here<\/a>.<br \/>\n<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"font-size: 12pt;font-family: arial,helvetica,sans-serif\"><br \/>\nBefore we begin, it is important to highlight the types of variables that we declare at the top section of the code immediately after name of the program. By default, Fortran reserves variable names starting with the alphabet &#8220;i&#8221; to &#8220;n&#8221; as integer type, and real type for other remaining alphabets. However, this default setting is negated by the command &#8220;implicit none&#8221;. It that case, we need to declare the type of each variable that we are going to use. For ease of discussion, we shall limit ourselves here to two types of variables namely <span style=\"color: #ff0000\"><strong>integer<\/strong><\/span> and <span style=\"color: #ff0000\"><strong>real<\/strong><\/span> types.<br \/>\n<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"font-size: 12pt;font-family: arial,helvetica,sans-serif\"><br \/>\nIn the code below, you will notice that I introduced two input parameters in which variable names have been arbitrary chosen as &#8220;a&#8221; and &#8220;b&#8221;, both having been declared as integers. The section which follows immediately the input parameters are the four different mathematical operations. The first three mathematical operations are easily understood &#8211; I think you can easily code these operations as well. The fourth (exponential) might be new to some of you.<br \/>\n<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"font-size: 12pt;font-family: arial,helvetica,sans-serif\"><br \/>\nLet say we want to ask the code to calculate the square of a value (say 3). We can achieve this by writing:<br \/>\n<\/span><\/p>\n<p style=\"text-align: center\"><span style=\"font-size: 12pt;font-family: arial,helvetica,sans-serif\">3**2<\/p>\n<p style=\"text-align: left\"><span style=\"font-size: 12pt;font-family: arial,helvetica,sans-serif\">Similarly, if we want to calculate 2 to the power of 3\/2 we will write<\/p>\n<p style=\"text-align: center\"><span style=\"font-size: 12pt;font-family: arial,helvetica,sans-serif\">2**(3\/2)<\/p>\n<p style=\"text-align: justify\"><span style=\"font-size: 12pt;font-family: arial,helvetica,sans-serif\"><br \/>\nHere, I will need to mention about the hierarchy of operation in Fortran. \u00a0As in the last example above, the operation inside the parenthesis takes precedence over other operations. That is the code will first execute this operation first before going to the others. In the absence of parenthesis, the hierarchy (from higher to lower priority) would be <span style=\"color: #0000ff\">exponential<\/span>, <span style=\"color: #0000ff\">multiplication \/ division<\/span> and lastly <span style=\"color: #0000ff\">addition \/ subtraction<\/span>. Therefore, 2**(3\/2) is not the same as\u00a02**3\/2 ! You may want to try this on your own to see the outcome of the code.<br \/>\n<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"font-size: 12pt;font-family: arial,helvetica,sans-serif\"><br \/>\nSo here is the code which I have written for this simple exercise.<br \/>\n<\/span><\/p>\n<p><a href=\"https:\/\/people.utm.my\/kohmenghock\/wp-content\/uploads\/sites\/982\/2017\/08\/main_code_maths_op.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-734 size-large\" src=\"https:\/\/people.utm.my\/kohmenghock\/wp-content\/uploads\/sites\/982\/2017\/08\/main_code_maths_op-1024x544.png\" alt=\"\" width=\"1024\" height=\"544\" srcset=\"https:\/\/people.utm.my\/kohmenghock\/wp-content\/uploads\/sites\/982\/2017\/08\/main_code_maths_op-1024x544.png 1024w, https:\/\/people.utm.my\/kohmenghock\/wp-content\/uploads\/sites\/982\/2017\/08\/main_code_maths_op-300x159.png 300w, https:\/\/people.utm.my\/kohmenghock\/wp-content\/uploads\/sites\/982\/2017\/08\/main_code_maths_op-768x408.png 768w, https:\/\/people.utm.my\/kohmenghock\/wp-content\/uploads\/sites\/982\/2017\/08\/main_code_maths_op.png 1920w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><span style=\"font-size: 12pt;font-family: arial,helvetica,sans-serif\"><br \/>\nAfter building the project and compiling the code, you will get the following results on the terminal.<a href=\"https:\/\/people.utm.my\/kohmenghock\/wp-content\/uploads\/sites\/982\/2017\/08\/results_maths_op_1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-735 size-large\" src=\"https:\/\/people.utm.my\/kohmenghock\/wp-content\/uploads\/sites\/982\/2017\/08\/results_maths_op_1-1024x582.png\" alt=\"\" width=\"1024\" height=\"582\" srcset=\"https:\/\/people.utm.my\/kohmenghock\/wp-content\/uploads\/sites\/982\/2017\/08\/results_maths_op_1-1024x582.png 1024w, https:\/\/people.utm.my\/kohmenghock\/wp-content\/uploads\/sites\/982\/2017\/08\/results_maths_op_1-300x170.png 300w, https:\/\/people.utm.my\/kohmenghock\/wp-content\/uploads\/sites\/982\/2017\/08\/results_maths_op_1-768x436.png 768w, https:\/\/people.utm.my\/kohmenghock\/wp-content\/uploads\/sites\/982\/2017\/08\/results_maths_op_1.png 1348w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\"><span style=\"font-size: 12pt;font-family: arial,helvetica,sans-serif\"><br \/>\nYeah, we managed to execute the code and we should be happy. But wait for a moment. We need to check if the results are correct. Once you check it properly, you will find that there are, in fact, some mistakes. Can you identify those mistakes and pinpoint the cause of it? Try to identify it on your own and I will discuss about this in my next discussion.<br \/>\n<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this discussion, we will look at performing simple mathematical operations using Fortran programming language. We will create a new project for this exercise. You may want to revisit our previous discussion on creating new project in Code::Blocks here. Before we begin, it is important to highlight the types of variables that we declare at &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/people.utm.my\/kohmenghock\/codeblocks-simple-guide-simple-mathematical-operations\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Code::Blocks simple guide &#8211; simple mathematical operations I&#8221;<\/span><\/a><\/p>\n","protected":false},"author":14180,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"open","template":"","meta":{"footnotes":""},"class_list":["post-731","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/people.utm.my\/kohmenghock\/wp-json\/wp\/v2\/pages\/731","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/people.utm.my\/kohmenghock\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/people.utm.my\/kohmenghock\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/people.utm.my\/kohmenghock\/wp-json\/wp\/v2\/users\/14180"}],"replies":[{"embeddable":true,"href":"https:\/\/people.utm.my\/kohmenghock\/wp-json\/wp\/v2\/comments?post=731"}],"version-history":[{"count":0,"href":"https:\/\/people.utm.my\/kohmenghock\/wp-json\/wp\/v2\/pages\/731\/revisions"}],"wp:attachment":[{"href":"https:\/\/people.utm.my\/kohmenghock\/wp-json\/wp\/v2\/media?parent=731"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}