You are here:



5) argc is an integer that displays the number of arguments on the command line, what does it mean to be an argument on the command line?

6) Can you show some way to use argv and argc in a program?

7) what does the following 3 functions do exactly? atof(), atol(), and atoi(). Can you show them in examples?

8)Can you explain this program, this is the QuickSort:
#include <iostream>
#include <cstring>
Using namespace std;

Void quicksort(char *items, int len);
Void qs(char *items int left, int right);

Int main() {
 Char str[] = “kljaehreagthtgw”;

 Cout << “Original Order: “ << str << ‘\n’;
 Quicksort(str, strlen(str));

 Cout << “Sorted Order: “ << str << ‘\n’;

 Return 0; }

Void quicksort(char *items, int len)  qs (items, 0, len-1)

Void qs(char *items, int left, int right) {
 Int I, j;
 Int x, y;

 I = left; j = right;
 X = items [(left + right) / 2];

   While((items[i] < x) && (I < right)) i++;
   While ((x < items[j])&&(j > left)) j--;

   If(I <= j) {
       Y = items [i];
        Items [i] = items [j];
       Items [j] = y;
       j--; }
 While (I <= j);

 If(left < j) qs (items, left, j);
 If(I < right) qs (items, I, right); }

#include <iostream>
Using namespace std;

Int fact(int n);
Int factr(int n);

Int main() {
 // use the recursive version.
 Cout <<  “4 factorial is: “ << factr(4);
 Cout << ‘\n’;

 // use the iterative version.
 Cout << “4 factorial is: “ << fact(4);
 Cout << ‘\n’;

 Return 0; }

Int factr(int n) {  //recursive version, please explain how this work
 Int answer;

 If(n==1) return (1);
 Answer = factr(n-1) * n;
 Return (answer);   }

Int fact(int n) { // iterative version, please explain this as well
 Int answer, t;

 Answer  = 1;
 For (t = 0; t <= n; t++) answer = answer * (t);
 Rturn(answer); }

I'm not sure about the way the about code is programmed, can you explain it in a detailed way? Thank you so much,


Wow, another bunch of questions! :)

5. When you execute a program, you are calling a command-line executable, whether you're aware of it or not (due to the operating system's interface generally making this transparent to the user).  What this means is that when you click on the icon for solitaire in Windows XP, for example, it executes the program sol.exe.

When you execute a program, you can add parameters for the execution after the program's executable name.  Back in the days of DOS, this was extraordinarily common.  In Unix and Unix-based operating systems, this is still extraordinarily common.  Even in Windows programs, it's still fairly common to support command-line arguments.  If you were executing a program manually from a command prompt, you would simply add arguments like this:

   program.exe argument1 argument2 argument3

You can also add arguments to a program's execution (in Windows) by bringing up the properties of the shortcut.  Then, in the "Target" box, after the program executable's file name, you can simply add arguments.  In Visual Studio, you can add command-line arguments to your own program by bringing up the project properties, going to the Debugging option under Configuration Properties, and typing any arguments into the "Command Arguments" box.

Interpretation of command-line arguments is entirely up to the programmer.  Some applications require /'s before command-line arguments.  Some require -'s before command-line arguments.  This is all up to the programmer and whatever convention she wishes to use.

6. A simple program to work up using argc and argv is this:

   #include <iostream>
   using namespace std;

   int main(int argc, char *argv[])
       if (argc > 1)
         cout << argc << " arguments were passed to the program." << endl
         << "These argumnents are:" << endl;
         for (int i = 1; i < argc; ++i)
         cout << "\tArgument " << i << ": " << argv[i] << endl;
         cout << "No command-line arguments were passed to the program." << endl
         << "Why don't you try passing some so you see what this program really does?" << endl;

Notice that I check that argc > 1.  This is because argc is always at least 1, and the first value in argv is the name of the program.

7. Those functions are all used for converting strings to numeric values.  They're all used in the same way: you have some string that contains a numeric value, you call atoi with the string as the parameter, it returns the numeric value inside the string.  Something like this:

   const char[] numString = "1024";
   int value = atoi(numString);

8. To be perfectly honest, I so rarely use sorting algorithms that I always have to look them up whenever I need one.  Some of them are extraordinarily easy to understand, and some are less so.  Quicksort is a popular sorting algorithm, thought (incorrectly) by some to be the fastest known sorting algorithm.  Someone far cleverer than myself came up with it.  I recommend you read the Wikipedia article about Quicksort to see what it's all about:

9. I've not seen this example of recursion versus iteration.  I like it.  It's simple.

To understand this code, let's first make sure we're on the same page about what a factorial is.  A factorial is the product of the integer in question and every positive integer less than the integer in question.  This means that 3! is 6, because it's 3 * 2 * 1.  4! is 24 (4 * 3 * 2 * 1).  Ok, now that we have that out of the way, let's look at the two versions of factorial implemented here.

   int fact(int n)
       int answer, t;

       answer  = 1;
       for (t = 0; t <= n; t++)
         answer = answer * (t);

Iterative version first.  OK, let me start by saying that this code is wrong.  This code will always return 0, because the loop starts at 0.  0 multiplied by anything is 0, then you carry that 0 over to every other multiplication, resulting in 0.  You can either start the loop at 1, or start the loop at 2 (you can start at 2 because answer starts at 1, so you already have the 1 required for the factorial.  Starting at 1 is less correct, technically, and an extra (useless) iteration, but it will work because 1 * 1 is just 1 again.

Now the actual explanation of what's happening in the code.  Remember that a factorial is every number from 1 to the target number multiplied together.  So, you just set a loop to go from 1 to the target number (or 2, again, since the 1 is covered when answer is set to 1).  In this manner, you will get the needed multiplications.

   int factr(int n)
       int answer;

         return (1);
       answer = factr(n-1) * n;
       return (answer);

Now the recursive version.  This code would actually work to generate a factorial.  Some people have difficulty wrapping their mind around recursion.  It can be a bit weird, but it's extraordinarily handy sometimes.  One thing you need to include in a recursive function is an end point bailout.  That is covered here by the section:

         return (1);

Here it checks to see if the parameter passed in is equal to 1, and, if so, simply returns the parameter.  Much like the iterative version, you could actually use 2 here, since the 1 part of a factorial is pretty useless.  It's just a tiny optimization, not important for our purposes.

OK, so we know the definite endpoint of the recursion in this case, n == 1.  The next thing you need in recursion is the parameter passed to the function needs to change somehow each time the function is called.  In this case, the parameter is changed by simply subtracting 1 from it:

   answer = factr(n-1) * n;

Notice that when factr is called again from within this function, it's called with n - 1 as the parameter.  So, the way this recursive factorial is set up, each call of the function will call factr using n - 1 as the parameter.  The first one to return will be when n == 1.  Then the calling function (with n == 2) will complete its multiplication procedure and return the answer.  Then n == 3 will do the same, then ... etc.  You get the idea.

OK, I think I answered all of your questions.  If you have further questions or if I left anything out, just let me know!


All Answers

Answers by Expert:

Ask Experts


Joseph Moore


I've been programming in one form or another since my brother taught me BASIC when I was 6. I've been programing professionally since I was 20, first web development with HTML, JS, DHTML, CSS, etc., then I became a video game developer, writing code in C, C++, C#, SQL, assembly, and various scripting languages. I've even written my own scripting languages, custom designed for the games I was making. I also dabble in Java, PHP, and Perl. I've worked on pretty much every aspect of game development, including graphics, audio, gameplay, tool, UI, input, animation, and physics.


I've been writing C++ code for 12 years, both on my own in my spare time and professionally.


Bachelor of Science in Game Design and Development, Full Sail University, Winter Park, FL

Awards and Honors
Salutatorian and Advanced Achiever Awards at Full Sail; Independent Games Festival Student Showcase winner, 2004; Featured article on Gamasutra about an experimental game developed in 2004

©2017 All rights reserved.