Visual C++ Statement + Vars - c++-cli

How Can I bind for example int value to the statement below?
System::String^ Content = "just example";
int iAValue = 5;
lblOutput_{iValue}->Text = Content;

You don't; you use an array or collection of some sort. This sort of thing is often attempted by beginners. It is not possible, nor is it a good idea to tie your program logic to the names of your variables.
auto labels = gcnew List<Label>();
labels->Add(lblOutput1);
labels->Add(lblOutput2);
labels->Add(lblOutput3);
labels->Add(lblOutput4);
labels->Add(lblOutput5);
// ...
String^ Content = "just example";
int iAValue = 4;
labels[iAValue].Text = Content;
And then later you can iterate over all of them easily:
for(int i = 0; i < labels->Count; ++i) {
// i is the label "number"
// labels[i] is the label
}

Related

How can I customize values for genes of chromosome?

I am trying to solve one job assignment problem using GeneticSharp. It is assigning gates to the trucks, and not all gates are suitable for the trucks.
Each chromosome is required to have gene values from a certain array of double values, corresponding to gene index (each gene index is equal to truck number). So, I'm trying to get a value randomly from that array and assign to gene in FloatingPointChromosome class, but this gives me an error of 'Object reference not set to an instance of an object. allowedStands was null'.
Could you, please, advise me how to solve it?
public FloatingPointChromosome(double[] minValue, double[] maxValue, int[] totalBits, int[] fractionDigits, double[] geneValues, double[][] allowedStands)
: base(totalBits.Sum())
{
m_minValue = minValue;
m_maxValue = maxValue;
m_totalBits = totalBits;
m_fractionDigits = fractionDigits;
// If values are not supplied, create random values
if (geneValues == null)
{
geneValues = new double[minValue.Length];
//var rnd = RandomizationProvider.Current;
var rnd = new Random();
for (int i = 0; i < geneValues.Length; i++)
{
int a = rnd.Next(allowedStands[i].Length);
geneValues[i] = allowedStands[i][a];
//I make here that it randomly selects from allowed gates array
}
}
m_originalValueStringRepresentation = String.Join(
"",
BinaryStringRepresentation.ToRepresentation(
geneValues,
totalBits,
fractionDigits));
CreateGenes();
}
I guess in the case of truck and gate assignment is better you create your own chromosome, take a look on TspChromosome to get an idea.
public TspChromosome(int numberOfCities) : base(numberOfCities)
{
m_numberOfCities = numberOfCities;
var citiesIndexes = RandomizationProvider.Current.GetUniqueInts(numberOfCities, 0, numberOfCities);
for (int i = 0; i < numberOfCities; i++)
{
ReplaceGene(i, new Gene(citiesIndexes[i]));
}
}
Using the same approach, you cities indexes are your gates indexes.

How to covert Textbox to integer array in c++/cli

I'm trying to convert the textbox to an integer array assuming that every character of the textbox is a digit.
//textbox is named input
int size = this->input->Text->Length;
int * num = new int[size];
int Counter = 0;
//for loop used since textbox inputs like a calculator
//Ex: the number: 234 is inputed: 2, then 23, then 234
for (int i = size; i > 0; i--)
{
num2[Counter] = System::Convert::ToInt32(input->Text[i-1]);
Counter += 1;
}
Array of numbers should be:
num[0] = 4, num[1] = 3, num[2] = 2
Upon research though it seems that it's finding the integer unicode value instead.
Code input->Text[i-1] returns a single Unicode character value of the wchar_t type. That is implicitly cast to Int32, i.e. the symbol code.
You have to convert the char to a string, before converting to the number. You can use the Substring method or the ToString method for this purpose.
You can do it as follows:
String^ text = this->input->Text;
int size = text->Length;
int * num = new int[size];
for (int i = size - 1; i >= 0; i--) {
num[i] = Convert::ToInt32(text->Substring(size - i - 1, 1));
}
However, you should not mix managed and unmanaged code.
There is a better way. Use a generic collection instead of an array.
String^ text = this->input->Text;
int size = text->Length;
List<int>^ nums = gcnew List<int>();
for (int i = size - 1; i >= 0; i--) {
nums->Add(Convert::ToInt32(text[i].ToString()));
}
Don't forget
using namespace System::Collections::Generic;
The list can be accessed by index like an array:
nums[i]
So it is convenient to work with. And most importantly, do not need to worry about freeing memory.

Making an incremental for loop end by a certain number in objective-c

I'm trying to find a solution to this coding problem:
Create a for loop that will begin with a value of 5 and end with a value of 25. In each iteration, add the incrementing value to mathTotal. (HINT: the last value used INSIDE the loop should be 25)
But the way I can think of doing it returns with a final number for mathTotal of 26. I'm not sure how to manipulate the code to stop at 25 without actually doing the math to figure out what number to make the condition for the program to stop running.
This is what I have:
int mathTotal;
for(int i = 5; mathTotal <=25; i++) {
mathTotal = mathTotal + i;
}
I know this is a simple problem, but I'm learning how to code and don't want to move on without fully understanding something.
Thank you!
There are two major issues:
mathTotal is not initialized. You have to set an initial value.
int mathTotal = 0;
The upper border (the second parameter of the for loop) is defined as mathTotal <= 25 – rather than i <= 25 – which will be reached when i is 8.
for (int i = 5; i <=25; i++) {
mathTotal = mathTotal + i;
}
The traditional for loop in Objective-C is inherited from standard C and takes the following form:
for (/* Instantiate local variables*/ ; /* Condition to keep looping. */ ; /* End of loop expressions */)
{
// Do something.
}
For example, to print the numbers from 1 to 10, you could use the for loop:
for (int i = 1; i <= 10; i++)
{
NSLog(#"%d", i); //do something
}
This is logically equivilant to the following traditional for loop:
for (int i = 0; i < [yourArray count]; i++)
{
NSLog([myArrayOfStrings objectAtIndex:i]);
}
Your Doubt
int mathTotal = 0;
for (i = 5 = 0; i <=25 ; i++)
{
mathTotal = mathTotal + i;
}

Does creating a new variable use up more memory than instantiating the variable?

sorry if the title is a little 'off', couldn't think of a better title for it.
Anyway, the question is I have some code:
Random rnd = new Random();
for (int i = 1; i <= 50; i++)
{
int dice = rnd.Next(1, 7);
}
Basically this will generate a random number 50 times, the question I have is does instantiating the variable use up more memory than just changing the variable 'dice'
So the code would be like this:
int dice;
Random rnd = new Random();
for (int i = 1; i <= 50; i++)
{
dice = rnd.Next(1, 7);
}
Just curios to if it does use up more memory or not to re-assign the variable
No, it is the same, compiler will just reserve enough space to store the value of "dice", regardless of where it's declared.
Just keep your variable in inner-most scope you can and that's it.

Accessing my malloc'd 2d array with [x][y]

I'm updating a class to use member variable instead of #defines to define the bounds of a 2d array. It used to look like:
#define kWidth 3
#define kHeight 100
NSUInteger fields[kWidth][kHeight];
Now kWidth and kHeight are iVars. I switched to malloc convention, because I see no other choice as the bounds can now change. The problem is I cannot access the array using two [] ([][]). See my inline comments. I am sure I've malloc'd correctly. I've done this many times before, and under iOS. Why can't I access this way?
self.kFieldsHeight = 100;
self.kFieldsWidth = 3;
NSUInteger** fields = (NSUInteger**)malloc(sizeof(NSUInteger) * self.kFieldsHeight * self.kFieldsWidth);
memset(fields, 0xFF, self.kFieldsWidth * self.kFieldsHeight * sizeof(NSUInteger));
//// Now with LLDB I can examine the array in one dimension
// p fields[0] // 0xFFFFFFFF
// p fields[299] // 0xFFFFFFFF
// p fields[300] // 0xGARBAGE
//// THIS fails with "error: Couldn't dematerialize struct : Couldn't read a composite type from the target: gdb remote returned an
error: E08"
// p fields[0][0]
// Thus this fails in my code
NSUInteger i = fields[0][0];
What's the deal?
Edit: (more detail)
I've also tried mallocing like this:
fields = (NSUInteger**)malloc(sizeof(NSUInteger*) * self.kFieldsHeight);
if(fields){
for(int i = 0; i < self.kFieldsHeight; i++){
fields[i] = (NSUInteger*)malloc(sizeof(NSUInteger) * self.kFieldsWidth);
}
}
Edit: (even more detail). I swapped the width and height with the same results:
fields = (NSUInteger**)malloc(sizeof(NSUInteger*) * self.kFieldsWidth);
if(fields){
for(int i = 0; i < self.kFieldsWidth; i++){
fields[i] = (NSUInteger*)malloc(sizeof(NSUInteger) * self.kFieldsHeight);
}
}
You need to do this:
NSUInteger **fields = malloc(self.kFieldsHeight * sizeof(NSUInteger*));
for (int i = 0; i < self.kFieldsHeight; i++) {
fields[i] = malloc(sizeof(NSUInteger) * self.kFieldsWidth);
memset(fields, 0xFF, self.kFieldsWidth * sizeof(NSUInteger));
}
But this is not the way most people do the job. Most developers use a big array instead of a 2d array and try to index it themselves. Like this:
NSUInteger *fields = malloc(self.kFieldsHeight * self.kFieldsHeight * sizeof(NSUInteger));
fields[h * width + w] = pixel_value;
try this
fields = (NSUInteger**)malloc(sizeof(NSUInteger*) * self.kFieldsWidth);
if(fields){
for(int i = 0; i < self.kFieldsWidth; i++){
fields+i = (NSUInteger*)malloc(sizeof(NSUInteger) * self.kFieldsHeight);
}
}
no real reason for doing it this way, just another way of saying a[i] is a+i
I am using my C knowledge here and not objective C hope I can help