Anda di halaman 1dari 7

LAB 1: INTRODUCTION TO C PROGRAMMING

Upon completion of this lab session, students will be able to:

Convert algorithm for programming application

Discover the three types of programming error

Demonstrate how to solve a problem by applying three


phases in software development process

Construct a program using variables

NCB20103: COMPUTER PROGRAMMING FOR ENGINEERS 1


LAB HANDBOOK Version: 2.2 (September 2015)
1.1 A simple C program

The electrical resistance, r, of a metal wire, in ohms, is given by the formula r = (ml)/a where
m is the resistivity of the metal; l is the length of the wire in feet; and a is the cross-sectional
area of the wire, in circular mils. Using this information, write a C program to calculate the
resistivity of a wire that is 125 feet long, has a cross-sectional area of 500 circular mils, and
is copper. The resistivity of copper, m is 1.04.

Using the software development process, first phase is development and design.

STEP 1: PROBLEM DEFINITION / ANALYSE THE PROBLEM


Calculate the electrical resistance of a wire?
This program is so computationally extensive, that we want to write a C program to
solve it on a computer. At least we will assume it is for now.

ACTIVITY 1
Analyze the problem on a piece of paper.
Input: ?
Output: ?
Formula: ?

STEP 2: PROGRAM DESIGN / DEVELOP SOLUTION ALGORITHM


Before we attempt to write the program, let's develop an algorithm for solving the
problem.

ACTIVITY 2
(a) Design the algorithm for this problem. On a piece of paper draw a flowchart to
represent the algorithm. Remember your algorithm must be precise. This algorithm
must be translated to C to obtain the program.
(b) Now that you have the algorithm, test it to see if it works on paper.

STEP 3: IMPLEMENTING THE ALGORITHM / CODING IN C


This is where you will translate the algorithm to C. A program is designed based on the
algorithm that is given in the previous part. Check to make sure you find everything is
translated preciously.
ACTIVITY 3
Create a new C project. Write the above C program. Build and run.

STEP 4: TEST THE PROGRAM


The last thing you need to do is to test the program to make sure it produces the correct
results.

ACTIVITY 4
Verify the displayed result on PC with your calculation on a paper. Does it produce the
correct result?

NCB20103: COMPUTER PROGRAMMING FOR ENGINEERS 2


LAB HANDBOOK Version: 2.2 (September 2015)
1.2 Programs Error

When using a program to solve a problem, you may have one of the following three errors:
i. Syntax Error
ii. Logic Error
iii. Run-time Error

i. Syntax Error
Syntax error will be the result of violation of the syntax (the grammar rules) of the
programming language that you use. For instance, if you forget to put ; at the end of
a C instruction, your compiler will not correctly compile and will display and error on
the screen. To see the type of error, you can remove one of the ; a try to compile
the program. Another example of this type of error is if you do not have a paired
open { and close } set of braces.

ACTIVITY 5
Modify the program in the Activity 3. Remove ; from the following statement:
resistivity = 10.4;

Becomes,

resistivity = 10.4

Then, build and run. Is there any compiler message? Describe the message and
classify the error.

NCB20103: COMPUTER PROGRAMMING FOR ENGINEERS 3


LAB HANDBOOK Version: 2.2 (September 2015)
ii. Logic Error
The logic error will be the result of incorrect translation of your algorithm when you
were writing the program. This error will not be detected by the computer and the
only way to find it is to test the program carefully after it is completed. For instance,
if in the following statement:

resistance = (resistivity * length) / area; //correct

which is the correct statement for computing the resistance, but if by mistake, we
use + instead of *, then we will have:

resistance = (resistivity + length) / area; //wrong

We will get an answer, but that answer is not correct. The error is the result of the
mistake in the translation of our algorithm. Instead of * we have used +.

ACTIVITY 6
Modify the program in the Activity 2. Change * to + in the following statement:
resistance = (resistivity * length) / area;

Becomes,

resistance = (resistivity + length) / area;

Then, build and run. What is the displayed result? Is it correct? Classify the error.

iii. Run-time Error


A run-time error is detected when we run a program. This type of error is mostly
related to numeric calculations. For example, a computer cannot compute the
square root of a negative number.

NCB20103: COMPUTER PROGRAMMING FOR ENGINEERS 4


LAB HANDBOOK Version: 2.2 (September 2015)
1.3 Variable

"All variables must be declared before they are used in a C program."

Variables properties
There are 6 properties associated with a variable. The first three are very important as you
start to program. The last three are important as you advance and improve your skills (and
the complexity of the programs you are creating).

i. A Name
The name is Symbolic. It represents the "title" of the information that is being stored with
the variable.
The name is perhaps the most important property to the programmer, because this is how
we "access" the variable. Every variable must have a unique name.

ii. A Type
The type represents what "kind" of data is stored with the variable.
In C, Java, ActionScript, etc., the type of a variable must be explicitly declared when the
name is created.
In Matlab, the type of the variable is inferred from the data put into the variable.

iii. A Value
A variable, by its very name, changes over time. Thus, if the variable name is my_age and is
assigned the value 21, at another point, my_age may be assigned the value 27.

iv. A Scope
Good programs are "Chopped" into small self-contained sections (called functions) much like
a good novel is broken into chapters, and a good chapter is broken into paragraphs, etc. A
variable that is seen and used in one function is NOT available in another section. This allows
us to reuse variable names, such as age. In one function 'age' could refer to the age of a
student, and in another function 'age' could refer to the vintage of a fine wine.

v. A Life Time
The life time of a variable is strongly related to the scope of the variable. When a program
begins, variables "come to life" when the program reaches the line of code where they are
"declared". Variables "die" when the program leaves the "Scope" of the variable.

vi. A Location (in Memory)


Luckily, we don't have to worry too much about where in the computer hardware the
variable is stored.

NCB20103: COMPUTER PROGRAMMING FOR ENGINEERS 5


LAB HANDBOOK Version: 2.2 (September 2015)
All variables used in a C program must be declared. The declaration of a variable can be
done in several different ways;
1) right at the beginning of the main function,
2) right before its use in the program, and
3) right before the main function.

As it was shown in the sample C program in the previous lab, we declared variables of type
double right at the beginning of the main function. In general, the syntax for variable
declaration is:
Type_name Variable_Name_1, Variable_Name_2;

Example:
double resistivity, area, length, resistance;

ACTIVITY 7
Write a short program that declares and initializes double variables one, two, three, four,
and five to the values 1.000, 1.414, 1.732, 2.000, and 2.236, respectively. Then write output
statements to generate the following legend and table. Use the tab escape sequence \t to
line up the columns. If you are unfamiliar with the tab character, you should experiment
with it while doing this exercise. A tab works like a mechanical stop on a typewriter. A tab
causes output to begin in a next column, usually a multiple of eight spaces away. Many
editors and most word processors will have adjustable tab stops. Our output does not.

The output should be:


N Square Root
1 1.000
2 1.414
3 1.732
4 2.000
5 2.236

ACTIVITY 8
Write a complete C program that reads in two whole numbers and outputs their sum. Be
sure to prompt for input, echo input, and label all output.

NCB20103: COMPUTER PROGRAMMING FOR ENGINEERS 6


LAB HANDBOOK Version: 2.2 (September 2015)
1.4 TUTORIAL / LAB EXERCISES

This set of problems involves converting a value in one unit to a value in another unit. Each
program should prompt the user for a value in the specified units and then print the
converted value, along with the new units.

Write a complete C program to convert (use software development process):

1) Miles to kilometres. (Given that 1 mi = 1.6093440km)

2) Pounds to kilograms. (Given that 1 kg = 2.205 lb)

3) Degrees Fahrenheit (Tf) to degrees Rankin (Tr). (Given that Tf = Tr 459.67oR)

NCB20103: COMPUTER PROGRAMMING FOR ENGINEERS 7


LAB HANDBOOK Version: 2.2 (September 2015)

Anda mungkin juga menyukai