CS115 Lab 1 Exercises:
If you have not already done so, please create a directory for this lab.
You can follow these steps:
- mkdir cs115
- cd cs115
- mkdir lab1
- cd lab1
Programming Exercise 1
Transfer the file
from the CS Dept's anonymous FTP site to your account space. Do that by:
- Entering the command:
cp /net/data/ftp/pub/class/115/ftp/cpp/hello.cpp hello.cpp
- OR, by using the command line FTP program using this information:
The path: pub/class/115/01-unix/
The file: hello.cpp
PLEASE avoid copying and pasting code. It makes a mess of formatting and may render the code uncompilable.
Compile and run this C++ program.
Remember use g++ hello.cpp -o hello on Linux, and
CC hello.cpp -o hello on Hercules to compile.
Refer back to the lab notes for a review
if you need to.
Use an editor to make these changes to the hello.cpp program.
Add a couple of integer variables before the user_name variable.
const int ThisYear = 2007; // Current Year
Add these lines following the "Hello" statement.
cout << "How old are you now? ";
cin >> user_age;
user_retire = ThisYear + (65 - user_age);
cout << "You will be able to retire in the year " << user_retire << endl;
You can either type these lines in directly, or you can copy and paste
between the web page containing this assignment and the editor.
Compile your modified program.
If you get syntax errors, go back into the editor and fix them
until you get a clean compile.
Then run the program.
Programming Exercise 2
The prerequisites for CS115 include introductory programming
It is therefore expected that you are familiar with
the concepts of one-dimensional arrays and looping constructs.
If you require a review on arrays and the "for" loop, you can
refer to the cs110 lab notes.
Use a text editor to write a C++ program that uses a one
dimensional integer array to:
- prompt the user to input a set of 5 numbers
- compute the sum of the numbers
- compute the average of the numbers
- output the sum and average
- output the set of numbers.
Compile your program.
Your output should look something like this:
This program will compute the average of 5 numbers.
Enter number 1: 1
Enter number 2: 2
Enter number 3: 3
Enter number 4: 4
Enter number 5: 5
The sum of the numbers is: 15
The average of the numbers is: 3
The numbers are:
Unix/Linux Command Exercise
This exercise assumes that you have created the directories from the
"Preparation" section at the beginning of the lab exercises.
If you have not done so, please
create the directories and ensure that hello.cpp is in
(where yourusername is your
Do the following in the order specified
- Start a script from your home directory
- Print your current working directory
- Change into the cs115/lab1 directory
- Use more to display the contents of hello.cpp
- Make a directory called Part1
- Copy hello.cpp into the Part1 directory
- List the contents of your current directory
- Without changing directories, list the contents of the Part1
- Make a directory called BAK
- Move hello.cpp into BAK
- Change into the BAK directory
- Delete hello.cpp
- Move up one directory (Hint: you can use cd ..)
- Delete the BAK directory
- Exit the script
- Show the script to your lab instructor
- That's it! Have a good week!
This page last modified:
Thursday, 08-Jan-2009 10:21:09 CST
Copyright: Department of Computer Science, University of Regina.