Computer Science 394
Physics 313
Microcomputer Control
Dickinson College
Spring Semester 1999
Grant Braught
Class #2 - Introduction to Programming
Main Ideas For Today 
- C++ Hello World Program
- The main() function
- variables (int, char, double, bool)
- Output with cout
- comments
- operators
- Input with cin
- for loops
Activities 
Complete the following activities with the assitance of the TA and Prof. Braught. Be sure to keep a complete record of what you have done in your lab notebook. (The Lab Notebook specifications are now on-line.)
C++ Hello World Program:
The first program written by almost every programmer since the dawn of programming has been the "Hello World" program. This program does nothing more than display the string "Hello World" on the computer screen. The program listing shown below is a C++ implementation of the "Hello World" program. The "Hello World" program contains a main() function. Every C++ program must contain a main() function. The "Hello World" program provides a good starting point for any C++ program.
- Enter this program and run it using the Turbo C++ Integrated Development Environment (IDE).
#include
void main()
{
cout << "Hello World" << endl;
}
- Change this program so that it prints a customized greating using your name.
- What is the purpose of endl?
- Change this program so that it prints the following:
- Use a DOS Shell to create an "intro" directory on the computer.
- Save your program as "Testing.cpp" in the intro directory.
Variables:
Just about every program you write will need to remember and keep track of some values. This is done using variables. The following program demonstrates several different kinds of variables that can be created using C++.
- Enter and run the following program:
#include
void main()
{
int X = 7; // Make an integer variable named X and store 7 in it.
int myInteger = 9;
double myPI = 3.1415927; // Make a double variable and store PI in it.
char letter1 = 'a'; // Make a character variable and store 'a' in it.
char letter2 = '?';
char aString[26] = "This is a String."; // Make a string variable.
// 25 is the maximum length for this string.
// Always leave 1 extra space for C++ to use.
/* Now lets print out these values
to the screen so that we can see
them.
*/
cout << "The integer X = " << X << endl;
cout << "The double myPI = " << myPI << endl;
cout << "The character letter1 = " << letter1 << endl;
cout << "The string aString = " << aString << endl;
}
You must declare all of your variables before you use them. It is also a good idea to assign an initial value to all of your variables when you declare them. Everything following a // is a comment and is ignored by the computer. It is there solely for your (and my) benefit. Everything between /* and */ is also a comment. This type of comment can extend over several lines.
- Make changes to the above program so that it prints out the values of all of the variables that we have created.
- Add a new variable and initialize its value to your age.
- Add a new variable and initialize its value to your name.
- Add a cout line that prints out "Hello name, I hear you are age years old." where name and age are replaced by using your new variables.
- Change the initial values of your variables and run your program again.
- Save this program as "Variable.cpp" in the intro directory. (Note: File names are limited to 8 characters plus a period and then 3 more characters. The last 3 will be .cpp for all of the programs we are writing in this class.)
Operators:
Your programs are also likely to perform calculations using the values that your program stores in its variables. These calculations will be done using operators.
- Enter and run the following program:
#include
void main()
{
int X = 7;
int Y = 2;
int A = 0;
int B = 0;
int C = 0;
A = X + 7; // Add 7 to X and store it in A
B = Y * 3; // Multiply Y by 3 and store it in B
C = A + B * Y; // Calculate a new value for C
X = X + 1; // Increase the value of X by 1.
Y++; // Shorthand for Y = Y + 1;
cout << "The variable A = " << A << endl;
cout << "The variable B = " << B << endl;
cout << "The variable C = " << C << endl;
cout << "The variable X = " << X << endl;
cout << "The variable Y = " << Y << endl;
}
- Add a new integer D variable D to this program.
- Set the value of D to be: half of the the square of the value stored in C.
- Output the value of D to the screen.
- Save this program as "Operator.cpp" in the intro directory.
Input:
It is often important to allow the user of your program to interact with the program. C++ provides an easy way for your program to accept input from the user. Input in C++ is done using the cin command. The cin command reads input from the keyboard and places whatever is typed into the variable that appears after the >>.
- Enter and run the following program:
#include
void main()
{
int anInt = 0;
double aDouble = 0.0;
char aCharacter = ' ';
char aString[31] = "";
cout << "Enter an integer value: ";
cin >> anInt;
cout << endl << "Enter a double value: ";
cin >> aDouble;
cout << endl << "Enter a character: ";
cin >> aCharacter;
cout << endl << "Enter a string (no more than 30 characters: ";
}
- Modify the program so that the last thing it does is display the values you entered.
- Save this program as "Input.cpp" in the intro directory.
- Create a new version of the "Hello World" program. This version should first ask the user for their name and age and then print the message: "Hello name, I hear you are age years old." where name and age are replaced by the values that were read in from the user.
- Save this program as "NewHello.cpp" in the intro directory.
For Loops:
It is often necessary for your programs to repeat a section of code over and over again. You could simply type that section of code many times but that is not very efficient. Further if you do not know in advance how many times the code must be repeated you will be out of luck. One solution to this problem is the for loop.
- Enter and run the following program:
#include
void main()
{
int i = 0;
for (i = 1; i <= 10; i++)
{
cout << "Line " << i << "..." << endl;
}
}
The for loop works as follows:
- When the for statement is first reached the assignment, i = 1; is performed.
- The condition, i <= 10; is checked. If it is true then the loop body is executed.
- After the loop body is executed the operation, i++ is performed.
- After the operation is performed the condition is checked again and the whole process repeats until the the condition is false.
- Change the for statement in the program to read: for (i = 2; i <= 20; i=i+2)
- Save your program as "For1.cpp" in the intro directory.
- Write a program that:
- Reads a number in from the user.
- Prints out the sum of all numbers less than or equal to that number.
For example if the user enters 5 the output of your program should be:
Sum of numbers 1...1 = 1
Sum of numbers 1...2 = 3
Sum of numbers 1...3 = 6
Sum of numbers 1...4 = 10
Sum of numbers 1...5 = 15
- Save this program as "SumEm.cpp" in the intro directory.