Introduction to Software Development

Software development is practiced everyday by computer scientists. Software can be developed using any of the hundreds of programming languages that exists. However, the basic process of developing software the same regardless of the language:

  1. Develop an algorithmic solution to the problem i.e. pseudocode
  2. Choose a language to use to implement the solution (C++ in CIS121, but can be other languages in other classes or other situations)
  3. Write/translate the algorithm to the programming language using a text-editor
  4. Compile program using a compiler
  5. Execute program and test it
  6. If program does not work, then repeat steps 3-5

 

This process is the basis for the remainder of this document.

Terminology

A text-editor is a program like a word processor where you can type your programs. The files are saved as plain text and not a special format. You will use an editor named xcoral.

 

A compiler is a piece of software that generates a binary executable program based on the program you wrote in your chosen language. The compiler is g++.

Preliminaries

Create a directory structure for this class

host% cd

host% mkdir cis121

host% cd cis121

host% mkdir intro

host% cd intro

Editing a Program

After you have developed an algorithm in Step 1. Then you will have pseudocode that can be translated to C++.  You are then ready for step 3; writing the program. The text-editor is called xcoral.

 

  1. open a terminal window
  2. start xcoral

host%   xcoral  &

You should see a window that has a standard menu ball and a toolbar of icons.

  1. Some basic xcoral features
    1. To open a file: select the file menu and select open
    2. To open a new xcoral window (to see more than one file): select window menu and select new text window
    3. To save a file: select the file menu and select save or save as.
    4. To navigate in the document: use the arrow keys to move the cursor, or the mouse, page up and page down, or the scrollbar.
    5. To go to a particular line in a file (used when looking for errors): Select the search menu and select Goto Line Number
  2. Now type in the following C++ program

/* Name: <enter your name>

   Program Description: This program is my first C++ program in

   CIS121. It gives the user 3 chancees to guess the random number

   between 0 and 10.

*/

 

#include <iostream>   // I/O cin and cout

#include <string>     // string type

#include <stdlib.h>   // random number generator

 

using namespace std;

 

int main () {

  // variable declarations

  string firstName;   // first name of user

  int num;            // number to guess

  int count = 3;      // count the guesses

  int guess = -1;     // user's guess

 

  // Get input from user

  cout << "Please enter your first name: ";

  cin >> firstName;

 

  // compute the random number to guess

  srand((unsigned)time(NULL));

  num = rand() % 10;

 

  cout << "You have 3 chances to guess the number between 0 and 10"    

       << endl;

 

  // get guesses until 3 wrong guesses or one correct guess

  while  ( (count > 0)  && (guess != num) ) {

    cout << "Enter guess #" << count << ":  ";

    cin >> guess;

    count = count - 1;

  }

 

  // Did the user win or lose?

  if (guess == num) {

      cout << "You Won!!!" << endl;

  } else {

      cout << "Your used all of your guesses" << endl;

  }

 

  // output the actual number, and exit

  cout << "The number was " << num << endl;

  cout << "GAME OVER" << endl;

 

  return 0;

}

 

 

  1. Now save the program as intro.cpp to your intro directory, and got to a terminal window to start the next section.

Compiling and Executing a Program

Now you are ready to compile your program. Note that you may have error messages from mistyping something, the program should not have any errors as presented above. If you have errors, then ask your professor immediately.

 

  1. make sure you terminal is in the proper directory using “pwd” and “ls”

host%  pwd

/home/username/cis121/intro

host%  ls

intro.cpp

 

Yours may be slightly different, but you should see the intro.cpp file which you just saved. If this is not the case, the ask for help.

 

  1. Now to compile the program

host%   g++ intro.cpp –o intro

host%

 

This is where you may have errors. If all goes well, then the “host%” prompt will come back and no errors as shown here. If there are errors, then ask your professor or TA for help.

 

  1. Now execute the program

host% intro

Following the prompts on the screen and play the game. Run it again by typing intro again when the game is over. Now read the program again and make sure you understand how it is doing everything.

 

Under normal circumstances, you may have a program that compiles but does not do what you want it to do. If that is the case, you will need to “debug” the program. This requires tracing the program source code, fixing the code, saving the file again, recompile, and execute. This cycle is done over and over until the program is correct. We will show you techniques and tools in the next lab to help with this.

What is the compiler doing? (optional)

From you lecture, you know that the compiler is converting the C++ program to assembly/machine code. To show you what is happening, do the following from the same directory:

 

1.     compile the program with a special option so that it will output the assembly code in addition to the binary executable.

host%   g++ -S intro.cpp

host%

 

2.     Now this will produce a intro.s file, which you can view by opening it in xcoral

 

Ask your instructor to see the files intro-ppc.s and intro-win.s (assembly language for Intel processors – PCs)

 

Translation of an Algorithm

Here we will translate a simple algorithm into C++. The objective here is to see how its done, in class you will get more details.

 

1. set the value of purchased to 0

2. set the value costly to 0

3. set the value of total to 0

4. set the value of last to false

5. while (last == false) do steps

6.   get the value of the item_name

7.   get the value of the item_price

8.   if item_price < 0 then last = true

9.  else

10.      if item_price > costly then

11.            set costly to item_price

12.      increment purchased by 1

13.       increment total by item_price

13.       print item_name and item_price

14.  end if

15. end while

16. set cost to total * 1.06

17. pirnt “number of items purchased” purchased

18. print “sub total: “ total

19. print “tax: “ 0.06 * total

20. print “Total: “ cost

21. print “Most expensive item: “ costly

 

A Unix Nugget

Wouldn’t it be nice to simply click an icon and start programs like xcoral and mozilla. Well in GNOME, this can be done:

  1. At the top of the screen is a bar called a panel which has icons and menus connected to it. Right click on the panel and choose “+” (to add) and then launcher.
  2. Fill in the first field wit the name xcoral
  3. Open a terminal window, and enter

host% which xcoral

 

  1. Fill in the “command” field of the launcher with the response from the terminal window (it should be something like “/export/usr/local/xcoral/xcoral”)
  2. Click on the icon image, and select an icon for your xcoral launcher
  3. Click “ok”

 

This method for creating launcher icons can be used for other programs, like “mozilla”.

 

Now click on your xoral launcher, and you should see it start up.