This question already has answers here:
C# Creating an array of arrays
(5 answers)
Closed 7 years ago.
I came across this strange IndexOutOfRangeException exception. The code I am working with was originally a C code, I have managed to translate it to C#
So I the followin loop gives me the error:
for (int i = 0; i < 6; i++)
{
L[0] = new double[]{ T[0] + rxp[0][i] - (p[0][i])
}
According to the 'Locals' tab, rxp has only 3 'children' with only 1 items / child.
So I guess the problem is here:
void getrxp()
{
for (int i = 0; i < 6; i++)
{
rxp[0] = new double[]{ M[0][0] * (re[0][i]) + M[0][1] * (re[1][i]) + M[0][2] * 0};
rxp[1] = new double[]{ M[1][0] * (re[0][i]) + M[1][1] * (re[1][i]) + M[1][2] * 0};
rxp[2] = new double[]{ M[2][0] * (re[0][i]) + M[2][1] * (re[1][i]) + M[2][2] * 0};
}
}
Am I getting this to to create a double rxp[3][6]; array wrong or there is something else?
The original code looks like this (C):
void getrxp()
{
for(int i=0;i<6;i++){
rxp[0][i] = M[0][0]*(re[0][i])+M[0][1]*(re[1][i])+M[0][2]*0;
rxp[1][i] = M[1][0]*(re[0][i])+M[1][1]*(re[1][i])+M[1][2]*0;
rxp[2][i] = M[2][0]*(re[0][i])+M[2][1]*(re[1][i])+M[2][2]*0;
}
}
If you are trying to create a 2-dimensional array, the syntax should be double [,] rxp = new double[3,6];. Here's the detailed documentation of Multidimensional Arrays at MSDN.
Related
In this case,this is the array which serves as coefficients and degrees which first value having no degree.
double[] arr = { 12, 2, 3 ,4};
I then made a method to print the above array in terms of polynomial equation.
It gives output in type string as follows :
2x^2 + 3x^3 + 4x^4 + 12
I want to a function which takes an argument x and then solve the above polynomial with respect to value of x.
How can I do that?
Any kind of help will be appreciated!.
Edit: Question Solved
To evaluate it you can simply sum the power values times the coefficients. Using LINQ, that's one line:
double result = arr.Select((c,i) => c * Math.Pow(x, i)).Sum();
Here i is the index into your array, it starts at zero, so x^0 = 1 * 12 == 12 etc.
You can also do it without LINQ like this:
List<string> debug = new List<string>();
double y = 1.0;
result = 0.0;
for (int i = 0; i < arr.Length; i++)
{
debug.Add($"{arr[i]} * x^{i}");
result = result + arr[i] * y;
y = y * x;
}
Console.WriteLine(string.Join(" + ", debug));
Console.WriteLine(result);
Which, for x=3 outputs:
12 * x^0 + 2 * x^1 + 3 * x^2 + 4 * x^3
153
Same result as LINQ.
This is what I created:
for (int i = 1; i < degree.Length; i++)
{
result_first += degree[i] * Math.Pow(x, degree[i]);
}
result_first += degree[0];
It works perfectly.
This question already has answers here:
What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?
(5 answers)
Closed 5 years ago.
What's the problem here? I have a block with 2500 random number and i want to sort them with bubble sort. But when I run the program I've got this:
System.IndexOutOfRangeException
error code after this:
if (szamok[i] > szamok[i + 1]).
(Sorry for bad English :/)
int r = 2500;
int seged;
while (r > 1)
{
for (int i = 0; i < 2500; i++)
{
if (szamok[i] > szamok[i + 1])
{
seged = szamok[i + 1];
szamok[i + 1] = szamok[i];
szamok[i] = seged;
}
}
r = r - 1;
}
The error says it: Your index is out of range. You are trying to access an element in your array after the last element in this array.
The line szamok[i] > szamok[i + 1] seems to be the culprit. +1 is one too many.
Try changing you loop so you don't visit the last element but only the second to last:
for (int i = 0; i < (2500-1); i++)
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm doing a project using key stimulation and I'm using two ways to display a key character on the screen. First I use Clipboard but using Clipboard sometimes is inconvenient so I try to use SendInput() to display the key character when user press any button on the keyboard. I follow the code tutorial but I didn't know how does the SendInput() function work ( I mean when I invoke SendInput() what will it work step by step?). Does anyone have experience about my case? Please explain clearly for me.
Thanks all!
private void SendUnicode(string _text)
{
if (IntPtr.Size == 4)
{
ViKey.INPUT[] input = new ViKey.INPUT[_text.Length * 2];
for (int i = 0; i < _text.Length; i++)
{
input[i * 2].type = 1;
input[i * 2].ki.wVk = 0;
input[i * 2].ki.wScan = (short)_text[i];
input[i * 2].ki.dwFlags = 4;
input[i * 2 + 1] = input[i * 2];
input[i * 2 + 1].ki.dwFlags = 6;
}
ViKey.SendInput((uint)input.Length, input, Marshal.SizeOf(input[0]));
return;
}
else
{
ViKey.INPUT64[] inputx64 = new ViKey.INPUT64[_text.Length * 2];
for (int i = 0; i < _text.Length; i++)
{
inputx64[i * 2].type = 1;
inputx64[i * 2].ki.wVk = 0;
inputx64[i * 2].ki.wScan = (short)_text[i];
inputx64[i * 2].ki.dwFlags = 4;
inputx64[i * 2 + 1] = inputx64[i * 2];
inputx64[i * 2 + 1].ki.dwFlags = 6;
}
ViKey.SendInput((uint)inputx64.Length, inputx64, Marshal.SizeOf(inputx64[0]));
}
}
All relevant information can be found in the documentation about the function.
The function returns the number of events that it successfully
inserted into the keyboard or mouse input stream. If the function
returns zero, the input was already blocked by another thread.
So how it works is that it inserts the events in the INPUT structures (which are fed to the function through the parameters) serially into the keyboard or mouse input stream and then returns the number of those events that are successfully inserted..
This question already has answers here:
Add new item in existing array in c#.net
(20 answers)
Closed 8 years ago.
Whenever I need to add an element to an array I've always used this algorithm
data toAdd = 10;
data[] theArray = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
data[] tempArray = new int[theArray.Length + 1];
for (int i = 0; i < theArray.Length; i++)
{
tempArray[i] = theArray[i];
}
theArray = new data[tempArray.Length];
for (int i = 0; i < theArray.Length; i++)
{
theArray[i] = tempArray[i];
}
theArray[theArray.Length - 1] = toAdd;
However I was wondering if there was a better way to do this, as with much larger arrays this will require a large amount of computing time.
You could use Array.Resize. But you should really use an ArrayList or List<> which do this much more easily and effectively.
I've been looking for a while and i couldn't find anything that would help me with my problem, but sorry if i missed something.
So for school we had to learn VB and make a game, and i chose to make Sudoku. I found VB easy to understand so i decided to try a different language to see if it was the same. C# was my choice. I decided to start off by making the Sudoku game again and compare it to my VB game.
In the VB code i was able to make an array of all the textboxes that make up the 9x9 grid from the code:
For Y = 0 to 8
For X = 0 to 8
Grid(X, Y) = New Windows.Forms.TextBox
Pencil(X, Y) = New Windows.Forms.TextBox
With Grid(X, Y)
.BackColor = Grid(X, Y).BackColor
.Name = Asc(97 + X) & Y + 1
.Location = New System.Drawing.Point(35 + 50 * X + (FindBox(X) - 1) * 15, 50 + 50 * Y + (FindBox(Y) - 1) * 15)
.Size = New System.Drawing.Size(50, 50)
.Multiline = True
.MaxLength = 1
.Font = New Font(Grid(X, Y).Font.Name, Grid(X, Y).Font.Size + 10)
.TextAlign = HorizontalAlignment.Center
.TabIndex = (X + 1) + (Y * 9) + 1
.BorderStyle = BorderStyle.FixedSingle
End With
Me.Controls.Add(Grid(X, Y))
next
next
This meant i could easily refer to the Sudoku textbox's as a grid coordinate in the array. I attempted to replicate this in C# and ran into a problem almost instantly
public partial class Form1 : Form
{
TextBox[,] Grid = new TextBox[8,8];
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
for (int Y = 0; Y < 9; Y++)
{
for (int X = 0; X < 9; X++)
{
TextBox TBox = new TextBox();
Grid[X, Y] = TBox;
TBox.Location = new Point(50 + X * 100, 50 + Y * 50);
this.Controls.Add(TBox);
}
}
}
This code runs, but for some reason it only runs till Y = 7, then stops and does not loop any more times. This code works fine until i try to add anything that links the textbox's to the array (In this case Grid[X,Y] = TBox). I've tried it without using TBox (And just straight away using the array, but the same problem persists).
Just wondering if anyone can enlighten me as to why adding the line "Grid[X, Y] = TBox;" can completely ruin a nested for loop.
Thanks in advance, sorry if i didn't say enough/Said too much.
There is an important difference between C# and VB.NET in the context of arrays. Just a simple example. In C# the following array has exactly 10 elements and allowed indexes are from 0 to 9:
int[] array= new int[10];
In VB.NET the following array has 11 elements and allowed indexes are from 0 to 10:
Dim array(10) as Integer
You translated you code from VB.NET to C# without taking this difference into account and it is why you have problems. To fix this problem you should use:
TextBox[,] Grid = new TextBox[9,9];
Instead of:
TextBox[,] Grid = new TextBox[8,8];
It doesn't just stop.You get a IndexOutOfRangeException
Change this
new TextBox[8,8]
to this
new TextBox[9,9]
Or make the for loops "< 8"