Execute Microsoft Visual Studio project multiple times with different parameters - c++

I would ask for your help and apologise if the question doesn't make sense.
I have a Microsoft Visual Studio project which I want to execute it multiple times in one go, and every time I will change one parameter.
Please see below the concept:
#include <iostream>
using namespace std;
int size_list = 2;
int my_list[2] = { 5, 6 };
int main()
{
for (int i = 0; i < size_list; i++)
{
cout<<"the number is "<<my_list[i]<<endl;
}
return 0;
}
So, I would like to replace the loop and instead I will have each element of my_list as parameters.
Is there any way to do so?
Thanks

You can pass the parameters in main().
int main(int argc, char* argv[]){std::cout<< argv[1];}
And use script like
for ($i=0;$i -lt 5; $i++)
{
#your CPP project and parameters array
Start-Process test.exe $arr[0]
Wait-Process test
}

Related

Qt Creator debugger just show local variables

I am using QtCreator5.14.0 MinGW-64bit and I wrote this code :
(do not check the code, it is correct , my problem is another thing)
#include <iostream>
using namespace std;
#define MAX 100
int Solutions[MAX]={0,1};
int CountOfCalls = 0;
long int f(int a){
if(a==0)
return 0;
if(Solutions[a])
return Solutions[a];
CountOfCalls++;
return (Solutions[a] = f(a-1) + f(a-2));
}
int main(){
cout<<f(40)<<endl;
cout<<"Count of calls : "<<CountOfCalls<<endl;
return 0;
}
when I begin debugging at the first code in the main , right pane doesn't show global variables(here is Solutions array and CountOfCalls variable) just shows local variables.
debugging : https://i.imgur.com/YvVvbyk.png
my debugger settings :
General : https://i.imgur.com/Ziz8vHJ.png
GDB : https://i.imgur.com/FdjsGmf.png
GDB Extended : https://i.imgur.com/eJ8Bq4Y.png
Locals & Expressions : https://i.imgur.com/ef4KxSj.png
I can right click and choose add new expression evaluator to have the Solutions array and CountOfCalls variable in the right pane , but it is boring for big source codes to add everything you want .
how to tell debugger to evaluate all global and local variables automatically?

From Lower Cases to Upper Cases Quote Console App

I have a console app which takes an input. The user inputs a text such as "It's better to be a '<'good man'>' rather than a '<succesful man'>" , and transforms it like so : "It's better to be a '<'GOOD MAN'>' rather than a '<SUCCESFUL MAN'>".
I did the following script but beeing C++ newbie it doesn t quite work and I get the following output:"It's better to be a '<good man'>' rather than a '<SUCCESFUL MAN'>"
Note:the program can have multiple "quotes".
This is my attept :
#include <iostream>
#include <cstring>
using namespace std;
char a[]="It's better to be a <good man> rather than a <succesful man>",*p;
int x,y,bec1,bec2;
int main()
{
for(int i=0;i<strlen(a);i++){
if(a[i]=='<'){
x=i;
bec1=1;
}
if(a[i]=='>'){
y=i;
bec2=1;
}
if(bec1==1 && bec2==1)
for(int j=x+1;j<y;j++){
if(a[j]!=' ')
a[j]-=32;
if(j==y){
bec1=0;
bec2=0;
}
}
}
cout<<a;
return 0;
}

Understanding the dp on tree

I was recently solving a problem from Codeforces. After giving it a lot of tries I was not able to get how in tree dp the matrix calculation works in the editorial solution. The following is the code where I have added comments to the parts I don't understand in it.
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int f[2][10010][110];//0 max 1 min
char s[10010];
int tr[10010][2],size,n,fa[10010],p,m,minn,pre;
void dfs(int x)
{
//cout<<x<<" "<<f[0][x][0]<<endl;
if (!tr[x][0]) return;
int l=tr[x][0],r=tr[x][1];
dfs(l),dfs(r);
/*The part which gets complicated need help why and how this calculation works*/
for (int i=0;i<=minn;i++)
for (int j=0;i+j<=minn;j++)
{
f[0][x][i+j+(p<m)]=max(f[0][x][i+j+(p<m)],f[0][l][i]+f[0][r][j]);
f[0][x][i+j+(p>=m)]=max(f[0][x][i+j+(p>=m)],f[0][l][i]-f[1][r][j]);
f[1][x][i+j+(p<m)]=min(f[1][x][i+j+(p<m)],f[1][l][i]+f[1][r][j]);
f[1][x][i+j+(p>=m)]=min(f[1][x][i+j+(p>=m)],f[1][l][i]-f[0][r][j]);
}
}
int main()
{
scanf("%s",s+1);
scanf("%d%d",&p,&m);
memset(f[0],-63,sizeof(f[0]));
memset(f[1],63,sizeof(f[1]));
/* Why we have used min of the two and how does it handle both condition */
minn=min(p,m);
n=strlen(s+1);
size=1;pre=size;
for (int i=1;i<=n;i++)
{
if (s[i]=='('||s[i]=='?')
{
tr[pre][tr[pre][0]?1:0]=++size;
fa[size]=pre;
pre=size;
}
else if (s[i]==')') pre=fa[pre];
else f[0][size][0]=f[1][size][0]=s[i]-'0',pre=fa[pre];
}
dfs(1);
printf("%d",f[0][1][minn]);
}
The part where I get lost is this
f[0][x][i+j+(p<m)]=max(f[0][x][i+j+(p<m)],f[0][l][i]+f[0][r][j]);
f[0][x][i+j+(p>=m)]=max(f[0][x][i+j+(p>=m)],f[0][l][i]-f[1][r][j]);
f[1][x][i+j+(p<m)]=min(f[1][x][i+j+(p<m)],f[1][l][i]+f[1][r][j]);
f[1][x][i+j+(p>=m)]=min(f[1][x][i+j+(p>=m)],f[1][l][i]-f[0][r][j]);
I always struggle with such types of problems. Can someone give the link to approach such problems.
Which part of the lines don't you understood? I take one line
f[0][x][i+j+(p<m)]=max(f[0][x][i+j+(p<m)],f[0][l][i]+f[0][r][j]);
and rewrite it
const int index_max = 0;
int y = i+j + (p<m? 1: 0); // in your code p<m is cast to int, true=1, false=0
int old_max = f[index_max][x][y];
int next_value = f[index_max][l][i] + f[index_max][r][j]:
f[index_max][x][y] = max(old_max, next_value);
You are looking for the maximum of the next_values of your double-loop. As l, r are fixed the next_values are sums of values in two rows.
Similar for the other 3 lines.

How to pass integer vector from shell?

I want to pass an integer array/vector as shell argument? Below is my last attempt:
#include <iostream>
#include <vector>
#include <string>
//using namespace std;
int main(int argc, const char *argv[])
{
char dummy;
std::vector<int> argument(argc-1);
std::cout<<argc<<std::endl;
for (int i = 1; i <= argc; ++i)
{
dummy=*(*(argv+1)+i);
argument[i-1]=std::stoi(dummy);
}
std::cout<<argument.size()<<std::endl;
return 0;
}
I got error:
error: no matching function for call to ‘stoi(char&)’
Is there any simpler way to do it?
basically run program as:
./executable {1, 2, 3, 4, 5}
and intialize an array/vector in programme with those values?
Resolving the compiler error is a waste of time. OP had the right idea initially using std::string dummy;, so let's stick with that and figure out what went wrong.
First off lets assume 4 arguments: 1 12 123 1234 and examine the range of the 4 loop.
for (int i = 1; i <= argc; ++i)
Because OP starts the loop at 1, I assume they know that the first argument is the command executed. Great start.
But i <= argc will allow the i to range from 1 to 5. argv is valid from 0 to 4. That means undefined behaviour was invoked accessing argv[5] and Crom only knows what happens after that. Very likely dummy got loaded with garbage from argv[5] and std::stoi could not parse this garbage into an int. This is only speculation. Determining the behaviour of undefined behaviour is a waste of time. It might be different next time. It might even look like it worked next time.
So the first thing to do is get rid of the out of bounds read.
for (int i = 1; i < argc; ++i)
Now we have an i with a range of 1 to 4 and everything stays in bounds.
Next, we can clean up
dummy=*(*(argv+1)+i);
since we aren't tying to fit it into a char anymore. The simplest is
std::string dummy(argv[i]);
which declares string dummy and initializes it to contain one command line argument. Can this be done faster? Yeah, but this is stupidly easy to read. Start with easy and only go into the difficult if easy doesn't meet the program requirements. The only change I would make here until forced is change dummy to a more descriptive name.
Even simpler you could
argument[i-1]=std::stoi(argv[i]);
But I prefer the dummy approach at least for now. It has a major debugging advantage: You can print or use the debugger to inspect the value of dummy before std::stoi throws an exception because dummy can't be converted.
The resulting code:
int main(int argc, const char *argv[])
{
std::vector<int> argument(argc-1);
std::cout<<argc<<std::endl;
for (int i = 1; i < argc; ++i)
{
std::string dummy(argv[i]);
argument[i-1]=std::stoi(dummy);
}
std::cout<<argument.size()<<std::endl;
return 0;
}
called with ./executable 1 12 123 1234 the output will be
5
4
5 input arguments and 4 items in the vector because the command is discarded.
Figuring out how to handle the curly braces in
./executable {1, 2, 3, 4, 5}
I'm going to leave to OP to figure out.
No need to make complex simply use
argument[i-1]=std::stoi(argv[i]); in place of
dummy=*(*(argv+1)+i);
argument[i-1]=std::stoi(dummy);
we know that array of size n stores element from location 0 to location n-1.
argc is the number of strings when you run the program including your executable file name we know that argv is array of strings(character sequence) so at location zero in argv is a executable file name. that is why start for loop from location i=1, and we need to run for loop i=1 to i < argc( means till i<=argc-1)
try following program
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main(int argc, const char *argv[])
{
char dummy;
std::vector<int> argument(argc-1);
std::cout<<argc<<std::endl;
for (int i = 1; i < argc; ++i)
{
// dummy=*(*(argv+1)+i);
// argument[i-1]=std::stoi(dummy);
argument[i-1]=std::stoi(argv[i]);
}
std::cout<<argument.size()<<std::endl;
return 0;
}

Some questions regarding 2d Vectors, C++

This 2d vector is being used to hold a game-board for minesweeper. I want to create a 2d vector of struct cell, which has several "state" variables all holding information needed to construct the game board (I am creating a basic minesweeper game to run on the command line, very rudimentary, just want to get a better grasp of classes). First of all, what am I doing wrong when trying to pass the vector to the void function? And then how would I be able to access the separate variables to read and write to them? I know this may be unusual (could solve using arrays) but I'd like to do it a little differently. I have looked through various forums but people don't seem to use this approach. Thanks guys.
EDIT:
What I'm trying to accomplish with the vector of cell's is basically 3 vectors in 1 so that I can simultaneously use the information in the different states to check whether various conditions have been met when a player makes a move (i.e. check whether there is a mine there, or whether that spot has already been opened/marked/unmarked etc.) Please let me know if the code below doesn't allow for what I want to accomplish.
code:
#include <iostream>
#include <vector>
using namespace std;
void gameboard(vector<vector<int>> &stateboard)
struct cell
{
int state; //( 0 hidden, 1 revealed, 2 marked)
int value; //(-1 mine, 0 no surrounding, # > 0
bool isMine;
};
void gameboard(vector<vector<int>> &stateboard)
{
}
int main()
{
int columns = 10;
int rows = 10;
vector <vector<cell> > gameboard(rows, vector<cell>(columns));
gameboard(&gameboard);
return 0;
}
sorry guys, this piece of code doesn't even begin to resemble the outline I have in Xcode, I was just trying to present the question in an easier to follow manner and threw this together.
new code:
#include <iostream>
#include <vector>
using namespace std;
struct cell
{
int state; //( 0 hidden, 1 revealed, 2 marked)
int value; //(-1 mine, 0 no surrounding, # > 0
bool isMine;
};
void game_state(vector<vector<cell>> &stateboard)
{
}
int main()
{
int columns = 10;
int rows = 10;
vector <vector<cell> > gameboard(rows, vector<cell>(columns));
game_state(gameboard);
return 0;
}
I guess having the same name for a function and vector was throwing Xcode off, which is why I made game board a reference originally but now I see why that was stupid. Now that this works, how can i specifically read and write to just the bool isMine variable? I'm not asking for you to do it completely but a basic line of code showing me how to access that specific part would be a greatly help me. Am I conceptualizing this incorrectly?
hope it helps you:
#include <iostream>
#include <vector>
// your columns and rows are equal,
//and they should no change, so i think better to do them const
const int BOARD_SIZE = 10;
struct cell {
int state;
int value;
bool isMine;
};
void game_state(std::vector < std::vector <cell > > &stateboard) {
}
int main (){
std::vector < std::vector <cell > > gameboard;
//I give more preference to initialize matrix like this
gameboard.resize(BOARD_SIZE);
for (int x = 0; x < BOARD_SIZE; x++) {
gameboard[x].resize(BOARD_SIZE);
for (int y = 0; y < BOARD_SIZE; y++) {
// and this is an example how to use bool is mine
// here all cells of 10x10 matrix is false
// if you want place mine in a first cell just change it
// to gameboard[0][0].isMine = true;
gameboard[x][y].isMine = false;
}
}
game_state(gameboard);
return 0;
}