Sei sulla pagina 1di 3

Introduction to Programming Environments (C++/Unix)

Lab Exercise – 01C


Year 1 Semester 1, 2010

Objectives:
The objectives of this lab exercise are as follows.
1. Practicing commands in Linux
2. Running a C++ program

Note : In this lab you are required to follow the lab sheet given below. You can get help
from your instructors to complete portions, which you find difficulties. You should try to
complete all the exercises given in the Lab Sheet.

Exercise 1 – Practicing commands in Linux

1. Load up the Linux environment.


2. Right-click on your desktop and select “New Terminal” from the pop-up menu.
3. Look at the following diagram and create the directories and files as given

Student

DIT_10_C1_000 ( Make a Directory by


your Student number)

IPE CF TC

Week 1

File1.txt File2.txt File3.txt

-1-
4. Do the following tasks
1. Copy file1.txt from “IPE” directory to “CF” directory and rename it as
“test1.txt”.
2. Copy file2.txt from “IPE” directory to “Week 1” directory in “TC” directory
and rename it as “test2.txt”.
3. Copy file3.txt from “CF” directory to “IPE” Directory.
4. Go to the DIT_10_C1_000 Directory and copy the “file1.txt” to the “TC”
Directory.
5. Remove the “IPE” Directory.

Exercise 2 : Running a C++ program

1. To get into a vi editor type vi lab1ex2.cpp and enter. (Note: lab1ex2.cpp is going to
be the name of your program. You may also use a name of your choice. But do not
change the extension, .cpp)

2. Type the following Program, which converts the temperature in Fahrenheit to Celsius.

//This program will convert temperatures between


//Fahrenheit and Celsius

#include<iostream>
using namespace std;

int main()
{
double Celsius, Fahrenheit;
Fahrenheit = 95.0;
Celsius = (Fahrenheit - 32) * 5.0/9.0;
cout<<Fahrenheit<<”degrees Fahrenheit is”<<Celsius;
cout << ”degrees Celsius.\n”;
return 0;
} //main

Note: If you are not familiar with the vi editor type vimtutor in a different terminal to
open the tutorial. This tutorial explains enough commands for you to easily work with the
vi editor.

3. Save the program

-2-
Once the program has been created using the vi editor, you can exit from the vi
editor and get back to the command prompt by pressing the Esc key and “ : “ key
consecutively. Then type wq and press enter.

4. To compile the program use g++ command as specified


(Eg: g++ -o <object name> <source file name> ).
E.g. :- g++ -olab1ex2 lab1ex2.cpp

5. Execute the program


If there are no compilation errors or warnings you can execute the program.
If there are compilation errors, reopen the file using the vi editor and fix the errors
so that your program looks exactly the program in the handout.

To execute the program use the following command


./lab1ex2

-3-

Potrebbero piacerti anche