CS 11

Writing a Program From Scratch

This page shows you the steps required to write, compile, run, and test a program.

The outline is pretty simple:

  1. Login to the system
  2. Make a folder for the project
  3. Figure out a procedure to solve the problem
  4. Write the source code
  5. Compile the source code (fix errors)
  6. Run the program
  7. Test the program
  8. Correct the errors

The following sections walk you through this process for a simple program to print the sum of two numbers.

a. Login

Use ssh or puTTY to connect to homework.cs.tufts.edu using the appropriate software.

b. Make a Folder

Once logged in, make a folder under your cs11 folder. In this case, we will be making a folder called starter.:

cd cs11
mkdir starter
cd starter 
If you did not call your folder cs11, you need to use the correct name in that first command. Open your cs11 folder in Atom. In the Project column, right click on cs11. Then, got to Remote Sync PRO > Download Folder.

c. Figure Out a Procedure to Solve the Problem

We're not going to give you any guidance on this now. You'll get lots of practice over the term. For now, I'll assume we've done this. Do not try to do this and write source code at the same time!

d. Write Some Source Code

In Atom, right click on your starter folder, and select New File. Enter the name adder.cpp and press the return key. Now there will be an empty file, giving you a place to type in your code. Type in this code:

//
// adder.cpp
// purpose:  add two numbers
//      by:  your name here
//    date:  the date here
//
#include <iostream>

using namespace std;

int main()
{
        double a, b, sum;
        cout << "Enter two numbers: ";
        cin >> a;
        cin >> b;
        sum = a + b;
        cout << "The sum is " << sum << endl;
        return 0;
} 
The indenting does not matter to the computer, but it does matter to humans, like your graders and your instructor. Indent it as shown (there are 8 spaces for each level of indentation in the example above).

Now save your work by pressing command-S for Mac or Ctrl-S for Windows (the same way you would save a Word Document or any equivalent). This will upload your adder.cpp file to the Halligan servers. If you go to your terminal or puTTY application and type ls, you should see the file adder.cpp listed within the current directory. If you make any changes to the file, just save it again in the same way and the changes will be uploaded to the Halligan server.

e. Compile

To translate your C++ source code to an app that can run on the computer, you need to compile the source code. Type:
g++ -o adder -Wall -Wextra -Werror adder.cpp
This command runs the g++ compiler on the source file called adder.cpp asking for all warnings (-Wall), even extra warnings (-Wextra), and to turn all warnings into errors (-Werror).

If things go well, there will be no messages printed.

If you made any typing mistakes, the compiler will print out its complaints. If there are complaints, then go back to step the previous step and make the needed changes.

f. Run

To run this program, type

./adder
    

which means to run the program named adder, which can be found in the current directory (.).

g. Test

Now the program is waiting for the two numbers. Type in two numbers separated by a space:

      5.2 7.3
    
The output should show that the sum is 12.5

h. Correct Errors

If the program contains any logic errors, the result will not be correct. If this happens, first, think about what happened. Trace the code based on the input followin the code precisely (not what you think should happen, but what actually happens!). Then go back to the editing step and make changes to correct the error.

That's It

The most difficult parts of using computers to solve problems are:

This introduction does not explain either of the first two parts; it just shows you how to type in code, compile it, run it, and test it. We'll gain lots of practice with all these steps over the semester.

Mark A. Sheldon (msheldon@cs.tufts.edu)
Last Modified 2022-Jun-13