Lab 1: Introduction To C Programming: Upon Completion of This Lab Session, Students Will Be Able To
Lab 1: Introduction To C Programming: Upon Completion of This Lab Session, Students Will Be Able To
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.
ACTIVITY 1
Analyze the problem on a piece of paper.
Input: ?
Output: ?
Formula: ?
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.
ACTIVITY 4
Verify the displayed result on PC with your calculation on a paper. Does it produce the
correct result?
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.
which is the correct statement for computing the resistance, but if by mistake, we
use + instead of *, then we will have:
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,
Then, build and run. What is the displayed result? Is it correct? Classify the error.
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.
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.
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.
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.