How to Display a Jagged Array? - C# - c#

I am trying to get a jagged array that will solve the sums of the elements from dailyTemperature and that will successfully display them in the Main () under the Weather class. This is what I have so far.
public int[,] GetTemperatures()
{
int[,] dailyTemperature =
{
{38, 40, 42, 34},
{55, 41, 40, 30},
{28, 39, 21, 60},
{61, 52, 43, 42},
{35, 36, 30, 29},
{24, 33, 37, 40}
};
Console.Write(" ");
for (int i = 0; i < dailyTemperature.Length; i++)
{
Console.Write(dailyTemperature[i] + " ");
}
Console.WriteLine();
for (int i = 0; i < dailyTemperature.GetLength(0); i++)
{
Console.Write(dailyTemperature[i].Substring(0, dailyTemperature[i].Length - 1) + " ");
for (int j = 0; j < dailyTemperature.GetLength(1); j++)
{
Console.Write(dailyTemperature[i, j] + " ");
}
Console.WriteLine();
}
return dailyTemperature;
}
{
public int[] GetTemperatureSum(int [,] dailyTemperature)
{
int[][] temperatureSum= new int[6][];
temperatureSum[0][1] = dailyTemperature[0, 0] + dailyTemperature[0, 1] + dailyTemperature[0, 2] + dailyTemperature[0, 3];
temperatureSum[0][2] = dailyTemperature[1, 0] + dailyTemperature[1, 1] + dailyTemperature[1, 2] + dailyTemperature[1, 3];
temperatureSum[0][3] = dailyTemperature[2, 0] + dailyTemperature[2, 1] + dailyTemperature[2, 2] + dailyTemperature[2, 3];
temperatureSum[0][4] = dailyTemperature[3, 0] + dailyTemperature[3, 1] + dailyTemperature[3, 2] + dailyTemperature[3, 3];
temperatureSum[0][5] = dailyTemperature[4, 0] + dailyTemperature[4, 1] + dailyTemperature[4, 2] + dailyTemperature[4, 3];
temperatureSum[0][6] = dailyTemperature[5, 0] +dailyTemperature[5, 1] + dailyTemperature[5, 2] + dailyTemperature[5, 3];
for (int i = 0; i < temperatureSum.GetLength(0); i++)
{
for (int j=0; j<temperatureSum.GetLength(1); j++)
{
Console.Write(temperatureSum[i][j]);
}
Console.WriteLine();
}
return temperatureSum;
}
static void Main(string[] args)
{
Weather weather= new Weather();
{
weather.GetTemperatures();
weather.GetTemperatureSum(dailyTemperature);
}
};
My strategy here is to display all of these methods through the Main() method, however I am getting an error under (dailyTemperature) by weather.GetTemperatureSum. This is what the error says, "An object reference is required for the nonstatic field, method, or property 'member'".
I'd love it if someone could help me on this, although as I understand the formatting has to be relatively similar to this. I absolutely need to, for example, solve the sums through the computer as opposed to doing so manually. Thanks.

More details about:
Multidimensional Arrays
Jagged Arrays
Multidimensional Array [][] vs [,] [duplicate]
int[,] dailyTemperatures =
{
{ 38, 40, 42, 34 },
{ 55, 41, 40, 30 },
{ 28, 39, 21, 60 },
{ 61, 52, 43, 42 },
{ 35, 36, 30, 29 },
{ 24, 33, 37, 40 }
};
Weather.GetTemperatures(dailyTemperatures);
Weather.GetTemperatureSum(dailyTemperatures);
public static class Weather
{
public static int[,] GetTemperatures(int[,] dailyTemperatures)
{
var rows = dailyTemperatures.GetUpperBound(0) + 1;
var columns = dailyTemperatures.Length / rows;
for (var i = 0; i < rows; i++)
{
for (var j = 0; j < columns; j++)
{
Console.Write($"{dailyTemperatures[i, j]} \t");
}
Console.WriteLine();
}
return dailyTemperatures;
}
public static int[] GetTemperatureSum(int[,] dailyTemperature)
{
var temperatureSum = new int[6];
temperatureSum[0] = dailyTemperature[0, 0] + dailyTemperature[0, 1] + dailyTemperature[0, 2] + dailyTemperature[0, 3];
temperatureSum[1] = dailyTemperature[1, 0] + dailyTemperature[1, 1] + dailyTemperature[1, 2] + dailyTemperature[1, 3];
temperatureSum[2] = dailyTemperature[2, 0] + dailyTemperature[2, 1] + dailyTemperature[2, 2] + dailyTemperature[2, 3];
temperatureSum[3] = dailyTemperature[3, 0] + dailyTemperature[3, 1] + dailyTemperature[3, 2] + dailyTemperature[3, 3];
temperatureSum[4] = dailyTemperature[4, 0] + dailyTemperature[4, 1] + dailyTemperature[4, 2] + dailyTemperature[4, 3];
temperatureSum[5] = dailyTemperature[5, 0] + dailyTemperature[5, 1] + dailyTemperature[5, 2] + dailyTemperature[5, 3];
for (var i = 0; i < temperatureSum.GetLength(0); i++)
{
Console.WriteLine(temperatureSum[i]);
}
return temperatureSum;
}
}
In the GetTemperatureSum method, the returned type is an array (int[]).
It is better to pass dailyTemperatures variable as a method parameter

Related

Having trouble generating a mesh in unity

I have been trying to generate a mesh using the code below, and have been successful generating a mesh with width and length of one, but any more and I get an error in the console that says
"Failed setting triangles. Some indices are referencing out of bounds vertices. IndexCount: 24, VertexCount: 9 UnityEngine.Mesh:set_triangles (int[])"
I have done all the math of calculating the triangles on paper, and all of the values are within the length of the vertices array, going from 0 to 8. I don't have any clue what I am doing wrong, or why this error message is getting thrown, so any help would be greatly appreciated.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MeshGenerator : MonoBehaviour
{
Mesh mesh;
Vector3[] vertices;
int[] triangles;
public Mesh GenerateMesh(int mapWidth, int mapHeight)
{
mesh = new Mesh();
vertices = new Vector3[(mapHeight + 1) * (mapWidth + 1)];
triangles = new int[6 * (mapHeight * mapWidth)];
int index = 0;
for (int x = 0; x <= mapHeight; x++)
{
for (int y = 0; y <= mapWidth; y++)
{
vertices[index] = new Vector3(x, 0, y);
index += 1;
}
}
int z = 0;
for (int i = 0; i <= triangles.Length - 1; i += 6)
{
Debug.Log(i);
if (z == mapWidth)
{
triangles[i] = i + 1;
triangles[i + 1] = i + 2;
triangles[i + 2] = i + mapWidth + 3;
triangles[i + 3] = i + 1;
triangles[i + 4] = i + mapWidth + 3;
triangles[i + 5] = i + mapWidth + 2;
z = 1;
}
else
{
triangles[i] = i;
triangles[i + 1] = i + 1;
triangles[i + 2] = i + mapWidth + 2;
triangles[i + 3] = i;
triangles[i + 4] = i + mapWidth + 2;
triangles[i + 5] = i + mapWidth + 1;
z++;
}
}
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.RecalculateNormals();
return mesh;
}
}
I figured it out. The problem was I was using the index of the triangle array to calculate the index of the vertex array. Instead, I needed to use a separate variable (a) to calculate the vertex index. The new code is below.
int a = 0;
for (int i = 0; i < triangles.Length; i += 6)
{
Debug.Log(i);
if (z == mapWidth)
{
triangles[i] = a + 1;
triangles[i + 1] = a + 2;
triangles[i + 2] = a + mapWidth + 3;
triangles[i + 3] = a + 1;
triangles[i + 4] = a + mapWidth + 3;
triangles[i + 5] = a + mapWidth + 2;
z = 1;
a += 2;
}
else
{
triangles[i] = a;
triangles[i + 1] = a + 1;
triangles[i + 2] = a + mapWidth + 2;
triangles[i + 3] = a;
triangles[i + 4] = a + mapWidth + 2;
triangles[i + 5] = a + mapWidth + 1;
z++;
a++;
}
}

C# Dividing a Matrix into Sub Blocks

i have a matrix came from an image 1600x1600. now i need to assign this matrix into 4x4 blocks. As an example:
00 01 02 03
IMAGE = 04 05 06 07 BLOCK(i) = 00 01 BLOCK(i+1) = 02 03
08 09 0A 0B 04 05 06 07
0C 0D 0E 0F
BLOCK(i+2) = 08 09 BLOCK(i+3) = 0A 0B
0C 0D = 0E 0F
1 ) Firstly i dont know the image dimensions , the user opens it. i get it later. my test image 1600x1600.But blocks dimensions are fixed at 4x4. And image dimensions are , lets we agree can be divided with 4 for now...
2 ) I dont know how many blocks going to be.
3 ) I need to acces the row and coloumb of the blocks later because i will be performing mathematical operations with the blocks later... For example , XOR operation with block(n)[x,y] with block(n+1) [x,y].
So this decleration part , this part of the program is very very important.
i stucked this part of the program for 2 weeks i cant continue. Pls help me. İt looks very simple code but.......
My structure is like this , begining part
private void Form1_Load(object sender, EventArgs e)
{
Bitmap bmp = new Bitmap("c:\\yavanna.jpg");
pictureBox1.Image = Image.FromFile("c:\\yavanna.jpg");
int width = bmp.Width;
int height = bmp.Height;
Color p;
int[,] alpha_map_int = new int[width, height];
int[,] red_map_int = new int[width, height];
int[,] green_map_int = new int[width, height];
int[,] blue_map_int = new int[width, height];
int[,] grayscale_map_int = new int[width, height];
string[,] gray_scale_map = new string[width, height];
string temp_hexValue;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
//get pixel value
p = bmp.GetPixel(x, y);
//extract pixel component ARGB
int a = p.A;
alpha_map_int[x, y] = a;
int r = p.R;
red_map_int[x, y] = r;
int g = p.G;
green_map_int[x, y] = g;
int b = p.B;
blue_map_int[x, y] = b;
//convert to gryscale
double grayscale = 0.2126 * red_map_int[x,y] + 0.7152 * green_map_int[x, y] + 0.0722 * blue_map_int[x, y];
grayscale_map_int[x, y] = Convert.ToInt32(grayscale);
temp_hexValue = Convert.ToString(grayscale_map_int[x, y]);
gray_scale_map[x, y] = temp_hexValue;
}
}
Here's a version of jdweng's answer, that generates 4x4 arrays and handles source arrays that don't divide by 4. You can see why he posted a simplified sample. Any bigger and it would be worth using two more loops to populate the 4x4 array.
'image' is the input, 'bytes4x4' is the output.
List<List<List<byte>>> bytes4x4 = new List<List<List<byte>>>();
for (int row = 0; row<length-3 ; row += 4)
{
for (int col = 0; col<width-3; col += 4)
{
bytes4x4.Add(new List<List<byte>>() {
new List<byte>() { image[row, col], image[row, col + 1], image[row, col + 2], image[row, col + 3]},
new List<byte>() { image[row + 1, col], image[row + 1, col + 1], image[row + 1, col + 2], image[row + 1, col + 3] },
new List<byte>() { image[row + 2, col], image[row + 2, col + 1], image[row + 2, col + 2], image[row + 2, col + 3] },
new List<byte>() { image[row + 3, col], image[row + 3, col + 1], image[row + 3, col + 2], image[row + 3, col + 3] }
});
}
This declares and populates 'bytes4x4', which is a long list of two dimensional blocks. Access a block like this:
var block100 = bytes4x4[100];
And use that to get a pixel:
var block100pixelrow1col3 = block100[1][3];
or
var block100pixelrow1col3 = bytes4x4[100][1][3];
Note that these indexs are all zero based, so there's no element [4] in the blocks.
Now I think about it, you might be after a 2-dimensional array of 2-dimensional blocks. If so the code would look like this:
var bytes4x4 = new List<List<List<List<byte>>>>();
for (int row = 0; row<length-3 ; row += 4)
{
var row = new List<List<List<byte>>>();
bytes4x4.Add(row);
for (int col = 0; col<width-3; col += 4)
{
row.Add(new List<List<byte>>() {
new List<byte>() { image[row, col], image[row, col + 1], image[row, col + 2], image[row, col + 3]},
new List<byte>() { image[row + 1, col], image[row + 1, col + 1], image[row + 1, col + 2], image[row + 1, col + 3] },
new List<byte>() { image[row + 2, col], image[row + 2, col + 1], image[row + 2, col + 2], image[row + 2, col + 3] },
new List<byte>() { image[row + 3, col], image[row + 3, col + 1], image[row + 3, col + 2], image[row + 3, col + 3] }
});
}
Then you could access a block that is 14 rows down and 23 columns across like this:
var block14by23 = bytes4x4[14][23];
Try following :
const string FILENAME = #"c:\temp\test.jpg";
public Form1()
{
InitializeComponent();
Bitmap image = new Bitmap(FILENAME);
int height = (int)image.Height ;
int width = (int)image.Width;
List<List<List<Color>>> bytes = new List<List<List<Color>>>();
List<List<List<Int32>>> grayscale_map_int = new List<List<List<Int32>>>();
for (int row = 0; row < height; row += 4)
{
for (int col = 0; col < width; col += 4)
{
bytes.Add(new List<List<Color>>() {
new List<Color>() { image.GetPixel(col, row), image.GetPixel(col + 1, row), image.GetPixel(col + 2, row), image.GetPixel(col + 3, row)} ,
new List<Color>() { image.GetPixel(col, row + 1), image.GetPixel(col + 1, row + 1), image.GetPixel(col + 2, row + 1), image.GetPixel(col + 3, row + 1)} ,
new List<Color>() { image.GetPixel(col, row + 2), image.GetPixel(col + 1, row + 2), image.GetPixel(col + 2, row + 2), image.GetPixel(col + 3, row + 2)} ,
new List<Color>() { image.GetPixel(col, row + 3), image.GetPixel(col + 1, row + 3), image.GetPixel(col + 2, row + 3), image.GetPixel(col + 3, row + 3)} ,
});
grayscale_map_int.Add(new List<List<Int32>>() {
new List<Int32>() { GetGrayScale(image.GetPixel(col, row)), GetGrayScale(image.GetPixel(col + 1, row)), GetGrayScale(image.GetPixel(col + 2, row)), GetGrayScale(image.GetPixel(col + 3, row))} ,
new List<Int32>() { GetGrayScale(image.GetPixel(col, row + 1)), GetGrayScale(image.GetPixel(col + 1, row + 1)), GetGrayScale(image.GetPixel(col + 2, row + 1)), GetGrayScale(image.GetPixel(col + 3, row + 1))} ,
new List<Int32>() { GetGrayScale(image.GetPixel(col, row + 2)), GetGrayScale(image.GetPixel(col + 1, row + 2)), GetGrayScale(image.GetPixel(col + 2, row + 2)), GetGrayScale(image.GetPixel(col + 3, row + 2))} ,
new List<Int32>() { GetGrayScale(image.GetPixel(col, row + 3)), GetGrayScale(image.GetPixel(col + 1, row + 3)), GetGrayScale(image.GetPixel(col + 2, row + 3)), GetGrayScale(image.GetPixel(col + 3, row + 3))} ,
});
}
}
}
public Int32 GetGrayScale(Color color)
{
return Convert.ToInt32(0.2126 * color.R + 0.7152 * color.G + 0.0722 * color.B);
}

C# How to improve loop populating Excel cells

I'm mainly parsing large amounts of text from a text file and then populating it into an excel.
//populate into worksheet
for (int x = 0; x < rawLine.Length; x++)
{
string[] tempLine = rawLine[x].Split(';');
for (int y = 0; y < tempLine.Length; y++)
{
DateTime hour = Convert.ToDateTime(tempLine[6]);
xlWorkSheet.Cells[y + 2, 1] = tempLine[0];
xlWorkSheet.Cells[y + 2, 2] = tempLine[1];
xlWorkSheet.Cells[y + 2, 3] = tempLine[2];
xlWorkSheet.Cells[y + 2, 4] = tempLine[3];
xlWorkSheet.Cells[y + 2, 5] = tempLine[4];
xlWorkSheet.Cells[y + 2, 6] = tempLine[5];
xlWorkSheet.Cells[y + 2, 7] = tempLine[6];
xlWorkSheet.Cells[y + 2, 8] = tempLine[7];
xlWorkSheet.Cells[y + 2, 9] = tempLine[8];
xlWorkSheet.Cells[y + 2, 10] = tempLine[9];
xlWorkSheet.Cells[y + 2, 11] = tempLine[10];
xlWorkSheet.Cells[y + 2, 12] = hour.Hour;
xlWorkSheet.Cells[y + 2, 13] = tempLine[8] == "0" ? "SAME" : tempLine[9];
}
Console.WriteLine("Current line = " + x + "\n");
}
Currently this code works, but it's just taking way too long. Is there anyway to speed it up? I have done some searching but found nothing much specific.
Thanks in advance.
It will probably be a very small improvement, but this line:
DateTime hour = Convert.ToDateTime(tempLine[6]);
Should be moved outside the y loop because it doesn't depend on it.
Other than that, you should probably look into some way to set multiple cells at the same time--most of the time is probably spent doing round trips to Excel. (It looks like this is what #Gusman suggests in the comments).
#Mohit's answer is good too because it is much shorter and simpler.
You can try:
//populate into worksheet
DateTime hour;
string[] tempLine;
StringBuilder output = new StringBuilder();
for (int x = 0; x < rawLine.Length; x++)
{
tempLine = rawLine[x].Split(';');
for (int y = 0; y < tempLine.Length; y++)
{
hour = Convert.ToDateTime(tempLine[6]);
xlWorkSheet.Cells[y + 2, 1] = tempLine[0];
xlWorkSheet.Cells[y + 2, 2] = tempLine[1];
xlWorkSheet.Cells[y + 2, 3] = tempLine[2];
xlWorkSheet.Cells[y + 2, 4] = tempLine[3];
xlWorkSheet.Cells[y + 2, 5] = tempLine[4];
xlWorkSheet.Cells[y + 2, 6] = tempLine[5];
xlWorkSheet.Cells[y + 2, 7] = tempLine[6];
xlWorkSheet.Cells[y + 2, 8] = tempLine[7];
xlWorkSheet.Cells[y + 2, 9] = tempLine[8];
xlWorkSheet.Cells[y + 2, 10] = tempLine[9];
xlWorkSheet.Cells[y + 2, 11] = tempLine[10];
xlWorkSheet.Cells[y + 2, 12] = hour.Hour;
xlWorkSheet.Cells[y + 2, 13] = tempLine[8] == "0" ? "SAME" : tempLine[9];
}
output.AppendLine("Current line = " + x);
}
Console.WriteLine(output.ToString());
May be just to improve the loop you can write like this. This does not improve the performance but would look cleaner.
for (int x = 0; x < rawLine.Length; x++)
{
string[] tempLine = rawLine[x].Split(';');
for (int y = 0; y < tempLine.Length; y++)
{
DateTime hour = Convert.ToDateTime(tempLine[6]);
for(int z=0; z<11; z++)
{
xlWorkSheet.Cells[y + 2, (z+1)] = tempLine[z];
}
xlWorkSheet.Cells[y + 2, 12] = hour.Hour;
xlWorkSheet.Cells[y + 2, 13] = tempLine[8] == "0" ? "SAME" : tempLine[9];
}
Console.WriteLine("Current line = " + x + "\n");
}

c# 2D array bubblesort [closed]

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 8 years ago.
Improve this question
I am trying to perform a bubble sort on a 2D array, sorting by the third index (the integer)
string[,] edges = new string[,] { {"A", "B", "2"},
{"A", "C", "3"},
{"A", "E", "10"},
{"B", "C", "5"},
{"B", "D", "10"},
{"C", "D", "2"},
{"D", "E", "5"},
{"E", "B", "3"}
};
I get an IndexOutOfRangeException on IF statenent of the sort code
string[] temp = {};
//sort edges and add them to sortedEdges - using bubblesort
for (int i = 0; i < edges.Length - 1; i++){
for (int j = 0; j < edges.Length - 1; j++){
if (Int32.Parse(edges[i, 2]) > Int32.Parse(edges[i + 1, 2])){
//make a swap
//put array record i into temp holder
temp[0] = edges[i, 0];
temp[1] = edges[i, 1];
temp[2] = edges[i, 2];
//copy i + 1 into i
edges[i, 0] = edges[i + 1, 0];
edges[i, 1] = edges[i + 1, 1];
edges[i, 2] = edges[i + 1, 2];
//copy temp into i + 1
edges[i + 1, 0] = temp[0];
edges[i + 1, 1] = temp[1];
edges[i + 1, 2] = temp[2];
}
}
}
My question is, how do I fix this so that the array "edges" is filled with the rows, ordered by the third column?
Thanks.
UPDATED v-3
The problem was in Length for such defined table which in your case was 24 because it seems to count all the elements in both dimensions.
Please try the following code:
string[] temp = new string[3];
for (int i = 0; i < edges.GetLength(0) - 1; i++){
int j;
j = 0;
for (; j < edges.GetLength(0) - 1; j++){
if (Int32.Parse(edges[j, 2]) > Int32.Parse(edges[j + 1, 2])){
//make a swap
//put array record j into temp holder
temp[0] = edges[j, 0];
temp[1] = edges[j, 1];
temp[2] = edges[j, 2];
//copy j + 1 into j
edges[j, 0] = edges[j + 1, 0];
edges[j, 1] = edges[j + 1, 1];
edges[j, 2] = edges[j + 1, 2];
//copy temp into j + 1
edges[j + 1, 0] = temp[0];
edges[j + 1, 1] = temp[1];
edges[j + 1, 2] = temp[2];
}
}
}
You can find (updated) working example here: https://dotnetfiddle.net/FQs4OA

MS Charts C# DataSource from array or List

i want to fill the data of the charts from values i have in 2D array, one column will present the X-axis and the second is present the Y-axis.. i did it, but its not reading from the array, it gives me the default line when i run the application, i found a solution using List<>, i had an error, so if any one could help me in this, i will be thankful :D
this is the code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ICS381Project
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
int[,] AndFunction = { { 0, 0, 0 }, { 0, 1, 0 }, { 1, 0, 0 }, { 1, 1, 1 } }; // intialized the function
int[,] D_n = new int[4, 1]; // creating the D(n)
int[,] x_n = new int[4, 3]; // creating X(n) vectors X1 --> x4
int[,] W_n = { { 0, 0, 0 } }; // creating and intiallizing W(n) vectors W1 --> w4
int[,] W_n_1 = { { 0, 0, 0 } };
int[,] Delta_W_n = { { 0, 0, 0 } };
int wx = 0; //
int Y_n = 0; //
int d_y = 0; //
for (int i = 0; i < 4; i++)
{
D_n[i, 0] = AndFunction[i, 2];
}
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 3; j++)
{
if (j == 2)
x_n[i, j] = 1;
else
x_n[i, j] = AndFunction[i, j];
}
}
int step = 0;
int CheckIfNoLearning = 0;
int RowsPointer = 0;
while (CheckIfNoLearning < 4)
{
//Console.Write("step= " + step+1 + "\n");
wx = (x_n[RowsPointer, 0] * W_n[0, 0]) + (x_n[RowsPointer, 1] * W_n[0, 1]) + (x_n[RowsPointer, 2] * W_n[0, 2]);
//Console.Write("[ " + x_n[RowsPointer, 0] + ", " + x_n[RowsPointer, 1] + ", " + x_n[RowsPointer, 2] + "] \t");
//Console.Write("[ " + W_n[0, 0] + ", " + W_n[0, 1] + ", " + W_n[0, 2] + "] \t");
//Console.Write("" + wx + "\t");
if (wx < 0)
Y_n = 0;
else
Y_n = 1;
// Console.Write("" + Y_n + "\t");
d_y = D_n[RowsPointer, 0] - Y_n;
// Console.Write("" + d_y + "\t");
Delta_W_n[0, 0] = d_y * x_n[RowsPointer, 0];
Delta_W_n[0, 1] = d_y * x_n[RowsPointer, 1];
Delta_W_n[0, 2] = d_y * x_n[RowsPointer, 2];
// Console.Write("[ " + Delta_W_n[0, 0] + ", " + Delta_W_n[0, 1] + ", " + Delta_W_n[0, 2] + "] \t");
for (int i = 0; i < 3; i++)
{
W_n_1[0, i] = W_n[0, i] + Delta_W_n[0, i];
}
//Console.Write("[ " + W_n_1[0, 0] + ", " + W_n_1[0, 1] + ", " + W_n_1[0, 2] + "] \n");
if (W_n_1[0, 0] == W_n[0, 0] && W_n_1[0, 1] == W_n[0, 1] && W_n_1[0, 2] == W_n[0, 2] && CheckIfNoLearning == RowsPointer)
CheckIfNoLearning++;
else
CheckIfNoLearning = 0;
W_n[0, 0] = W_n_1[0, 0];
W_n[0, 1] = W_n_1[0, 1];
W_n[0, 2] = W_n_1[0, 2];
//Console.Write("W_n= [ " + W_n[0, 0] + ", " + W_n[0, 1] + ", " + W_n[0, 2] + "] \n");
RowsPointer = (RowsPointer + 1) % 4;
step++;
}
double[,] equation = {{-10,0},{-9,0},{-8,0},
{-7,0},{-6,0},{-5,0},
{-4,0},{-3,0},{-2,0},
{-1,0},{0,0},{1,0},
{2,0},{3,0},{4,0},
{5,0},{6,0},{7,0},
{8,0},{9,0},{10,0}};
List<XY> xy = new List<XY>();
for (int i = 0; i < 21; i++)
{
equation[i, 1] = (-1 * (W_n_1[0, 1] * equation[i, 0] + W_n_1[0, 2])) / W_n_1[0, 0];
xy.Add(new XY(equation[i,0], equation[i,1]));
}
chart1.DataSource = xy;
chart1.Series[0].XValueMember = "Y";
chart1.Series[0].YValueMembers = "X";
chart1.ChartAreas["ChartArea1"].AxisX.Interval = 1;
chart1.DataBind();
}
}
public class XY
{
private double X;
private double Y;
public double DayOfWeek
{
get { return X; }
set { X = value; }
}
public double Sales
{
get { return Y; }
set { Y = value; }
}
public XY(double X, double Y)
{
this.X = X;
this.Y = Y;
}
}
}
In your XY class, instead of the private fields:
private double X;
private double Y;
use public properties:
public double X { get; private set; }
public double Y { get; private set; }
DataBinding does not work on fields.

Categories

Resources