PI Practical 01 2324
PI Practical 01 2324
In this practical, we will continue working with the tools available Aims
in the Linux operating system. •••
The main goals of this prac-
We will also keep on learning how to use the basic tools for coding
tical are:
our programs in C language: emacs and gcc.
- Keep on getting familiar
In addition, we will start to handle the basic mechanisms provided with the laboratory work-
by the C language libraries for: space.
- Keep on learning how to
• Entering information from the keyboard, by means of the scanf() use the most basic Linux
function, and Operating System com-
• Displaying information to the screen, by means of the printf() mands.
function. - Consolidate the necessary
steps for editing, compiling
Finally, we will introduce some of the data types in C: the integer and executing a C program.
types and the strings of characters. - Get acquainted with C in-
put/output mechanisms.
- Get familiar with some C
data types.
Scheduling
1
Coding a basic program in C
Modifying a program
In order to modify the behaviour of a program we must modify the file containing its source code (using
the editor) and, after that, compile it again (using the compiler).
Exercise #1:
Create a directory called pr01 in your account (if not previously created).
Move to that directory and use the shell cp command to make a copy of ex3.c exercise from the previous
practical:
We can also use a program’s source code as the basis for creating a new one.
Exercise #2:
user@machine:~/pr01$ cp ../pr00/ex3.c ex2.c
Modify your program so it prints two lines. The first one must say "Goodbye", and the second one "See
you".
Compilation errors
When we make a coding mistake in our source code, the executable file will not be generated and the
compiler will point out the error or errors it has found, that have stopped the compilation.
Exercise #3:
user@machine:~/pr01$ cp ex2.c ex3.c
Erase the final ";" in one statement in the previous exercise source code. Test what happens when you try
to compile the program.
2
Type conversion
In these exercises we will start practicing with the automatics conversions among the numerical data
types (double, float, int and short) and their implications: what if we assign to a given type variable a
value from another type?
Exercise #4:
Carry out the following steps:
1. Write a program:
int integer;
float real;
integer = 4;
real = integer/5;
fprintf (stdout, "Quotient = %.2f\n", real);
real = (1.0*integer)/5;
fprintf (stdout, "Division 1 = %.2f\n", real);
real = integer/5.0;
fprintf (stdout, "Division 2 = %.2f\n", real);
real = 4.7;
integer = real;
fprintf (stdout, "Integer = %d\n", integer);
return 0;
}
3
Exercise #5:
Carry out the following steps:
1. Write a program:
short_integer = integer;
fprintf (stdout, "short_integer: %hd\n", short_integer);
integer = real;
fprintf (stdout, "integer: %d\n\n", integer);
integer = 3000000;
real = integer;
fprintf (stdout, "integer: %d\n", integer);
fprintf (stdout, "real: %.2f\n\n", real);
integer = 1234567892;
real = integer;
fprintf (stdout, "integer: %d\n", integer);
fprintf (stdout, "real: %.2f\n", real);
fprintf (stdout, "real: %.9e\n", real);
return 0;
}
4
Exercise #6:
Carry out the following steps:
1. Write a program:
real = 25.50;
real_double = 25e+100;
real = real_double * real;
fprintf (stdout, "Using float: %.2f\n\n", real);
real = 25.50;
real_double = real_double * real;
fprintf (stdout, "Using double: %.2f\n", real_double);
fprintf (stdout, "In scientific notation: %.4e\n\n", real_double);
return 0;
}
5
Developing simple programs
In these exercises, we will learn the use of the basic functions for keyboard data input, fscanf(), and
screen data output, fprintf().
In addition, we will learn how the use of library functions facilitates the fulfilment of many operations.
Exercise #7:
Write a program:
This program, first, asks the user to enter an integer number, for which it makes use of scanf() or
fscanf() function:
Enter an integer: 75
And, then, calculates the square root of the number (this will be a real number) and prints the result in
the screen, with the following format, for which it makes use of printf() or fprintf() function:
ROOT: 8.66
That is, right aligned, in a 10-character wide field, and with 2 decimal digits.
In order to solve this exercise, we need to make use of the sqrt() (square root) function from the mathe-
matical function library. Read the manual page for this function:
Exercise #8:
Write a program:
Enter a character: w
Letter w capitalised is W
In order to solve this exercise, we need to make use of the toupper() function from the string library.
6
Exercise #9:
Write a program:
And, then, calculates its length and prints the result in the screen:
In order to solve this exercise, we need to make use of the strlen() function from the string library.
Exercise #10:
Write a program:
This program first asks the user to enter the base and height of a right-angled triangle:
And then, calculates its area and perimeter and prints the result in the screen, with the following format:
Height: 9 cm
Base: 15 cm
Area: 67.50 cm x cm
Perimeter: 41.49 cm
Make sure that all the results are correctly aligned. The base and the height must be positive integers.
This exercise is greatly simplified using the sqrt() function from the mathematical function library.
Summary
7
Appendix I. Exercises on algorithms and flowcharts
Point out the algorithm steps and sketch the flowchart1 for the following problems (in all cases, the input
data are requested by keyboard to the user and the result is shown in the screen):
1 You can use a tool like DIA (http://live.gnome.org/Dia/), or PowerPoint, or you can even draw it by hand.
8
Appendix II. Programming exercises
Write C programs that perform the necessary operations to solve the following problems (in any of
them, before starting coding, sketch the flowchart):
Ask the user to enter a real number and show double its value and half its value in the screen, with the pertinent
messages.
1. Ask the user to enter a character and show the previous and the following character in the screen, with the pertinent
messages.
2. Given 2 points (ask the user to enter the pairs (x1, y1) and (x2, y2)) calculate the distance between them. For doing
so, you can use the sqrt() function from the mathematical functions library. Print the result, according to the fol-
lowing format:
(X1,Y1) = (5,6)
(x2,y2) = (5,4)
Distance = 2
3. Ask the user to enter an integer number between 1 and 10 and show the multiplication table for that number correctly
aligned
4. Ask the user to enter an amount in euros and calculate its equivalent in pesetas, dollars and pounds. Make sure that
all the results are correctly aligned.
5. Ask the user to enter 2 real numbers and show the addition, subtraction and product of those numbers. Make sure
that all the results are correctly aligned.
6. Ask the user to enter the cost of a sold item and the amount of money handed over by the customer. Calculate and
show the change that must be returned. Show the result with 2 decimal digits.
7. Ask the user to enter the radius of a circle (real type variable), calculate and show both its area and its circumference
length. Show the result with 3 decimal digits. What if the user types an alphabetic word instead of a number?
8. In the winter Olympics the time spent by the participants in the track speed skating competition is measured in
minutes, seconds and centesimal. The course distance is expressed in meters. Calculate the average speed of the
participants in kilometres/hour from the data previously indicated. Show the result with 2 decimal digits. What if the
user types more than one space between the numbers?
9. Solve a linear equations system (ask the user to enter the values for A1, A2, B1, B2, C1 y C2):
𝐴! 𝑥 + 𝐵! 𝑦 = 𝐶!
𝐴" 𝑥 + 𝐵" 𝑦 = 𝐶"
We will use the Cramer’s rule:
9
𝐶 𝐵! 𝐴 𝐶!
$ ! $ $ ! $
𝐶 𝐵" 𝐴 𝐶"
𝑥= " 𝑦= "
𝐴 𝐵! 𝐴! 𝐵!
$ ! $ $ $
𝐴" 𝐵" 𝐴" 𝐵"
Thus, the values to be computed are:
𝑥 = ((𝐶1 ∗ 𝐵2 ) − (𝐶2 ∗ 𝐵1 )) / ((𝐴1 ∗ 𝐵2 ) − (𝐴2 ∗ 𝐵1 ))
𝑦 = ((𝐴1 ∗ 𝐶2 ) − (𝐴2 ∗ 𝐶1 )) / ((𝐴1 ∗ 𝐵2 ) − (𝐴2 ∗ 𝐵1 ))
10. Ask the user to enter a number of days. Calculate and print the number of hours, minutes and seconds in the given
number of days. Make sure that all the results are correctly aligned. What if the user types an alphabetic word instead
of a number?
11. Ask the user to enter a 4 digit integer, and generate the following printing pattern (if the typed number is 1234):
1234
234
34
4
What if the user types an alphabetic word instead of a number?
12. Ask the user to enter the edge of a cube (real variable), calculate the base area, the lateral area and the total surface
area. What if the user types letters instead of digits?
13. Ask the user to enter a date (year, month, day) and calculate the number of seconds elapsed from January 1, 1970 to
the given date. Consider in both dates that the starting hour is 00:00.
14. Ask the user to enter the successive ales revenues of a department store from Monday to Saturday. Calculate the
weekly average.
15. A runner participating in Vig-Bay (21.5 km) wants to know his/her statistics after the race. The program must ask
the user to enter the following data:
• Starting time: hour, minutes, seconds.
• Finish time: hour, minutes, seconds.
The program must show as a result:
• His/her average speed, expressed in km/h, with 1 decimal digit (example: 11.3 km/h)
• His/her average running pace, expressed as minutes per km, with the MIN:SEC format (example: 5:19
min/km).
10