I am using c# and I have the flowing problem:
I decelerated the height variable before for cycle ant it says do not exist in the cycle.
In this part of the code I want the program to store all of the 3rd element of the "data" array except the first, so if the "data" array looks like this: 1,2,3,4,5,6,7,8,9,10,11,12... I want to get: 6,9,12...
static int[] tall()
{
int[] data = database();//recalling an array filled with numbers
int j = 0;
int[] height;
for (int i = 6; i < data.Length; )
{
i = i + 3;
j++;
height[j] = data[i];//Use of unassigned local variable 'height'
}
return height;
}
The compiler tells you what the problem is. Unfortunately, it does not tell you how to fix it.
You need to assign height to an array of int, but first you need to figure out its length. You can compute the length by subtracting the index of the initial data point (i.e. 6) from the length, dividing the result by 3, and adding 1. This can be simplified to (length-3)/3:
int[] height = new int[(data.Length-3)/3];
This assumes that data.Length is at least 4, otherwise the count is going to be negative.
You should also move the adjustment of indexes to a point after the assignment, or better yet, to the header of the loop:
// Start i at 5, because array indexes are zero-based.
for (int i = 5; i < data.Length; i+=3, j++) {
height[j] = data[i];
}
Demo.
Related
I'm working on a simple matching game and I want to output a leaderboard, but i get an exception array index out of bounds. In the debug though it says that the table's size is 3 and that both i and j are 0 and 1, which is weird to me becouse that is not out of bounds since the array size is 3. This is my code for sorting the array of objects from biggest to smallest by the highscore, thanks for the help.
for(int i = 0; i<tabUporabnikov.Length-1; i++)
{
for(int j = (i+1); j<tabUporabnikov.Length; j++)
{
if (tabUporabnikov[i].highscore < tabUporabnikov[j].highscore)
{
Uporabnik[] zacasna = new Uporabnik[1];
zacasna[1] = tabUporabnikov[j];
tabUporabnikov[j] = tabUporabnikov[i];
tabUporabnikov[i] = zacasna[1];
}
}
}
it should be zacasna[0]. Aray can have only one value in this case and array starts with index 0. so it should be zacasna[0]. better you declare a variable instead of array because you need to store only value. declare it as Uporabnik zacasna
I am coding an algorithm and I am using arrays of custom data and if it looks a bit strange is that I am coding the algorithm for a futures trading platform and so they may have functions that don't look standard C#.
I'm trying to Resize my arrays because I need them resized every time a new value is found to be added and then I use the SetValue sometimes to replace the last value found when a better one is found again within the next 5 values after the last value was set.
Trouble is, when I debug it in Visual Studio, it stops at line after ArrayResize and when I hover over the LastLSwDMIpriceBar[k], it shows the k = 0, just as I expected it to be, as it would be the first element in the array of one, so what Index is then outside the bounds of the array?
The way I understand and hoped the code to work is this: when the conditions met are by setting LSwDMIbool to true, the array is resized from 0 to 1 element and the element with index [0] is then set as LastLSwDMIpriceBar[k]. Am I wrong about this?
Any help would be greatly appreciated.
private int k, l;
private int[] LastLSwDMIpriceBar;
LastLSwDMIpriceBar = new int [1];
.....
if (LSwDMIbool)
{
lastLSwDMIbar = CurrentBar - 2 - LowestBar(LSwDMI, 5);
LastLSwDMI[0] = DMI(Closes[2], Convert.ToInt32(DmiPeriod)).Values[0].GetValueAt(lastLSwDMIbar);
Array.Resize(ref LastLSwDMIpriceBar, l++);
LastLSwDMIpriceBar[k] = CurrentBar - LowestBar(Lows[2], 5);
k++;
LSwDMIprice[0] = Lows[2].GetValueAt(LastLSwDMIpriceBar[k]);
}
......
if(!LSwDMIbool)
{
for (int LastBar = CurrentBar - 1; IsFirstTickOfBar && LastBar <= lastLSwDMIbar + 5; LastBar++)
LastLSwDMIpriceBar.SetValue((CurrentBar - LowestBar(Lows[2], 5)), k);
}
You may find this helpful:
https://stackoverflow.com/a/24858/12343726
I believe you want ++l instead of l++ so that the new value of l is used when resizing the array. I suspect that l is zero the first time Array.Resize is called so the array is being resized to zero.
I'm trying to Resize my arrays because I need them resized every time a new value is found to be added
Don't use an array for this!
Instead, use a List<T>.
private int k, l;
private List<int> LastLSwDMIpriceBar = new List<int>();
.....
if (LSwDMIbool)
{
lastLSwDMIbar = CurrentBar - 2 - LowestBar(LSwDMI, 5);
LastLSwDMI[0] = DMI(Closes[2], Convert.ToInt32(DmiPeriod)).Values[0][lastLSwDMIbar];
var newValue = CurrentBar - LowestBar(Lows[2], 5);
LastLSwDMIpriceBar.Add(newValue);
k++;
LSwDMIprice[0] = Lows[2][newValue];
}
......
if(!LSwDMIbool)
{
for (int LastBar = CurrentBar - 1; IsFirstTickOfBar && LastBar <= lastLSwDMIbar + 5; LastBar++)
LastLSwDMIpriceBar[k] = CurrentBar - LowestBar(Lows[2], 5));
}
I am working with Arrays and conditionals statements, little lost right now and was hoping for some input.
So, I created two Arrays
int[] one = new int[] {
4160414, 6610574, 2864453, 9352227, -4750937, -3132620, 2208017,
-2226227, -8415856, -9834062, -3401569, 7581671, 8068562, 7520435,
-9277044, -7821114, -3095212, 966785, 6873349, -8441152, -7015683,
-6588326, -282013, 4051534, 9930123, -3093234 };
int[] two = new int[] {
1099626, 6083415, 8083888, -8210392, 2665304, -8710738, -8708241,
8859200, -1255323, 5604634, 2921294, -7260228, 7261646, 1137004,
5805162, 4883369, 8789460, 9769240, 319012, -7877588, -1573772,
5192333, 1185446, 1302131, 4217472, -3471445};
My next step what i was thinking is i am going to have to loop through each array
for (int i = 0; i < one.Length; i++)
{
int xValue = one[i];
for (int j = 0; j < two.Length; j++)
{
int yValue = two[j];
}
}
Now that i have the index of each Array i need to check wether the index of xValue is less than the index of yValue
if (xValue < yValue)
{
// dO SOMETHING HERE
}
if (yValue < xValue)
{
// Do Something HERE
}
Where i am getting confused at, is with C# from my understanding you can not push new values into an Array, it needs to be a new instance of the array and copy?
So i tried doing
if (xValue < yValue)
{
Array.Copy(one, x, 13);
}
if (yValue < xValue)
{
Array.Copy(two, x, 13)
}
Both Arrays have 26 values, so a new array of 13 would need to be created to insert the checked value, but Array.Copy seems to not be working getting an array out of bounds check lower bounds.
I'm just confused on checking the values of both arrays at their index, then grabbing the smallest value of the checked values then taking that small value and inserting it into a new array, then use a foreach-loop to iterate over it and print the values to the console. FacePalm
You can use LINQ's Zip to achieve this:
int[] smallest = one.Zip(two, (o, t) => Math.Min(o,t)).ToArray();
Essentially, Zip will provide both items to the lambda expression, allowing you to combine them how you see fit. In this case, we just choose the minimum and return it.
Try it online
Basically, you need to define the size of the new array when you declare it. Make it the same size as one. Then add the smallest item from one or two on each iteration by comparing the items in each array at index i.
int[] smallest = new int[one.Length];
for (int i = 0; i < one.Length; i++)
{
if (one[i] < two[i])
{
smallest[i] = one[i];
}
else
{
smallest[i] = two[i];
}
}
How can I loop through the values that are returned from a specified range?
I tried the following code but it returns:
Cannot apply indexing with [] to an expression of type 'System.Array'
var rng = (Excel.Range)sheet.Range[sheet.Cells[3, 2], sheet.Cells[3, 27]];
var wsValues = rng.Cells.Value;
for (var j = 0; j < wsValues.Length; j++)
{
var test = wsValues[j];
}
Its a system array not a 'normal array' (based upon the error). So you should be able to use var test = wsValues.GetValue(1, j); Note this will return an object. So you will have to do a cast to your expected type.
Also note that the 1 in that case is because all your test data has 1 as your first value in your multidimensional array. You can view a multidimensional array as a grid, with an x co-ordinate and a y co-ordinate, so if you wanted to iterate through them properly (as if you were using up multiple columns instead of just one). Then you would do:
for (var x = 1; x <= wsValues.GetLength(0); x++)
{
for (var y = 1; y <= wsValues.GetLength(1); y++)
{
var test = wsValues[x, y];
}
}
In this the GetLength command returns the length of that dimension, so the first one returns the length of how many 'columns' or the maximum x value there are, and the second one returns the maximum y value or how many rows there are.
I have a measurement array of 1024 values. But in this array are some values wrong like noise/peaks. The array is normally like sinus so all values should be in a good line.
How can i create a filter to the array to remove these peaks and noise?
I heard something of an algorithm that compares always three values and creates the mean-value of it but I can't find an example of this.
The wrong values are "peaks" which are bigger than the value before so it is perhaps easier if i just compare the value with the value before for a given "Offset"?
But how to do this?
public int FilterArray(double[] Values, double Offset, out double[] Results)
{
int ArrLength = Values.Length;
Results = new double[ArrLength];
for (int i = 0; i < ArrLength; i++)
Values[i] = 0;
try
{
for (int i = 0; i < ArrLength; i++)
{
if (Values[i+1] + Offset) > (Values[i]
{
// here someting is needed
}
else
{
// nothing to do if next value is ok
}
}
}
return 0;
}
Thanks for all help
I suggest to use a List instead of an Array. If you need an Array to process your data you can use the ToArray() Method but removing/filtering items from a List is way more easy than resizing Arrays.
It seems you heard about Median Filter (due to phrase that compares always three values phrase). It works good when you have to remove rare peaks.
Example: for data point triplet [3, 100, 7] algorithm finds that median (not mean!) value of this triplet is 7, and (100) data point is replaced by (7) value.
public int FilterArray(List<double> Values, double Offset, out double[] Results)
{
foreach(double value in new List<double>(Values))
{
// some logic
Values.Remove(value);
}
return 0; //??
}