Sie sind auf Seite 1von 12

Centre for Foundation Studies in Science, University of Malaya

Semester 2 Session 2017/2018


FAC1003/FJAF0113 Programming II
Tutorial 1

PART A: Introduction to Code::Blocks and setting up a project

1. Begin the exercise by clicking the START button from Window Taskbar and select Programs.
Click the Code::Blocks program. This will bring up the template window below.

Code::Blocks is an Open Source free Integrated Development Environment (IDE) which is easy
to use. Code::Blocks has a C editor and compiler. It will allow you to create and test your
programs from one easy to use application.

2. Setting up a project allows you to keep track of all the files in an organized way. To create a
project, click on the Create a new project. (Refer the red circle at the above figure).

3. This will bring up the New from template window. Opening (clicking on) Console application will
then allow you to write a program on the console. The other applications are for developing
more advanced types of applications. After selecting Console application, click on the Go button
to begin using Console Application Wizard.

1
Centre for Foundation Studies in Science, University of Malaya
Semester 2 Session 2017/2018
FAC1003/FJAF0113 Programming II
Tutorial 1

4. Press Next to go to the next step.


5. The next window allows you to choose the language that you will use. Select the language as C,
then press Next.

6. Start by filling in the Project Title. For example, type Tutorial 1 as the Project title. You will
notice that the Project Filename automatically becomes the same name but it contains
Code::Blocks Project file (.cbp) named Tutorial 1.cbp.

7. Next, fill in the Folder to create project in: as follows:


D:\FJAF0113\TUTORIAL_GROUP_YOUR_NAME

For example, if your name is Lisa Surihani from group C3, thus
D:\FJAF0113\C3_LISA_SURIHANI

When the directory for your project has been selected, click on the Next button when done.

2
Centre for Foundation Studies in Science, University of Malaya
Semester 2 Session 2017/2018
FAC1003/FJAF0113 Programming II
Tutorial 1

8. The next window will be the Compiler screen. This specifies where the Debug and Release
compiled versions of your program will be placed. Leave this setting alone and press Finish.

9. The system will then return to the [Tutorial 1] window and you are ready to write your program.
It should be noted that the Build target is Debug, which will allow you to use the debugger to
find errors. In the Management area of the screen (Shift-F2 toggles the Management display),
you will see the files that are part of the project in the Projects tab. To see the source files, click
on the plus [+]’s to expand the Workspace and its subdirectories. Under Sources, there is a file
called main.c, which is automatically created for you when you build a console application.

3
Centre for Foundation Studies in Science, University of Malaya
Semester 2 Session 2017/2018
FAC1003/FJAF0113 Programming II
Tutorial 1

PART B: Compiling the program.

1. Double click the main.c file and it will appear in the window with line numbers.

2. Compile your file from the Build pull-down menu by clicking on Compile current file (Ctrl-Shift-
F9). To compile a file means to take the instructions that you have written and translate it into
machine code for the computer to understand.

keyboard shortcut

3. Test the project from the Build pull-down menu by clicking on Build and run. This step will build
an executable file for you. A project build will take the compiled versions of your source files and
combine them into one program.
You are able to press F9, which is a keyboard shortcut that will build your project and run it at
the same time. The message window will indicate if there are any errors during a compile or
build phase.
4. This is the output from the Tutorial 1 program. Pressing any key will exit the program.

5. When you are done, save all your files by pulling down the File menu and clicking on Save all
files(Ctrl-Shift-S). Now you can select to save the project. After that, select Close workspace.

4
Centre for Foundation Studies in Science, University of Malaya
Semester 2 Session 2017/2018
FAC1003/FJAF0113 Programming II
Tutorial 1

PART C: Open a project

1. From the File menu, select Open. From the All files(*.*)in the window, select “Code::Blocks
project files” and then select the .cbp file pertaining to your program. Press Open when done.
The project has reopened.

All files(*.*)

2. You may also open a project directly from Window Explorer by double-clicking on the file with
the .cbp extension.

PART D: Creating a new file.

1. If you are creating a new file, you can use the pull-down File menu, open New and then an
Empty file.

2. You will be asked if you want to add this file to the project. Choose Yes.

5
Centre for Foundation Studies in Science, University of Malaya
Semester 2 Session 2017/2018
FAC1003/FJAF0113 Programming II
Tutorial 1

3. Code::Blocks will ask for a file name to save the file. Give a name to the file. Here it is called
Question1.c. The files need to be of Save as type C/C++ files. Press Save to save the file.

4. Press Select All to have this file saved as both Debug & Release targets. Press OK when done.

5. The Sources now has Question1.c as a source file in addition to the main.c file. Copy the C
source code below in the window with line numbers.
/*Print a message*/
#include<stdio.h>
int main(void)
{
printf(“Welcome\nTo\nPASUM\n”);
return 0;
}

6. When finish, before compiling, build and run the program, you must remove the main.c file
since the main.c is not needed for your project.

6
Centre for Foundation Studies in Science, University of Malaya
Semester 2 Session 2017/2018
FAC1003/FJAF0113 Programming II
Tutorial 1

7. From the Project menu, select Remove files. Place a check mark next to any file(s) that you wish
to remove. Press OK when you are done.

8. You will need to confirm that you wish to remove the file(s). Press YES, if you are sure you want
to remove them. Otherwise press No.

Don’t worry, the main.c file is still remaining in the D:\FJAF0113\C3_LISA_SURIHANI\Tutorial 1


folder. It is only been removed from the sources folder in workspace. Code::Blocks can only
compile, build and run the .c file one at a time.

9. Now you can compile, build and run the Question1.c file. The output is

7
Centre for Foundation Studies in Science, University of Malaya
Semester 2 Session 2017/2018
FAC1003/FJAF0113 Programming II
Tutorial 1

10. To practise, create another New file (you have to repeat PART D, steps 1 – 4). Name the file as
Question2.c. The Sources now has Question2.c as a source file in addition to the Question1.c
file. Copy the C source code below in the window with line numbers.

/*Calculate the product of three integers*/


#include<stdio.h>
int main(void)
{
int x, y, z, result; /*declare variables*/

printf(“Enter three integers:\n”); /*prompt*/


scanf( “%d%d%d”,&x,&y,&z); /*read three integers*/
result = x*y*z; /*multiply values*/
printf(“The product is %d\n”,result); /*display result*/
return 0;
}

11. Again, you have to remove Question1.c from Sources file in order to compile, build and run
Question2.c. When you were done, the output should be

PART E: Add files to project

1. If you have a project with additional existing files, go to the Project menu and select “Add files”.
This will bring in the files associated with your program.

8
Centre for Foundation Studies in Science, University of Malaya
Semester 2 Session 2017/2018
FAC1003/FJAF0113 Programming II
Tutorial 1

2. Clicking on Add files to project will bring up a window so you can browse to where your files
that you wish to add here. For the moment, select main.c file and press Open. The file will then
be added to your project.

3. Multiple selection Window will appear. Press Select All to have this file saved as both Debug &
Release targets. Press OK when done.

4. To get the output again for themain.cfile, you mustremove the other Sources file(s). Then go to
Build menu and select Rebuild (Ctrl-F11).

9
Centre for Foundation Studies in Science, University of Malaya
Semester 2 Session 2017/2018
FAC1003/FJAF0113 Programming II
Tutorial 1

5. You will be asked if you wish to rebuild the project. Choose YES, if you are sure you want to
rebuild the entire project. Otherwise press No.

6. Then, press F9 to build and run the main.c file. The output window will appear which is same as
before (PART B No.4).

-End of tutorial 1-

Note:
Phew, that was a lot to absorb, so maybe you could read it again to be sure and be more familiar to
Code::Blocks!
But knowing how these steps work is only half the battle.
Figuring out how to write the C code for the next tutorial questions can be quite hard.
So, good luck!!

10
Centre for Foundation Studies in Science, University of Malaya
Semester 2 Session 2017/2018
FAC1003/FJAF0113 Programming II
Tutorial 1

1. Write a program that inputs three different integers from the keyboard, and then prints the
sum and the average of these numbers. Here is a sample input/output dialog for your
reference.
Enter three different integers: 27 14 13

Sum is 54

Average is 18

2. Write a complete C program to print the corresponding Fahrenheit to Celsius table from 0 to
300 degrees by using the idea of subprogram.

celsius = K * (fahr - m); where K = 5.0/9.0 and m =32.0

Use #define to give meaningful names to significant constants.

3. The force F due to gravity between two bodies of masses m 1 and m2 is given by the

formula

Gm1m 2
F
r2
where G = 6.673 x 10-11 Nm-2 kg-2, r is the distance between the bodies (in metres),

and the masses m1 and m2 are measured in kilograms. Write a program that evaluates the

force of gravity between two bodies given their masses and separation. In your program you

should use the :

i) object like macro for representing the constant


ii) function like macro for representing the function F

4. In the given below code, what will be the value of a variable x?

#include<stdio.h>

int main(void)
{
int y = 100;
const int x = y;
printf("%d\n", x);
return 0;
}

11
Centre for Foundation Studies in Science, University of Malaya
Semester 2 Session 2017/2018
FAC1003/FJAF0113 Programming II
Tutorial 1

5. Following is the invalid inclusion of a file to the current program. Identify it.

A - #include <file>

B - #include “file”

C - #include < file

D - All of the above are invalid.

6. For the below definition what is the data type of ‘PI’

#define PI 3.141

A - Its float

B - Its double

C - There is no type associated with PI, as it’s just a text substitution

D - Syntax error, semi colon is missing with the definition of PI

12