•   support@lemon-web.net
  • 6012 350 7230

search

C Programming - Basic Use of Counter Closed Loop

Few years back, i was given an assignment to write an answer to an assignment question for Computer Programming part 2 involving use of Counter Closed Loop. I have written the code together with the diagram for illustration of the codes. Anyway, now i have decided to release my written assignment paper here as it may help any other students that is learning on this subject. 



1. PREPARATION OF THE PROGRAM DEVELOPMENT

1.1 – Analysis of the Assignment's QuestionB

fore we began developing the program, the first rule of thumb is to always careful examine to requirement of the assignment's question. With that being said, we can develop the program based on the assignment's requirement. Refer below are the extracted assignment's question:

Based on abstract above, let us retrieve back the analysis and the algorithms which was written by me in my previous assignment. Refer to my previous submitted assignment for further information on this.

Below are the program's algorithms written in pseudo code:

1.2 - Determining Programming Prerequisites 

As per assignment's request, I need to write the program in C language. Therefore, we need to identify which is the best development tool or more commonly known as Integrated Development Environment (IDE) for the job and which compiler to go for. At the same time, we need to determine as well which compiler will be used to compile this project.

​ IDE: ​ Code::Blocks Release 12.11 rev 8629
​ Compiler: ​ GNU GCC Compiler
​ Language: ​ C Program
​ Build Target: ​ Console Application
​ Alternative IDE: ​ C4droid Version 4.97 with GCC+ Bionic Compiler

1.3 – Installing Development Software and Project Setup

Next step is to download the prerequisites software for the development and install the software to begin developing the program.

Development software are downloaded from the following link:
Code::Blocks with mingw: http://www.codeblocks.org/downloads/26
C4Droid: https://play.google.com/store/apps/details?id=com.n0n3m4.droidc&hl=en

After both software have been successfully setup, it is time to setup the project. Below are the steps taken to setup the project:

1. Start by create new Project. Go to File  New Project. Select Console Application.

2. Select C as we are required to develop a C language program.

3. Enter the project's title, filename. From the following page, I can also specifies different folder path for the project.

2. PROGRAM DEVELOPMENT

2.1 – Author's Information

Since I have a developed the algorithm for the program already, let's start on writing the code step by step based on the algorithm.

The first step is to include author's information to the program. By doing so, it does not affects anything at all except it gives some information about the author of the program. To do so, create a comment to insert these information.

2.2 - Program's Library & Functions

Since I basically needed basic standard input and output operators, therefore the library needed to be included as the first step. Then, when we need to add functions to the program. Since I am writing a progr


2.3 – Declaration of Variables

Next step, I need to declare all variables that will be used for the program. They are Base, Height, Cross-Sectional Area, Moment of Inertia and Section Modulus. All these variables are in the form of number, therefore declaring them as Integer would be my first choice.

However, due to certain calculations that requires the output to display decimal points, I have decided to declare these variables as float instead.

2.4 – Displaying the Table's Header

First output needed for the program is obviously the table's header. I need to write the program to display the table header using the printf command.

Refer to image below for display output for the table's header.

2.5 – Displaying the Table's Content

Next, we need to displays the table's content based on the assignment question's requirement. Start off with set the initial base value for base and height which is 2.

Next, we start the first set of Counter Loop by using FOR statement. The statement should be write in the following manner; "for (initialisation; expression; counter) {statement;}". That being said, the counter will start to count on Base values, with an incremental value of 2 until it reaches the value 10. Within the Base Counter Loop statements, I created another sets of Counter Loop and this time to count the height values with the incremental value of 2 until it reaches the value of 12. To gain further clarification on this, refer to illustration 2.2 for the flowcharts on how the programs looped in order to display the result. 

Next, we define the formula for the calculations of Cross-Sectional Area, Moment of Inertia and Section Modulus. Once these has been written, we need to write the statements to display the table's content.

By using the PRINTF statement, I am able to display the result accordingly. As this is result in table's viewing, we need to insert's table separator in between each results. Refer below for the statements for the table's content.

2.6 – Finalising and Closing Functions and Statements

When compiled as a Windows Application (EXE extension), the program will run and automatically closes in less than one seconds. Due to this, we may need to add a pause to the program so that the user can view the table until a key has been pressed. Therefore, I have added the "getchar()" statement prior to this matter.

After I have finished with the whole program, I need to set return back to () value. As I am using the function "int main()" earlier instead of using "void main(void)", I need to return the int value back to 0 for a clean.

There is nothing wrong as well to compile and build the application as it will still be able to compile and executed successfully, however the process termination will have errors upon terminating without it with the error message "Process terminated with status 288 (X minutes, X seconds)". Therefore, when using int main() functions, always return 0 to ensure the application is exited or terminated successfully without any errors.


2.7 – Compiling, Executing & Debugging

Once done, compile and run the program to ensure it is written correctly. Depending on which IDE software used, the Debug will help assists in identifying what went wrong with the written codes.

For example, let's say a made a mistake on spelling out the word "float", the debug process helps me where the issues lies at. This phase is what we call as "Debugging" phase where we ensures that the code is written correctly. The debugging does not only applies during the compile and build session, but at the same time it also applies during the process and execution of the program.

After the program has been "Bug-Free" and was able to be compiled and build without any errors, run the program. Refer image above for the screenshot of the output of the program displayed in a console terminal

Based on the displayed output in the window, it shows that program successfully display the Engineering's table based on the assignment's required criteria.


2.8 – Implementation of White Spaces 

Part of the requirement for the assignment is the implementation of the white spaces for the program. Wikipedia (2015) defines these as per following:

"Whitespace is an esoteric programming language developed by Edwin Brady and Chris Morris at the University of Durham (also developers of the Kaya and Idris programming languages). It was released on 1 April 2003 (April Fool's Day). Its name is a reference to whitespace characters. Unlike most programming languages, which ignore or assign little meaning to most whitespace characters, the Whitespace interpreter ignores any non-whitespace characters. Only spaces, tabs and linefeeds have meaning. An interesting consequence of this property is that a Whitespace program can easily be contained within the whitespace characters of a program written in another language, except possibly in languages which depend on spaces for syntax validity such as Python, making the text a polyglot."

by Wikipedia (2015)

In laymen's term, whitespaces are the spaces outputted to the display and is should be written in a correct way. Now, let's examine back the PRINTF statements that was used to display the table's content.

The syntax which I've highlighted in bold above represents the bad way of displaying spaces, therefore it should be revised for a correct implementation of the whitespaces.

There are few ways of tackling this issue, as I might use the '\t' escape sequence to generate spaces. However, I believed that proper specification values for the float value ('%f') should be amended instead. Besides that, it is better to declare the table's divider and the symbol 'x' as a character and add define proper specification when displaying the output.

In effort to remove these whitespaces, I have made changes to the program as per code above. Two additional variables are declared which is Table's Divider and Character 'x', and I have removed the use of spaces and replaced it with escape sequence characters. 


3. FINAL BUILD

3.1 – Full C Program Source Code

After I have finished with everything, it is time for the final build. Refer below for the final revision of the build code for the program.

3.2 – Output displayed in Windows

Refer illustration 3.1 for screenshot displaying the program's output displayed in Console.

3.3 – Mobile Phone Environment 

As mentioned earlier, I will test the written program with another IDE, C4Droid. This is a Android Phone based IDE which comes together with a compiler capable of exporting to Android application.

Refer to illustration 3.2 for the displayed output viewing from my own mobile phone. Application has been compiled and executed successfully without any issues on my phone.


3.4 – Source Codes, Windows Application & Mobile Phone Apps 

The source code is attached together when submitting this assignment. However, I have uploaded the compiled and build version for both Windows & Android for any users to test run this application.

Refer below for the download details for the mentioned applications.

Application: CBCP2202-02 Windows EXE
Filename: CBCP2202-02.exe

Application: CBCP2202-02 Mobile Apps
Filename: cbcp2202-02.apk

1- Marini Abu Bakar, Dr Sufian Idris, Nor Leyza Jailani, Roziah Latih. (2014), CBCP2101 Computer Programming I, First Edition. Open University Malaysia.
2- Marini Abu Bakar, Dr Sufian Idris, Nor Leyza Jailani, Roziah Latih. (2014), CBCP2202 Computer Programming II, First Edition. Open University Malaysia.
3- Whitespace (Programming Language). In Wikipedia. Retrieved Mar 20, 2015, from http://en.wikipedia.org/wiki/Whitespace_%28programming_language%29

References

You may download the published assignment paper here.

Salam Ramadhan 2019
Let's Replace Notepad With Notepad++ in Windows
 

ABOUT

An experienced web master with more than 10 years of functional web design experiences and have designed websites and applications since 2006 as a freelance web-designer, mastering coding, deployment, database, designs and more.