Sushant
Sushant

Reputation: 13

Running C++ program multiple times

I have a C++ program which I need to run it multiple times. For example:- Run ./addTwoNumbers 50 times.

What would be a good approach to solve this problem?

Upvotes: 1

Views: 8669

Answers (4)

fdh
fdh

Reputation: 5344

int count=0;


int main()
{
beginning:    
//do whatever you need to do;
int count++;
if (count<=50);
{
goto beginning;
}
return 0;
}

Upvotes: -3

Nick
Nick

Reputation: 199

The way you were asking the question indicated that you had a finished binary. You want to run it as if it was from the command line. The forward slash, to me, is a clue that you are a Unix like operating system user. Well, that, and the fact that this post is tagged "Unix", which I just saw after writing the below. It should all be applicable.

The scheme of using the shell is probably the simplest one.

man bash tells you how to write a shell script. Actually we need to figure out what shell you are using. From the command line, type:

echo $SHELL
The response I get is 
/bin/bash

Meaning that I am running bash. Whatever you get, copy down, you will need it later.

The absolutely lowest knowledge base is to simply create a file with any standard text editor and no suffix. Call it, simply (for example) run50.

The first line is a special line that tells the unix system to use bash to run the command:

#! /bin/bash
(or whatever you got from echo $SHELL).

Now, in the file, on the next line, type the complete path, from root, to the executable. Type the command just as if you were typing it on the command line. You may put any arguments to your program there as well. Save your file.

Do you want to run the program, and wait for it to finish, then start the next copy? Or do you want to start it 50 times as fast as you can without waiting for it to finish? If the former, you are done, if the latter, end the line with &

That tells the shell to start the program and to go on.

Now duplicate that line 50 times. Copy and paste, it is there twice, select all, and then paste at the end, for 4 times, again for 8, again for 16, and again for 32. Now copy 18 more lines and paste those at the end and you are done. If you happen to copy the line that says #! /bin/bash don't worry about it, it is a comment to the shell.

Save the file.

From the command line, enter the following command:

chmod +x ./filenameofmyshellcommand

Where you will replace filenameofmyshellcommand with the name of the file you just created.

Finally run the command:

./filenameofmyshellcommand

And it should run the program 15 times.

If you are using bash, instead of duplicating the line 50 times, you can write a loop:

for ((i=1;i<=50;i++)) do
echo "Invocation $i"
/complete/path/to/your/command
done

I have included a message that tells you which run the command is on. If you are timing the program I would not recommend a "feelgood" message like this. You can end the line with & if you want the command to be started and the script to continue.

The double parenthesis are required for this syntax, and you have to pay your syntax.

for ((i=1;i<=50;i++)) do echo "invocation $i" & done

is an interesting thing to just enter from the command line, for fun. It will start the 50 echos disconnected from the command line, and they often come out in a different order than 1 to 50.

In Unix, there is a system() library call that will invoke a command more or less as if from the terminal. You can use that call from C++ or from perl or about a zillion other programs. But this is the simplest thing you can do, and you can time your program this way. It is the common approach in Unix for running one program or a sequence of programs, or for doing common tasks by running a series of system tools.

If youy are going to use Unix, you should know how to write a simple shell script.

Upvotes: 1

entitledX
entitledX

Reputation: 680

In POSIX shells,

for i in {1..50} ; do ./addTwoNumbers ; done

Upvotes: 10

Brian Roach
Brian Roach

Reputation: 76888

If this is code you are writing, take the number of times you want to "run" as an argument:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char* argv[]) { 

    int numTimes = 1;   

    if (argc > 1)
    {
        numtimes = atoi(argv[1]);
    }

    for (int i = 0; i < numTimes; i++)
    {
        // Your code goes here
    }
}

(Note this doesn't do any sanity checking on the input, but it should point you in the right direction)

Upvotes: 1

Related Questions