The main purpose of this lab is to introduce you to the computing environment you will be using. The ideas in this lab will be used throughout this course, so you should make every effort to understand not only what but why you are doing what you are doing at each step.
The manner in which you get into the environment used to create, compile, and execute a program differs from one school to another, on the kind of computers being used, whether they are networked or stand-alone, whether a security system is in place, and so on. Your instructor will give you information on how to get started.
The lab exercises and programming projects will require writing C++ programs to solve problems, getting them to execute, and then using them to solve the problems. In this lab exercise, you will be using the program below that accepts an integer entered during execution and outputs it in base-ten (decimal), base-eight (octal),and base-sixteen (hexadecimal) notation.
This is the program that you will be using in this lab exercise. Your instructor will inform you how to
/* bases.cpp demonstrates some basic features of input and output in C++. Written by: Jane Doe, Feb 29, 2012 for CS I at Universal University. Input(keyboard): aNumber, an integer; Output(screen): the base 10, 8 and 16 representations of aNumber. -----------------------------------------------------------------------*/ #include <iostream> using namespace std; int main() { // Declare an integer variable to store the input number int aNumber; // 0. Print a message explaining the purpose of the program. cout << "\nThis program inputs a base-10 integer" << "\n\tand displays its value in bases 8 and 16\n"; // 1. Ask the user to enter an integer. cout << "\nPlease enter an integer: "; // 2. Input the integer, storing it in variable aNumber. cin >> aNumber; // 3. Output the base-8 and base-16 representations of aNumber. cout << "\n\nThe base-8 representation of " << aNumber << " is " << oct << aNumber << ",\n\tand the base-16 representation is " << hex << aNumber << "\n\n"; }
Question #1.1: What do you think the symbols\n
and\t
do in the program?(Note: You don't have to be right yet; you're simply making a conjecture.)
Hint: they do slightly different things, but they both have an effect on the output of the program.
Question#1.2: Describe an experiment you can use to test your conjecture.
(Suggestion: Copy and paste the parts of the program that show your changes into a document you will hand in.
Question #1.3: Now conduct your experiment and describe what happens.
What results did you get? Do they confirm your conjecture?If so, design and carry out a tougher experiment to test the hypothesis further. Otherwise, start over with Question 1.1 with a new conjecture and try it all again.
Question #1.4: What conclusion have you reached about the\n
and\t
symbols?Question #1.5: Why do you suppose that these symbols are known as whitespace characters?
int main() {in the program bases.cpp, write a program statement that begins cout << and that will produce output something like the following (on 3 separate lines) but as specified by your instructor:
Your Name CPSC 104Xwhere Month-name is replaced by the name of the current month, dd is replaced by the day number of the date you are doing this, and yyyy is replaced by the year.
Month-name dd, yyyy
Lab 1
Be sure to put a semicolon at the end of your statement.