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 the analysis and algorithms written in the previous assignment (CDCP2101), write a complete C program according to the design. The assessment will be done based on the following criteria:
- i.A proper writing of C codes and its structure
- ii.The ability of program to be compiled and executed
- iii.Implementation of correct programming techniques
- iv.Complete documentation and correct submission
Note: You must write C programming codes for this assignment.
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.0 Start
2.0 – Declaration of Variables (base, height, CSA, MOI & SM) as float
3.0 – Print "Lumber Size" "Cross-Sectional Area" "Moment of Inertia" "Section Modulus"
// display table header
4.0 – Set initial value for i & j = 0
5.0 – for (i = 2; <=10; increase i by 2) // base value increase by 2 with <= 10 condition
6.0 – Start_for // Start Counter Controlled Loop for base
6.1 – for (j = 2; <12; increase j by 2) // height value increase by 2 with <=12 condition
6.2 – Start_for // Start Counter Controlled Loop for height
6.2.1 Set CSA = i x j // Cross-Sectional Area Formula
6.2.2 Set MOI = i x j x j x j / 12 // Moment of Inertia Formula
6.2.3 Set SM = i x j x j / 6 //Section Modulus Formula
6.2.4 Print i "x" j // display lumber size (base x height) where "x" as char
6.2.5 Calculate CSA // execute CSA formula
6.2.6 Print CSA // display CSA result
6.2.7 Calculate MOI // calculate MOI formula
6.2.8 Print MOI // display MOI result
6.2.9 Calculate SM // calculate SM formula
6.2.10 Print SM // display SM result
6.2.11 Write newline ()
6.3 – End_for
7.0 – End_for
8.0 – End
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:
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.
CPCP 2202 - COMPUTER PROGRAMMING II
SEMESTER JANUARY 2015
NAME : X
MATRICULATION NO : X
IDENTITY CARD NO. : X
TELEPHONE NO. : X
E-MAIL : This email address is being protected from spambots. You need JavaScript enabled to view it.
LEARNING CENTRE : BANGI LEARNING CENTRE
*/
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
#include <stdio.h>
/* Function Declaration */
int main()
{
// Program will be written here
}
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.
float i ; // Base
float j ; // Height
float CSA; // Cross-Sectional Area
float MOI ; // Moment of Inertia
float SM; // Section Modulus
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.
printf ("----------------------------------------------------------------------------");
printf ("\n");
printf ("| Lumber Size | Cross-Sectional Area | Moment of Inertia | Section Modulus |");
printf ("\n");
printf ("----------------------------------------------------------------------------");
printf ("\n");
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.
i = 2 ; j = 2 ;
/* Start Counter Loop for Base with increment of 2 until reach the value of 10 */
for (i = 2; i <= 10; i += 2)
{
/* Start Counter Loop for Height with increment of 2 until reach the value of 12 */
for (j = 2; j <= 12; j += 2)
{
/* Define the formula for CSA, MOI & SM */
CSA = (i * j); // CSA Formula
MOI = (i * j * j * j / 12); // MOI Formula
SM = (i * j * j / 6); // SM Formula
/* Displaying Table's Content */
printf ("| "); // Display Table Separator
printf ("%2.0f", i); // Display Base float value in 0 decimal points
printf (" x "); // Display the character 'x'
printf ("%2.0f", j); // Display Height float value in 0 decimal points
printf (" | "); // Display Table Separator
printf ("%3.0f", CSA); // Display CSA float value in 0 decimal points
printf (" | "); // Display Table Separator
printf ("%8.2f", MOI); // Display MOI float value in 2 decimal points
printf (" | "); // Display Table Separator
printf ("%8.2f", SM); // Display SM float value in 2 decimal points
printf (" |"); // Display Table Separator
printf ("\n"); // Go to next line
}}
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.
getchar(); // This will add a pause before terminating the application
/* Exit */
return 0;
}
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.
printf ("| "); // Display Table Separator
printf ("%2.0f", i); // Display Base float value in 0 decimal points
printf (" x "); // Display the character 'x'
printf ("%2.0f", j); // Display Height float value in 0 decimal points
printf (" | "); // Display Table Separator
printf ("%3.0f", CSA); // Display CSA float value in 0 decimal points
printf (" | "); // Display Table Separator
printf ("%8.2f", MOI); // Display MOI float value in 2 decimal points
printf (" | "); // Display Table Separator
printf ("%8.2f", SM); // Display SM float value in 2 decimal points
printf (" |"); // Display Table Separator
printf ("\n"); // Go to next line
}}
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.
float i ; // Base
float j ; // Height
float CSA; // Cross-Sectional Area
float MOI ; // Moment of Inertia
float SM; // Section Modulus
char divider; divider = '|'; // Table's Divider
char times; times = 'x'; // Character 'x'
/* Displaying Table's Content */
printf ("%c", divider); // Display Table Separator
printf ("%4.0f", i); // Display Base float value in 0 decimal points
printf ("%3c", times); // Display the character 'x'
printf ("%4.0f", j); // Display Height float value in 0 decimal points
printf ("%3c", divider); // Display Table Separator
printf ("%13.0f", CSA); // Display CSA float value in 0 decimal points
printf ("%10c", divider); // Display Table Separator
printf ("%13.2f", MOI); // Display MOI float value in 2 decimal points
printf ("%7c", divider); // Display Table Separator
printf ("%11.2f", SM); // Display SM float value in 2 decimal points
printf ("%7c", divider); // Display Table Separator
printf ("\n"); // Go to next line
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.
CPCP 2202 - COMPUTER PROGRAMMING II
SEMESTER JANUARY 2015
NAME : X
MATRICULATION NO : X
IDENTITY CARD NO. : X
TELEPHONE NO. : X
E-MAIL : This email address is being protected from spambots. You need JavaScript enabled to view it.
LEARNING CENTRE : BANGI LEARNING CENTRE
CBCP 2202 Assignment - Version 1
*/
/* Include Standard Input Output library */
#include <stdio.h>
/* Function Declaration */
int main()
{
/* Declaration of Variables (base, height, CSA, MOI, SM, divider & times) */
float i ; // Base
float j ; // Height
float CSA; // Cross-Sectional Area
float MOI ; // Moment of Inertia
float SM; // Section Modulus
char divider; divider = '|';
char times; times = 'x';
/* Displaying Table Header */
printf ("----------------------------------------------------------------------------");
printf ("\n");
printf ("| Lumber Size | Cross-Sectional Area | Moment of Inertia | Section Modulus |");
printf ("\n");
printf ("----------------------------------------------------------------------------");
printf ("\n");
/* Set initial value for base and height */
i = 2 ; j = 2 ;
/* Start Counter Loop for Base with increment of 2 until reach the value of 10 */
for (i = 2; i <= 10; i += 2)
{
/* Start Counter Loop for Height with increment of 2 until reach the value of 12 */
for (j = 2; j <= 12; j += 2)
{
/* Define the formula for CSA, MOI & SM */
CSA = (i * j); // CSA Formula
MOI = (i * j * j * j / 12); // MOI Formula
SM = (i * j * j / 6); // SM Formula
/* Displaying Table's Content */
printf ("%c", divider); // Display Table Separator
printf ("%4.0f", i); // Display Base float value in 0 decimal points
printf ("%3c", times); // Display the character 'x'
printf ("%4.0f", j); // Display Height float value in 0 decimal points
printf ("%3c", divider); // Display Table Separator
printf ("%13.0f", CSA); // Display CSA float value in 0 decimal points
printf ("%10c", divider); // Display Table Separator
printf ("%13.2f", MOI); // Display MOI float value in 2 decimal points
printf ("%7c", divider); // Display Table Separator
printf ("%11.2f", SM); // Display SM float value in 2 decimal points
printf ("%7c", divider); // Display Table Separator
printf ("\n"); // Go to next line
printf ("----------------------------------------------------------------------------");
printf ("\n"); // Go to next line
}}
// Pause
getchar(); // This will add a pause before terminating the application
// Exit
return 0;
}
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.
References
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
You may download the published assignment paper here.