Вы находитесь на странице: 1из 22

LBYEC72

Computer Fundamentals Programming 2

Laboratory Activity 2
Getting Started

By

Patrick D. Villoria, LBYEC72-EI


INTRODUCTION

Modular programming is a software design technique that emphasizes


separating the functionality of a program into independent, interchangeable
modules, such that each contains everything necessary to execute only one aspect
of the desired functionality.

Program Control Function

The main program piece in many programming languages is a special


function with the identifier name of main. The special or uniqueness of main as a
function is that this is where the program starts executing code and this is where it
usually stops executing code. It is often the first function defined in a program and
appears after the area used for includes, other technical items, declaration of
prototypes, the listing of global constants and variables and any other items
generally needed by the program. The code to define the function main is provided;
however, it is not prototyped or usually called like other functions within a program.

Program Layout

Most programs have several items before the functions, including:

Documentation – Most programs have a comment area at the start of the program
with a variety of comments pertinent to the program.

Include or import statements used to access standard library functions.

Language-specific code such as namespace references or function prototypes.

Global or module-level constants and variables, when required.

Key Terms

-braces

Used to identify a block of code in languages such as C++, C#, Java, and JavaScript.

-function

What modules are called in many predominant programming languages of today.

-function call

A function’s using or invoking of another function.

1
-function definition

The code that defines what a function does.

-function prototype

A function’s communications declaration to a compiler.

-identifier name

The name given by the programmer to identify a function or other program items
such as variables.

-modularization

The ability to group some lines of code into a unit that can be included in our
program.

-parameter passing

How the data is communicated in to and out of a function.

-program control

Functions used to simply subdivide and control the program.

-specific task

Functions designed to be used with several programs.

OBJECTIVES

● Declare functions and use functions in programs.


● Understand pass-by-value and parameter lists.
● Modularize programs with the use of functions and pass-by-value.
● Convert equations previously done in nested conditional and iterative
statements topic to equivalent modular and reusable functions.
● Create reusable functions that can be used in other programs.

MATERIALS

1. Dev-C++ Integrated Development Environment (IDE)


2. Snipping Tool

2
PROCEDURE
 Program is a modified version of the collection of formulas from Nested
Conditional and Iterative Statements.
 Convert the formulas previously done in the collection of formulas to
equivalent functions.
 Program can be subdivided into smaller units by creating functions that
compute for different variables.
 Requirements from Nested Conditional and Iterative Statements apply.

RESULTS AND DISCUSSION

1. Select a formula from the formula selection screen.

2. Select a variable from the variable selection screen.

3
3. Compute for the variable.

4. Compute for the second variable

4
5. Compute for the third variable

6. Compute for the last variable

5
SUMMARY

In this activity, I learned that functions and modularizing programs are very
useful functions in c programming. Some programs might have thousands or
millions of lines and to manage such programs it becomes quite difficult as there
might be too many of syntax errors or logical errors present in the program, so to
manage such type of programs concept of modular programming approached.
Modular programming emphasis on breaking of large programs into small problems
to increase the maintainability, readability of the code and to make the program
handy to make any changes in future or to correct the errors.

REFERENCES

1. https://press.rebus.community/programmingfundamentals/chapter/modular-
programming/
2. https://cnx.org/contents/MDgA8wfz@22.2:YzfkjC2r@17/Preface

6
APPENDIX

#include <stdio.h>

#include <stdlib.h>

float pressure(float v, float m, float t)

float p;

p = 8.3143 * m * t / v;

return p;

float volume(float m, float t, float p)

float v;

v = 8.3143 * m * t / p;

return v;

float mole(float v, float p, float t)

7
float m;

m = v * p / t / 8.3143;

return m;

float temperature(float v, float p, float m)

float t;

t = v * p / m / 8.3143;

return t;

float heat(float ki, float po, float in, float wo)

float he;

he = ki + po + in + wo;

return he;

float work(float he, float po, float ki, float in)

float wo;

wo = he - po - ki - in;

return wo;

8
}

float kinen(float he, float po, float wo, float in)

float ki;

ki = he - po - wo - in;

return ki;

float poten(float he, float ki, float wo, float in)

float po;

po = he - ki - wo - in;

return po;

float inten(float he, float ki, float wo, float po)

float in;

in = he - ki - wo - po;

return in;

int main()

9
{

int form, back, ret;

float vol, mol, temp, pres, Q, W, PE, KE, U;

do

printf("\nInput 1 for Ideal gas formula, input 2 for 1st law


of Thermodynamics formula, input 3 to exit.");

printf("\nChoice: ");

scanf("%d", &form);

if(form == 1)

do

printf("\nChoose a variable, or press 0


to go back to menu selection");

printf("\n1. Pressure");

printf("\n2. Volume");

printf("\n3. Numer of moles");

printf("\n4. Temperature");

printf("\nChoice: ");

scanf("%d", &back);

if(back == 1)

printf("\nYou have
selected Pressure ");

printf("\nEnter

10
volume: ");

scanf("%f", &vol);

printf("\nEnter
number of moles: ");

scanf("%f", &mol);

printf("\nEnter the
temperature: ");

scanf("%f",
&temp);

pres =
pressure(vol, mol, temp);

printf("\nThe
pressure is %.4f", pres);

printf("\nWould
you want to go back and pick another variable? 0 for yes, any other number to exit.
");

scanf("%d", &ret);

else if(back == 2)

printf("\nYou have
selected Volume ");

printf("\nEnter
pressure: ");

scanf("%f", &pres);

printf("\nEnter

11
number of moles: ");

scanf("%f", &mol);

printf("\nEnter the
temperature: ");

scanf("%f",
&temp);

vol = volume(mol,
temp, pres);

printf("\nThe
volume is %.4f", vol);

printf("\nWould
you want to go back and pick another variable? 0 for yes, any other number to exit.
");

scanf("%d", &ret);

else if(back == 3)

printf("\nYou have
selected Number of moles ");

printf("\nEnter
pressure: ");

scanf("%f", &pres);

printf("\nEnter
volume: ");

scanf("%f", &vol);

12
printf("\nEnter the
temperature: ");

scanf("%f",
&temp);

mol = mole(vol,
pres, temp);

printf("\nThe
number of moles is %.4f", mol);

printf("\nWould
you want to go back and pick another variable? 0 for yes, any other number to exit.
");

scanf("%d", &ret);

else if(back == 4)

printf("\nYou have
selected Temperature ");

printf("\nEnter
Pressure: ");

scanf("%f", &pres);

printf("\nEnter
Number of moles: ");

scanf("%f", &mol);

printf("\nEnter
Volume: ");

scanf("%f", &vol);

13
temp =
temperature(vol, pres, mol);

printf("\nThe
temperature is %.4f", temp);

printf("\nWould
you want to go back and pick another variable? 0 for yes, any other number to exit.
");

scanf("%d", &ret);

else if(back == 0)

break;

else

printf("Not a valid
choice, pls choose a valid choice. ");

ret == 0;

while(ret == 0);

else if(form == 2)

14
{

do

printf("\nChoose a variable, or press 0


to go back to menu selection");

printf("\n1. Heat");

printf("\n2. Work");

printf("\n3. Kinetic Energy");

printf("\n4. Potential Energy");

printf("\n5. Internal energy");

printf("\nChoice: ");

scanf("%d", &back);

if(back == 1)

printf("\nYou have
selected Heat ");

printf("\nEnter
Work: ");

scanf("%f", &W);

printf("\nEnter
Kinetic Energy: ");

scanf("%f", &KE);

printf("\nEnter
Potential Energy: ");

scanf("%f", &PE);

printf("\nEnter
Internal Energy: ");

15
scanf("%f", &U);

Q = heat(KE, PE, U,
W);

printf("\nThe Heat
is %.4f", Q);

printf("\nWould
you want to go back and pick another variable? 0 for yes, any other number to exit.
");

scanf("%d", &ret);

else if(back == 2)

printf("\nYou have
selected Work ");

printf("\nEnter
Heat: ");

scanf("%f", &Q);

printf("\nEnter
Kinetic Energy: ");

scanf("%f", &KE);

printf("\nEnter
Potential Energy: ");

scanf("%f", &PE);

printf("\nEnter
Internal Energy: ");

scanf("%f", &U);

16
W = work(Q, PE,
KE, U);

printf("\nThe Work
is %.4f", W);

printf("\nWould
you want to go back and pick another variable? 0 for yes, any other number to exit.
");

scanf("%d", &ret);

else if(back == 3)

printf("\nYou have
selected Kinetic Energy ");

printf("\nEnter
Work: ");

scanf("%f", &W);

printf("\nEnter
Heat: ");

scanf("%f", &Q);

printf("\nEnter
Potential Energy: ");

scanf("%f", &PE);

printf("\nEnter
Internal Energy: ");

scanf("%f", &U);

17
KE = kinen(Q, PE,
W, U);

printf("\nThe
Kinetic Energy is %.4f", KE);

printf("\nWould
you want to go back and pick another variable? 0 for yes, any other number to exit.
");

scanf("%d", &ret);

else if(back == 4)

printf("\nYou have
selected Potential Energy ");

printf("\nEnter
Work: ");

scanf("%f", &W);

printf("\nEnter
Heat: ");

scanf("%f", &Q);

printf("\nEnter
Kinetic Energy: ");

scanf("%f", &KE);

printf("\nEnter
Internal Energy: ");

scanf("%f", &U);

PE = poten(Q, KE,

18
W, U);

printf("\nThe
Potential Energy is %.4f", PE);

printf("\nWould
you want to go back and pick another variable? 0 for yes, any other number to exit.
");

scanf("%d", &ret);

else if(back == 5)

printf("\nYou have
selected Internal Energy ");

printf("\nEnter
Work: ");

scanf("%f", &W);

printf("\nEnter
Heat: ");

scanf("%f", &Q);

printf("\nEnter
Kinetic Energy: ");

scanf("%f", &KE);

printf("\nEnter
Potential Energy: ");

scanf("%f", &PE);

U = inten(Q, KE, W,
PE);

19
printf("\nThe
Potential Energy is %.4f", U);

printf("\nWould
you want to go back and pick another variable? 0 for yes, any other number to exit.
");

scanf("%d", &ret);

else if(back == 0)

break;

else

printf("Not a valid
choice, pls choose a valid choice. ");

while(ret == 0);

else if(form == 3)

return 0;

20
}

else

printf("Not a valid
choice, pls choose a valid choice. ");

back == 0;

while(back == 0);

return 0;

21