ICTLab07_SettingUpTheEnvironment
ICTLab07_SettingUpTheEnvironment
(CL1009)
LABORATORY MANUAL
Spring 2024
LAB: 07
SETTING UP THE ENVIRONMENT (DEV C++)
______________________________________
Tools needed:
1. PC running Windows Operating System (OS)
2. Internet access
3. Manual
Important Tip # 1:
Students must backup all work saved on the
computer to a cloud server (e.g. Google Drive,
OneDrive, DropBox, etc.). Any work stored in any
folder in the lab computer (including desktop) is
deleted if the power goes down or when you log out
of the station. Without your backup, you will lose
your work. More about this in Lab 02.
Turn on the PC and log into it using your student account information. What is this? Before
you can begin programming a PC, i.e., develop a software, and run it, you need to prepare
the PC and set up an Integrated Development Environment (IDE) that allows you to write a
program (code) using a specific language, find errors in it, prepare it to run on the computer
(compile it) and even help you troubleshoot (debug) it.
Note:
In case the the system requires admin password to uninstall, contact the lab assistant.
Setting up DevC++:
Now we will reinstall up an IDE known as DevC++ to be able to create basic programs
(software) for a Windows based PC.
OR
Use this link directly:
https://sourceforge.net/projects/orwelldevcpp/files/latest/download
Click the green Download button to download Installables folder Dev-Cpp 5.11
TDM-GCC 4.9.2 Setup.exe file.
2. Install it:
Run the downloaded file. It required at least 346.8 MB space on your computers
hard disk's drive C to install it. Continue the installation with default options.
The whole installation process typically takes less than 5 minutes (after the file has
been downloaded and run).
Click Finish and then click Next on the next screens and a final OK to finally run the
DevC++ IDE.
If you close the IDE, you can run it again through the DevC++ icon on Windows
desktop or by clicking Start button and locating the DevC++ program and clicking it.
Click File --> New --> Source File or press keys Ctrl+N (i.e., Ctrl key and N key
simultaneously). A new Untitled1 file will open ready to receive code:
2. Saving code:
For first time save, click File-->Save As to save the file into work folder of your hard
drive or your USB Drive. Give the file a descriptive name e.g. lab1_1.cpp. Code
files of C++ programming language usually end in .cpp extension. This name will
replace the Untitled1 in the tab above the file content area. For subsequent saves,
just press keys Ctrl+S.
Important Tip # 2:
Develop a habit of pressing Ctrl+S or click File--
>click Save whenever you make changes to your
code, because if you don’t do so and the computer
accidentally shuts down you will loose work done
since the last save command.
1 #include <iostream>
2 using namespace std;
3
4 int main()
5 {
6 cout<<"HELLO WORLD"<<endl;
7 return 0;
8 }
Code: lab01_1.cpp
Successful compilation results in showing that there are no errors, no warnings and
the name (path) of the application file. For example:
Important Tip # 3:
Remember the shortcut keys of the tasks you
repetetively do, for example, saving a file
(Ctrl+S), compiling a file (F9), running a code
(F10), compile & run (F11) etc. This will save you
development time.
3. I/O Console:
When you run lab01_1.exe from within DevC++ a black window, called a console
window, appears and handles the input/output (I/O) for the application. In this
course we will only be developing console applications.
Task # 1:
Your task now is to run the lab01_1.exe and write the console output (write everything you
see in the black window):
HELLO WORLD
Run DevC++ IDE now. Open a new code file and save it as lab01_2.cpp.
#include <iostream>
using namespace std;
int main()
{
int a,b,sum;
cout<<"Enter the 1st number: ";
cin>>a;
cout << "Enter the 2nd number: ";
cin>>b;
sum=a+b;
cout<< endl << "Sum of two numbers:"<< sum << endl;
return 0;
}
Code: lab02_1.cpp
This code performs a particular task. Try to understand what it does by running and
interacting with it.
Task # 2:
Run the code lab01_2.cpp with several different inputs and explain what it does.
Inputs Outputs
Task # 3:
Change the code lab01_3.cpp so that it also includes the functionality of subtraction. The
code should display the results as well.
Task # 4:
1 #include <iostream>
2 using namespace std;
3
4 int main()
5 {
6 int x = 10, y = 4.5, z;
7
8 cout << "Enter a number less than 10: ";
9 cin >> z;
10
11 y = x / z + y * 2;
12
13 cout << "y = " << y << endl;
14
15 return 0;
16 }
Code: lab01_4.cpp
Repeat the procedure for running the Program but this time you are also required to enter
an input number in console window.
Enter 6 and check the output. Write the value of Y in the box.
Now solve the same code with your Calculator (or by hand) and write the answer in
the box.
119101 Y=10.6667
Errors in C/C++
Syntax Error.
Run-Time Error.
Logical Error.
As the program didn’t show any error at compile and run-time, it means there is a
Logical error in the program.
Finding a Logical error and correcting it is known as Debugging.
Select a break point, by clicking on the number of desired line. This would be
considered as the starting line for debugging.
This would make the whole line RED coloured.
Now click on <Debug> present in small boxes, on the left of <Add Watch>.
The line immediate after break point would turn blue, shwoing current line
(that is going to be execute).
A Console output Screen would appear.
ICT LAB NUCES, ISLAMABAD Page 11 of
14
Setting up the Environment (DEV C++) LAB: 07
The execution has stopped just before line is to be executed (Blue Line).
Click Next line (or press Alt + N) in the lower pane of the IDE.
Line background will become red and the blue arrow and blue background will
move to the next line. This means line has just been executed.
In order to know what effect a particur instruction (line) has on the state of the
program, we can add a watch window.
In the lower pane click Add watch and enter variable names in it and click OK.
Add all the variables used in your program to the watch window by a similar
process.
Also enter next line till you reach the end of program.
Study the change in values of variable each time you press next line and try to
understand the error in the program.
4.5
Datatype error: We used ‘int' which is only appropriate for whole numbers.
We will replace int with float as we are dealing with decimals. This type of error
can be corrected by declaring the proper datatype