Computer Science I Chapter 1: Getting Started
Lab 2 Work: Due Friday September 12th 2008
Answers should be included in a Word document in the appropritate sections.
To gain experience with
Your initial C++ programs will be contained entirely in one file and there are some elements that they all will have because of requirements of the C++ language. Essentially, these are 1) markers for the compiler - to know where your program begins and ends - and 2) files that your program also needs in order to operate. When you build a program, your compiler looks for code of the form:
#include <iostream> using namespace std; int main() { /* your work goes here */ return 0; }
The textbook has a program that prints the message Hello, World! on the screen.
Try changing it to display Hello, Universe!
Type the program into your compiler's editor, compile and test. Then paste the source code in your word document with heading P1.
/* paste program in your word document with heading P1*/
There are numerous opportunities for error in any program, many times in places that seem too simple to require close attention. What do you think the following program is supposed to do?
Type your answer in your lab document under heading P2
#include iostream.h int main() { double radius = 11; /* centimeters double pi = 3.14 double sphere_volume = (4/3)*pi*(radius * radius ** radius); double surface_area = 5 * pi * radius; cout << "Volume = " < sphere_volume; cout << "Area = " << surface_area; return 0; }
Will it work as it is?
Try compiling the program. What were the results? (Use copy and paste to place a copy of your compiler's error messages here.) Type your answer in your lab document under heading P2
Fix the syntax errors. Place a copy of your program's output here.
The program has two logic errors. Fix them both and paste the corrected program in your word document under heading P2.
/* paste program in your word document under heading P2*/