I am trying to calculate all the Combinations of a 7 digit number. Each digit can occupy basically two values that's why I am calling it a binary number.
The digit are represented by FNMSDPL. Basically I want all the combinations of values and their sum.
I wrote the below code but its not working.
F[0] = 9.29;
F[1] = -4.47;
N[0] = 9.64;
N[1] = -5.77;
M[0]= -7.48;
M[1] = -2.13;
S[0] = 25.85;
S[1]= -3.55;
D[0]= 12.14;
D[1] = -4.90;
P[0] = 8.65;
P[1]= -0.85;
L[0] = 9.14;
L[1]= -1.73;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
for (int k = 0; k < 2; k++)
{
for (int l = 0; l < 2; k++)
{
for (int m = 0; m < 2; m++)
{
for (int n = 0; n < 2; n++)
{
for (int o = 0; 0 < 2; o++)
{
double count = F[i] + M[j] + L[k] + S[l] + D[m] + P[n] + L[o];
System.Console.WriteLine(count);
}
}
}
}
}
}
}
Error Message:
Index was outside the bounds of the array.
I am getting the above error at "double count =" line.
The font selection on Stack Overflow may have resolved this one
for (int o = 0; 0 < 2; o++)
The condition should be using o instead of 0. (Tip: avoid using o or O as variable names.)
Edit: Another problem here (should be incrementing l, not k):
for (int l = 0; l < 2; k++)
I guess this won't work as expected:
for (int l = 0; l < 2; k++)
You should probably use something more clever than 7 nested for loops (since, as the other answers point out, this is terribly prone to error, especially when you have to make up 14 variable names). A recursive solution would do nicely here.
As for the sum, you don't need to generate all possibilities to know the sum of all 128 possible numbers. Observe that each binary digit (0 or 1) occurs exactly 64 times in each slot, so the sum total is just 64 times the sum of the 14 floats in your arrays.
Related
So I have been searching for an answer for this for about an hour now and I was unable to find one so I'm going to try my luck here.
The problem:
I have a 3 dimensional Array that contains Containers. This array is for an algoritm for placing containers on a cargoship. The array consists of Length, width and height. I am trying to split the array at the middle of the width.
The only solution I have been able to sort of make work is making 2, 3 dimensional arrays and then using 6 for loops to copy the big array:
Container[,,] leftSideOfShip = new Container[ship.length, ((ship.width) / 2), ship.height];
Container[,,] rightSideOfShip = new Container[ship.length, ((ship.width) / 2), ship.height];
for(int a = 0; a < ship.length; a++)
{
for(int b = 0; b < ship.width/2; b++)
{
for(int c = 0; c < ship.height; c++)
{
if(ship.position[a,b,c] != null)
{
leftSideOfShip[a, b, c] = ship.position[a, b, c];
}
}
}
}
for (int d = 0; d < ship.length; d++)
{
for (int e = ship.width/2; e < ship.width; e++)
{
for (int f = 0; f < ship.height; f++)
{
if(ship.position[d,e,f] != null)
{
rightSideOfShip[d, e, f] = ship.position[d, e, f];
}
}
}
}
The 3 for loops for the leftSideOfShip work as expected but the second one gives an indexOutOfRangeException.
Is there a better way to split this and if so how?
First of all, you need to be mindful of how you round side width in case the width of the ship is odd. For example, you can always assume, that the left side is wider in case the width of the ship is odd, something like that:
int leftSideWidth = ship.width % 2 == 0 ? ship.width / 2 : (ship.width / 2) + 1;
int rightSideWidth = ship.width / 2;
Second of all, you have an error in your second loop block:
since you are looping for (int e = ship.width/2; e < ship.width; e++) e is greater than second dimension of rightSideOfShip array.
Full solution will look something like this:
Container[,,] leftSideOfShip = new Container[ship.length, leftSideWidth, ship.height];
Container[,,] rightSideOfShip = new Container[ship.length, rightSideWidth, ship.height];
for (int i = 0; i < ship.length; i++)
{
for (int j = 0; j < ship.width; j++)
{
for (int k = 0; k < ship.height; k++)
{
if (j < leftSideWidth)
{
leftSideOfShip[i, j, k] = ship.position[i, j, k];
}
else
{
rightSideOfShip[i, j - leftSideWidth, k] = ship.position[i, j, k];
}
}
}
}
just started to learn programming and I need 2D array without duplicates. This code (well edited for 1D) worked just fine for 1D but doesn't for 2D and have no clue why.
Would be very happy if someone helped me. Thanks.
Random r = new Random();
int[,] array = new int[10, 8];
for (int i = 0; i < array.GetLength(0); i++)
{
for (int j = 0; j < array.GetLength(1); j++)
{
array[i, j] = r.Next(10, 100);
for (int k = 0; k < i; k++)
{
for (int l = 0; l < j; l++)
{
if (array[i,j] == array[k,l])
{
i--;
j--;
break;
}
}
}
}
}
With the nested j loop you are filling the entire second dimension for each i, but in the k and l loops you are only checking the grid to the upper and left of current cell. You could place a number twice because you are not checking every previously filled location.
If we change the code to this:
for (int k = 0; k < array.GetLength(0); k++)
{
for (int l = 0; l < array.GetLength(1); l++)
{
if (i != k && j != l && array[i, j] == array[k, l])
{
i--;
j--;
break;
}
}
}
Then you eliminate that problem, but you very quickly find that you get a IndexOutOfRangeException because you're decrementing both i & j at the same time. That's not moving you to the previous value - it's jumping back a whole row and left one cell - and that's ultimately sending i or j to -1 and that's not good.
If you want to do it like you're attempting then you need to have a way to simply move back to the previously filled cell, regardless of the row or column you're on.
Try this:
for (int x = 0; x < array.GetLength(0) * array.GetLength(1); x++)
{
array[x % array.GetLength(0), x / array.GetLength(0)] = r.Next(10, 100);
for (int y = 0; y < x; y++)
{
if (array[x % array.GetLength(0), x / array.GetLength(0)] == array[y % array.GetLength(0), y / array.GetLength(0)])
{
x--;
break;
};
}
}
However, that's not very efficient. Try this instead:
var values = Enumerable.Range(10, 90).OrderBy(_ => r.Next()).ToArray();
for (int x = 0; x < array.GetLength(0) * array.GetLength(1); x++)
{
array[x % array.GetLength(0), x / array.GetLength(0)] = values[x];
}
So, first of all, that just seems inefficient. Not sure why you want to do it this way, but then again, don't know the reason. Looks like a programming assignment.
My guess, you need a sort of double break going on. When you break finding a match, you don't break out to the "k" for loop, so you continue looking for a match even though you found one. Try setting a boolean value to say it's found, then use that in the criteria for the for loop for k. That'll break it out and let you start over on the outside loops for i and j.
Even then, it won't work because you indescriminately subtracted i and j. So if you're on position 1,2 you will jump back to 0,1 rather than 1,2. So you need to subtract j, if it drops below 0, then subtract from i and add "array.GetLength(1)" to j.
This solution depends on HashSet's property of containing unique elements. It has an Add method that returns false when we try to add existing elements.
Random r = new Random();
int[,] array = new int[10, 8];
var usedValues = new HashSet<int>();
for (int i = 0; i < array.GetLength(0); i++)
{
for (int j = 0; j < array.GetLength(1); j++)
{
int uniqueValue;
while (true)
{
uniqueValue = r.Next(10, 100);
if (usedValues.Add(uniqueValue)) break; // It is unique indeed
// It is not unique, try again.
}
array[i, j] = uniqueValue;
}
}
The above solution is more suitable when the range of the acceptable unique values is large. In this specific case the range is very small (10-99), and the solution offered by #Enigmativity is preferable.
Basically i'm trying to multiply each element of the first array with each element of the second array and then store it all at the end as a total. I'm pretty new to coding so just trying to learn and this one really has me stuck. This is the example of what it should eventually do.
ExampleArray1 = 5,6,7,8
ExampleArray2 = 2,3,4
(5*2)+(5*3)+(5*4) + (6*2)+(6*3)+(6*4) + (7*2)+(7*3)+(7*4) + (8*2)+(8*3)+(8*4) = 234
My code
int[] firstArray = { 5, 6, 7, 8 };
int[] secondArray = { 2, 3, 4 };
int[] thirdArray = new int[firstArray.Length * secondArray.Length];
for (int i = 0; i < firstArray.Length; i++)
for (int j = 0; j < secondArray.Length; j++)
{
thirdArray[i * firstArray.Length + j] = firstArray[i] * secondArray[j];
Console.WriteLine(thirdArray[i * firstArray.Length + j]);
}
You dont need a third array, you can just sum the results
var total = 0;
for (int i = 0; i < firstArray.Length; i++)
for (int j = 0; j < secondArray.Length; j++)
{
total += (firstArray[i] * secondArray[j]);
}
Console.WriteLine(total);
However you forgot to minus one form the length, in the third array index.
i.e to get the index you need i * (firstArray.Length - 1) + j
int[] thirdArray = new int[firstArray.Length * secondArray.Length];
for (int i = 0; i < firstArray.Length; i++)
for (int j = 0; j < secondArray.Length; j++)
{
thirdArray[i * (firstArray.Length - 1) + j] = firstArray[i] * secondArray[j];
}
Console.WriteLine(thirdArray.Sum());
You can apply some basic algebra to simplify this:
var total = 0;
var array1Total = 0;
var array2Total = 0;
for (int i = 0; i < firstArray.Length; i++)
{
array1Total += firstArray[i];
}
for (int j = 0; j < secondArray.Length; j++)
{
array2Total += secondArray[j];
}
total = array1Total * array2Total;
Console.WriteLine(total);
The reason is that if you expand (x0+x1+x2+x3...)*(y0+y1+y2+...) then you can see that you will multiply x0 by each of the y, then multiply x1 by each of the y, and so on. So you'll get each of the elements in the first brackets multiplied by each of the elements in the second brackets.
While this may not seem much different it will be considerably better for large arrays. if the length of your arrays are m and n then by the nested loops method you'll have m*n loop iterations. With the above technique you will have m+n. For small values this isn't a big deal. If you have arrays of thousands of items then the above will be significantly faster.
Of course, there is an even easier way to do the above:
var total = firstArray.Sum()*secondArray.Sum();
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();
My goal is to make a triple for loop to multiply matrix X matrix, i get in input the matrix and i have to get matrix^2.
I get the error "IndexOutOfRangeException" - index was outside the bounds of the array when i debug the following code:
for (int i = 1; i < nodeList.Count+1; i++)
{
for (int j = 1; j < nodeList.Count+1; j++)
{
result[i, j] = "0";
for (int k = 1; k < nodeList.Count+1; i++)
{
if ((matrix[i, k] != null) && (matrix[k, j] != null))
{
n1 = Convert.ToInt32(matrix[i, k]);
n2 = Convert.ToInt32(matrix[k, j]);
n3 = Convert.ToInt32(result[i, j]);
total = n3 + n1 * n2;
_total = total.ToString();
result[i, j] = _total;
}
}
}
}
where the variables are:
1. matrix that is type String[,] and the dimensions are (nodelist+1,nodelist+1)
2.result that is is the same type and dimension of the matrix, where i want to put the resultant matrix
3.nodelist is the array of the names of the nodes that i have in the diagram
4. n1,n2,n3 are int, I put in them the convert int from the matrix
5.total is the result of the operation for the multiplication
6._total convert total int in total string for the result matrix
So i set the right dimensions for every array and matrix but i get constantly the same error. I don't get it why. Can please someone help to notice the error, because i don't see it.
In the k loop, you are incrementing i.
for (int k = 1; k < nodeList.Count+1; i++) <-- you are incrementing i, it should be incrementing k.
like this:
for (int k = 1; k < nodeList.Count+1; k++)
Arrays are 0-based in C# -- the first element is at position 0 instead of position 1.
for (int i = 1; i < nodeList.Count+1; i++)
... should be ...
for (int i = 0; i < nodeList.Count; i++)
You also have what appears to be a copy-paste error for the k-loop.
for (int k = 1; k < nodeList.Count+1; i++) // should be k++?
The standard way to use a for loop with an array is to use
for(int x= 0; x < arry.count ;x++)
using 1 and +1 as the conditional will assure that you get an index out of rage as c# arrays are indexed by 0
As mentioned you are incrementing by i in the K loop.
Also you will be getting the out of bounds error every time you try to access an matrix on the last iteration of the loops. Either you need to go from 0 to Count in your loops or you need to put a -1 in all of your matrix operations. ex:
results[i-1, j-1] = _total;
The matrix indexes start at 0.