You Just Might Be A Programmer!
by Robert Burns, Diablo Valley College

You don't need to be a math whiz...
You don't need to be Spock-like logic monster...

But...

Do you want to find out? Here's an exercise that will help you decide whether YOU might be a programmer: There will be three steps to this exercise:

  1. Download and install a (free) C++ "compiler" over the Internet.
  2. Write programs to simulate the roll of two six-sided dice, deal a poker hand, convert Fahrenheit to Celsius, and find the area of a circle.
  3. Compile and run these programs.

This contains everything you need for writing computer programs in C++! It has Internet links for downloading and instaling free software. It also has some programming exercises to get you started on your way to becoming a programmer.

First find a "text editor" on your computer -- you can use it to write your programs. Download and install the software you will need for "compiling" C++ programs, using the instructions provided below. Then type the computer language "code" for each of the programs listed below. Compile them into working "programs", and run them -- see what happens!

This may be enough to help you decide if programming is for you!


First Things First: Getting The "Compiler"

Here are the instructions for installing a compiler whether you use Linux, Apple, or Windows:

For Linux Users
Linux users probably already have the GNU compiler installed. To find out if GNU is installed, go to a "command prompt" and enter the command g++. (That is, type g++ and press the ENTER key.) If the command is recognized by Linux, as indicated by the message "no input files", then you have the GNU C++ compiler.


For Apple Users
Apple users can download XCode from Apple, which includes the GNU compiler. Click here to go to the XCode download page. You'll have to become a member of "Apple Developers Connection", but it's free. Put XCode's DMG installation file on your desktop, and run it to install XCode.


For Windows Users
Windows users can download VC++ Express from Microsoft for C++ compiling.

1 Using a browser that accepts cookies, click here to get to the Microsoft "developer" website...

2 ...scroll to the Download link and click it...

3 ...and follow the directions to download and install the software.

Now you should be ready to write some programs!


First Program: Simulating The Roll Of Two Six-sided Dice

Okay -- we are ready to program! This program is one of the easy ones, because it requires no input from a user. You just run the program, and it simulates the roll of two dice. Run it again for another roll, etc.

You will need a text editor. Any text editor will do, including Windows Notepad, Linux vi, and Apple TextEdit. These are general-purpose text editors, not specifically designed for use in programming, but they work just fine. (For details on setting up Apple TextEdit, go to http://support.apple.com/kb/TA20406?viewlocale=en_US.

(Word, Windows WordPad, and other editors capable of "rich text formatting" are not very suitable for writing code. They are page-oriented, and they embed formatting information into the files that they produce. While it is possible to configure such editors for text editing, it is better to avoid them.)

Type (or copy/paste) the following computer "code" into a text editor.

Rolling Two Six-sided Dice
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
 
int main()
{
  srand(time(0)); // "seed" the random number generator
 
  int a; // the name of the first die
  int b; // the name of the second die
  int total; // the result
  a = 1 + rand() % 6;
  b = 1 + rand() % 6;
  total = a + b;
 
  cout << "Dice result: " << total
    << " (" << a << " and " << b << ")" << endl;
 
  return 0;
} // main

Save the file as diceRoll.cpp. In Windows, be sure to put c:\ in front of the file name, and enclose the whole thing in quote marks!

Now get a "command prompt" -- in Apple, use the "Terminal" application. In Windows, do Start|All Programs|Visual C++ 9.0 Express Edition|Visual Studio Tools|Visual Studio 2008 Command Prompt, like this:

In the Windows command window, enter this command so that you can use the root folder of your c: drive (actually, you can use any drive and folder that you wish!):

cd\

Then enter these commands to compile and run your program -- and yes, uppercase or lowercase of letters does matter!:

cl diceRoll.cpp -EHs
diceRoll
that's see-EL, not see-ONE

Here's what it should look like for Windows (be patient, Apple and Linux users -- your instructions are found just below this):

(You type what's in yellow, and press the ENTER key to send the command to the computer.)

In Apple and Linux, type these commands -- note the dot-slash, and remember to use it in each of the other examples:

g++ diceRoll.cpp -o diceRoll
./diceRoll
that's dash-OH, not dash-ZERO

Did it work? That's your first C++ program!


Second Program: Converting Fahrenheit To Celsius

This might be interesting: if you can do this, then you can write programs to solve do lots of your physics and science problems. This program requires typed keyboard input from a user.
Use the instructions from the first program, and create the following file. Type (or copy/paste) the following computer "code" into a text editor.

Converting Temperature
#include <iostream>
using namespace std;
 
int main()
{
  double c; // degrees Celsius
  double f; // degrees Fahrenheit

  // ask user to enter Fahrenheit
  cout << "Enter the temperature in degrees Fahrenheit: ";
  cin >> f;

  c = 5 * (f - 32) / 9;
  cout << " That's " << c << " degrees Celsius!" << endl;

  return 0;
} // main

Save the file as f2c.cpp. In the Windows command window, enter these commands to compile and run your program:

cl f2c.cpp -EHs
f2c

In Apple or Linux, enter these commands to compile and run your program instead:

g++ f2c.cpp -o f2c
./f2c

Here's what it should look like for Windows:

(You type what's in yellow, and press the ENTER key to send the command to the computer.)


Third Program: Calculating The Area Of A Circle

Ready for another? This program also requires typed keyboard input from a user.
Use the instructions from the first program, and create the following file. Type (or copy/paste) the following computer "code" into a text editor.

Calculating Area
#include <iostream>
using namespace std;
 
#include <cmath>
 
int main()
{
  double r;
  double area;
  double pi = 4 * atan(1.0); // 4 times the angle whose tangent is 1

  // ask user to enter a circle's radius
  cout << "Enter the radius of a circle: ";
  cin >> r;

  area = pi * r * r;
  cout << " The area is " << area << endl;

  return 0;
} // main

Save the file as area.cpp. In the Windows command window, enter these commands to compile and run your program:

cl area.cpp -EHs
area

In Apple or Linux, enter these commands to compile and run your program instead:

g++ area.cpp -o area
./area

Here's what it should look like for Windows:

(You type what's in yellow, and press the ENTER key to send the command to the computer.)


Fourth Program: Dealing A Poker Hand

This program deals a hand of poker, with 5 cards. Like the first program, this one also requires no input from a user. You just run the program, and it lists 5 cards. Run it again for another deal, etc.
Use the instructions from the first program, and create the following file. Type (or copy/paste) the following computer "code" into a text editor.

Dealing A Poker Hand
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
 
int main()
{
  srand(time(0)); // "seed" the random number generator

  int n = 5; // deal this many cards
  for (int i = 0; i < n; i++)
  {
    int value = 1 + rand() % 13;
    int suit = rand() % 4;

    switch (value)
    {
      case 1:
        cout << "Ace";
        break;
      case 11:
        cout << "Jack";
        break;
      case 12:
        cout << "Queen";
        break;
      case 13:
        cout << "King";
        break;
      default:
        cout << value;
    } // switch

    switch (suit)
    {
      case 0:
        cout << " of Spades" << endl;
        break;
      case 1:
        cout << " of Hearts" << endl;
        break;
      case 2:
        cout << " of Diamonds" << endl;
        break;
      case 3:
        cout << " of Clubs" << endl;
    } // switch
  } // for

  return 0;
} // main

Save the file as pokerHand.cpp. In the Windows command window, enter these commands to compile and run your program:

cl pokerHand.cpp -EHs
pokerHand

In Apple or Linux, enter these commands to compile and run your program instead:

g++ pokerHand.cpp -o pokerHand
./pokerHand

Here's what it should look like for Windows:

(You type what's in yellow, and press the ENTER key to send the command to the computer.)


That's it! That's the whole exercise. If you liked that, you just might be a programmer!

You may want to start your programming education at DVC. We have three possible starting places: COMSC-100, -105, or -110, depending on your aptitude and preparation going into this. If you did really well with the exercises presented here, and there's no holding you back, COMSC-110 may be your starting place. But if you had trouble getting off the ground with this, and you want to know more, COMSC-100 is for you. If you are in between these extremes, consider starting at COMSC-105.

Here's a link to a diagram that shows the sequence of programming classes offered at DVC: www.dvc.edu/org/departments/computer-science/computer-science-sequence.pdf.