My function return the same number sequence.
When i give on input list with first list[0] all 0 and second list[1] all 1, i get on the output list e.g:
1 1 1 1 0 1 1
1 1 1 1 0 1 1
And all the time generate sequence on the output is the same.
I mean temp[0] and temp[1] have the same sequence
public List<float[][][]> rozmnarzanie(List<nur> list,Random dnd)
{
float[][][] ftemp=net.wagi;
List<float[][][]> temp = new List<float[][][]>();
for (int i = 0; i < 2; i++)
{
for (int a = 0; a < net.wagi.Length; a++)
{
for (int j = 0; j < net.wagi[a].Length; j++)
{
for (int k = 0; k < net.wagi[a][j].Length; k++)
{
if(dnd.Next(0,100)<=50)
{
ftemp[a][j][k] = list[0].listawagi[a][j][k];
}
else
{
ftemp[a][j][k] = list[1].listawagi[a][j][k];
}
}
}
}
temp.Add(ftemp);
}
return temp;
}
I mean temp[0] and temp[1] have the same sequence
Correct. You add the same array twice, temp[0] and temp[1] are both references to the same location in memory. What you see are the results of the last run. The same result will show up in net.wagi .
Your question isn't about random numbers but about arrays being reference types.
I don't know what net.wagi is but the solution could look something like:
//float[][][] ftemp=net.wagi;
List<float[][][]> temp = new List<float[][][]>();
for (int i = 0; i < 2; i++)
{
float[][][] ftemp = new float[a][b][c]; // pseudo code
...
temp.Add(ftemp);
}
You figure out the a, b and c.
Related
So, I am trying to wrap my head around understanding how you can use a variable to denote how many times a loop is nested.
Here is an example I write up to simulate the output of dimensions = 4:
static void Main(string[] args)
{
int dimensions = 4; // e.g. for (1, 2, 3, 4), dimensions = 4
Console.WriteLine($"{addNumbers(dimensions)}");
Console.ReadKey();
}
static long addNumbers(int dimensions)
{
long number = 0;
// hard coded to be dimensions = 4
for (int h = 0; h <= dimensions; h++)
for (int i = 0; i <= dimensions; i++)
for (int j = 0; j <= dimensions; j++)
for (int k = 0; k <= dimensions; k++)
number += h + i + j + k; // just some random math
return number;
}
This will present the expected output of:
5000
So to readdress the problem, how can I code to allow this for n dimensions? Thanks for your help!
For arbitrary n dimensions you can loop with a help of array int[] address which represents n dimensions:
static long addNumbers(int dimensions) {
int[] address = new int[dimensions];
// size of each dimension; not necessary equals to dimensions
// + 1 : in your code, int the loops you have i <= dimensions, j <= dimensions etc.
int size = dimensions + 1;
long number = 0;
do {
//TODO: some math here
// i == address[0]; j = address[1]; ... etc.
number += address.Sum();
// next address: adding 1 to array
for (int i = 0; i < address.Length; ++i) {
if (address[i] >= size - 1)
address[i] = 0;
else {
address[i] += 1;
break;
}
}
}
while (!address.All(index => index == 0)); // all 0 address - items're exhausted
return number;
}
Finally, let's add some Linq to look at the results:
int upTo = 5;
string report = string.Join(Environment.NewLine, Enumerable
.Range(1, upTo)
.Select(i => $"{i} -> {addNumbers(i),6}"));
Console.Write(report);
Outcome:
1 -> 1
2 -> 18
3 -> 288
4 -> 5000 // <- We've got it: 5000 for 4 dimensions
5 -> 97200
i have a homework using strand sort and i must take the growing sequence of numbers from the initial array and merge them together in the array that represents our result (C#)
Like this one http://imgur.com/nQFzJw7
So far i did something like that
public static int[] Str(int[] a)
{
int i, j, x ,temp,k=0,count=1;
int size = a.Length;
int len = a.Length;
Strand = new int[size];
Merged = new int[size];
for (i = k; i < size; i++)
{
x = a[i];
Strand[0] = x;
for (i = k; i < size; i++) //checking if there's a bigger int than the first one
{
if (a[i] > x)
{
x = a[i];
}
}
for (i = k; i < len; i++)
{
if (a[i] == x) // checking if the max appears more than 1 time
{
temp = a[i];
a[i] = a[len];
a[len] = temp;
len--; //swaps the max numbers to the last position
Strand[count] = x;
count++;
}
}
for (i = 0; i < count; i++) // cant find a way to put in the final merged and sorted array
{
}
count = 1;
k++;
}
Any suggestions?
You always need to extract the first element into strand
strand[0] = a[0]
count = 1
Then you need to extract suitable elements into strand, shifting the rest of array
for i = 1 to size - 1
if a[i] >= strand[count - 1]
strand[count++] = a[i]
else
a[i - count] = a[i]
size = size - count
Then you need merge current strand and merged - look for Merge procedure from MergeSort
Repeat these steps until size becomes 0
I've encountered the problem while working on my multiknapsack solver.
The program is working well on one knapsack but when it comes to multiple knaps there are some problems.
Problem: The items aren't removed from collection. I know that I need to do this, because for the second knapsack its iterating again through the same objects - so the maximized val is the same...
private void Knapsack()
{
List<Plecak> descendingKanps = _plecakList.OrderByDescending(o => o.W).ToList(); // List of configured Kanpsacks in descending order
List<Produkt> descendingProducts = _produktList.OrderByDescending(o => o.cena).ToList(); // List of products to pack in descending order
int N = descendingProducts.Count; //number of items in product list
double maxVal = 0; // accumulated value of one knapsack
foreach (Plecak p in descendingKanps) // for every knapsack...
{
double[,] V = new double[N + 1, (int)p.W + 1]; //array that stores best option (the value of item)
for (int c = 0; c <= p.W; c++) //since its a 0-1 MKP problem so initialize whole array with zeroes
{
V[0, c] = 0;
}
for (int r = 0; r <= N; r++)
{
V[r, 0] = 0;
}
for (int i = 1; i <= N; i++) // constraint of items count
{
for (int wt = 1; wt <= p.W; wt++) //the constraint of weight
{
if (descendingProducts[i - 1].waga < wt + 1) // if weight of the product is less than constraint, so it can be added...
V[i, wt] = Math.Max(descendingProducts[i - 1].cena + V[i - 1, wt - descendingProducts[i - 1].waga], V[i - 1, wt]); // find best solution and add the value to the Value array, comapre the value with the value form previous row
else
V[i, wt] = V[i - 1, wt]; // keep 0, or last best solution
maxVal = V[i, wt]; // assign to variable
}
}
summary.Items.Add("maximum val for knapsack: " + p.nazwa + " is: " + maxVal); // print out the value of knapsack
}
}
This will hopefully turn into an answer, but for now it is a question.
In your code you have this:
for (int c = 0; c <= p.W; c++) //since its a 0-1 MKP problem so initialize whole array with zeroes
{
V[0, c] = 0;
}
for (int r = 0; r <= N; r++)
{
V[r, 0] = 0;
}
You say that you "initialize whole array with zeroes". The above code will do this to an array:
0 0 0 0 0
0 1 1 1 1
0 1 1 1 1
0 1 1 1 1
You'll only ever reach the first column and the top row. If all places in the array should be changed to 0, then this is the correct approach:
for (int r = 0; r <= N; r++)
{
for (int c = 0; c <= p.W; c++)
{
V[r, c] = 0;
}
}
I hope this input can be of help.
I have this function to initiate a two dimensional array:
static Array Matrix(int Rows, int Columns)
{
int[,] LotteryArray = new int[Rows,Columns];
for (int i = 0; i < LotteryArray.GetLength(0); i++)
{
for (int j = 0; j < LotteryArray.GetLength(1); j++)
{
LotteryArray[i, j] = RandomNum(1, 46);
Console.Write("{0,3},", LotteryArray[i, j]);
}
Console.WriteLine();
}
return LotteryArray;
}
Then I have this which is supposed to give me a one dimensional array and see how many numbers in the winning array are in the matrix:
int RowNum = 1;
int Prediction = 0;
Console.WriteLine("Your winning numbers are!");
Console.WriteLine("------------------------");
int[] Winner = new int[6];
for (int i = 0; i < Winner.Length; i++) //this loop is to initiate and print the winning numbers
{
Winner[i] = RandomNum(1, 46); //the numbers are supposed to be between 1 and 45, so i tell it to do it until 46 because the upper limit is exclusive
Console.Write("{0,3},", Winner[i]);
}
Console.WriteLine();
Console.WriteLine("------------------------"); //these two lines are for aesthetics
Matrix(Rows, Columns);
foreach (int i in Winner)
{
for (int j = 0; j<LotteryArray; j++)
{
if (Winner[i] == j)
{
Prediction++;
if (j % 6 == 0) { RowNum++; }
}
Console.WriteLine("you got {0} correct prediction in row number {1}",Prediction,RowNum);
RowNum = 1;
}
}
It's telling me LotteryArray doesn't exist in the current context.
LotteryArray is a variable within another method. You cannot access it in the scope you are showing.
You can do get the return from your method into a variable and then use it.
var LotteryArray = Matrix(Rows, Columns);
foreach (int i in Winner)
{
for (int j = 0; j<LotteryArray; j++)
{
if (Winner[i] == j)
{
Prediction++;
if (j % 6 == 0) { RowNum++; }
}
Console.WriteLine("you got {0} correct prediction in row number {1}",Prediction,RowNum);
RowNum = 1;
}
}
LotteryArray is a variable declared in Matrix method, and is not visible outside.
I'm trying to make a program that: When given a 4 digit number (for example, 1001) it sums the digits of that number, for 1001 this is 1 + 0 + 0 + 1 = 2, than it finds all sequences of 6 numbers from 1 to 6 (including permutations, i.e. 1*1*1*1*1*2 is different than 2*1*1*1*1*1) whose product is that number.
The result should be printed on the console in the following format: each sequence of 6 numbers, with their Morse representation, separated with a single pipe: 1 is .---- , 2 is ..---: .----|.----|.----|.----|..---|, on a new row the next permutation: .----|.----|.----|..---|.----| and so on.
The problem is, my solution doesn't show the correct answers, not even the correct number of them.
Here's my code (and please, if possible, tell me where my mistake is, and not some one line hack solutions to the problem with LINQ and regex and God knows what):
string n = Console.ReadLine();
string[] digitsChar = new string[n.Length];
for (int i = 0; i < 4; i++)
{
digitsChar[i] = n[i].ToString();
}
int[] digits = new int[digitsChar.Length];
for (int i = 0; i < 4; i++)
{
digits[i] = Convert.ToInt32(digitsChar[i]);
}
int morseProduct = digits.Sum();
Console.WriteLine(morseProduct);
List<int> morseCodeNumbers = new List<int>();
for (int i = 1; i < 6; i++)
{
for (int j = 1; i < 6; i++)
{
for (int k = 1; i < 6; i++)
{
for (int l = 1; i < 6; i++)
{
for (int m = 1; i < 6; i++)
{
for (int o = 1; o < 6; o++)
{
int product = i * j * k * l * m * o;
if (product == morseProduct)
{
morseCodeNumbers.Add(i);
morseCodeNumbers.Add(j);
morseCodeNumbers.Add(k);
morseCodeNumbers.Add(l);
morseCodeNumbers.Add(m);
morseCodeNumbers.Add(o);
}
}
}
}
}
}
}
int numberOfNumbers = morseCodeNumbers.Count;
string[] morseCodes = new string[] { "-----", ".----", "..---", "...--", "....-", "....." };
for (int i = 0; i < numberOfNumbers; i++)
{
int counter = 0;
if (i % 5 == 0)
{
Console.WriteLine();
counter = 0;
}
if (counter < 5)
{
int index = morseCodeNumbers[i];
Console.Write(morseCodes[index] + "|");
counter++;
}
A lot of your for-loop conditions refer to i instead of j,k,l and m. The same for the increment part. For example:
for (int j = 1; i < 6; i++)
should be
for (int j = 1; j < 6; j++)
Furthermore if the range is from 1 to 6 you need to change < to <=, see:
for (int i = 1; i <= 6; i++)
You don't need to convert the string to a string array to get the int array of digits btw, so while this is correct:
for (int i = 0; i < 4; i++)
{
digitsChar[i] = n[i].ToString();
}
int[] digits = new int[digitsChar.Length];
for (int i = 0; i < 4; i++)
{
digits[i] = Convert.ToInt32(digitsChar[i]);
}
you could it do like that (sry for LINQ):
var digits = n.Select(c=>(int)char.GetNumericValue(c) ).ToArray();