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:
This process is the basis for the remainder of this document.
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++.
Create a directory structure for this class
host% cd
host% mkdir cis121
host% cd cis121
host% mkdir intro
host% cd intro
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.
host% xcoral &
You should see a window that has a standard menu ball and a toolbar of icons.
/* 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;
}
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.
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.
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.
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.
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)
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
Wouldn’t it be nice to simply click an icon and start programs like xcoral and mozilla. Well in GNOME, this can be done:
host%
which xcoral
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.