Programming and Graphics

(Kiana) #1

140 Introduction to C++ Programming and Graphics


Problems


5.3.1.Use thesortfunction to alphabetize a list of ten African countries.


5.3.2.Verify by computation that the CPU time of the bubble-sort algorithm
scales withN^2. This means that, whenNis doubled, the CPU time is
multiplied nearly by a factor of four.


5.4 Command line arguments


When execution has been concluded, the main program returns to the operating
system (OS) an integer. Conversely, the main program can receive information
from the operating system with the help of pointers.


To illustrate the protocol, we consider the following code contained in the
fileos.ccand compiled into an executable namedos:


#include <iostream>
using namespace std;

int main(int argc, char * argv[])
{
for (int i=0;i<=argc-1;i++)
{
cout << i+1 <<""<<argv[i] << endl;
}
return 0;
}

Running the executable by typing in the command line:


os

prints on the screen:


1os

The integerargcis an argument counter indicating the number of string vari-
ables (character arrays) passed from the operating system to the main program.
The variables themselves are contained in a string array indicated by the pointer
char * argvholding the argument values. In this case, the argument counter
is one, and the sole component of the string array is the name of the executable.


If we run the executable by typing in the command line:

os is running
Free download pdf