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.
Related
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
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++;
}
}
Attempting the hackerRank Q from https://www.hackerrank.com/challenges/2d-array
static void Main(String[] args)
{
int[][] arr = new int[6][];
for (int arr_i = 0; arr_i < 6; arr_i++)
{
string[] arr_temp = Console.ReadLine().Split(' ');
arr[arr_i] = Array.ConvertAll(arr_temp, Int32.Parse);
}
int[] sum = new int[6];
List<int> n = new List<int>();
for (int i = 0; i + 2 < arr.Length; i++)
{
for (int j = 0; j + 2 < arr.GetLength(0); j++)
{
sum[j] = arr[i][j] + arr[i][j + 1] + arr[i][j + 2] +
arr[i + 1][j + 1] +
arr[i + 2][j] + arr[i + 2][j + 1] + arr[i + 2][j + 2];
n.Add(sum[j]);
}
}
Console.WriteLine(n.Max());
}
}
}
If I run this program and enter the following as contents of 'arr'
111111
222222
333333
444444
555555
666666
Here I am trying to add 1 + 1 + 1
+ 2 +
3 + 3 + 3
using sum[j] = arr[i][j] + arr[i][j + 1] + arr[i][j + 2] +
arr[i + 1][j + 1] +
arr[i + 2][j] + arr[i + 2][j + 1] + arr[i + 2][j + 2];
but arr[i][j] returns 111111
how can I access 1? is this the right answer to the question on hackerrank?
Hope below code helps you
public class Solution {
private static final int _MAX = 6; // size of matrix
private static final int _OFFSET = 2; // hourglass width
private static int matrix[][] = new int[_MAX][_MAX];
private static int maxHourglass = -63; // initialize to lowest possible sum (-9 x 7)
/** Given a starting index for an hourglass, sets maxHourglass
* #param i row
* #param j column
**/
private static void hourglass(int i, int j){
int tmp = 0; // current hourglass sum
// sum top 3 and bottom 3 elements
for(int k = j; k <= j + _OFFSET; k++){
tmp += matrix[i][k];
tmp += matrix[i + _OFFSET][k];
}
// sum middle element
tmp += matrix[i + 1][j + 1];
if(maxHourglass < tmp){
maxHourglass = tmp;
}
}
public static void main(String[] args) {
// read inputs
Scanner scan = new Scanner(System.in);
for(int i=0; i < _MAX; i++){
for(int j=0; j < _MAX; j++){
matrix[i][j] = scan.nextInt();
}
}
scan.close();
// find maximum hourglass
for(int i=0; i < _MAX - _OFFSET; i++){
for(int j=0; j < _MAX - _OFFSET; j++){
hourglass(i, j);
}
}
// print maximum hourglass
System.out.println(maxHourglass);
}
}
I'm writing a little library and want to be able to sum an array.
A singlethread version works fine, but when I add multithreading, everything breaks.
I'm using a partitioner to split data on blocks and then sum every part in single result. Then I return it. But data is invalid, it's doesn't seems that there is any race condition, because every program reboot leads to same results.
[Pure]
public static double Sum(this double[] source)
{
source.IsNotNull("source");
if (source.Length < Constants.SingleThreadExecutionThreshold)
return Sum(source, 0, source.Length);
double result = 0;
object syncRoot = new object();
Parallel.ForEach(Partitioner.Create(0, source.Length),
() => (double)0,
(range, state, sum) => Sum(source, range.Item1, range.Item2),
x =>
{
lock (syncRoot)
result += x;
});
return result;
}
Sum(source, from, to) always give correct results. Here is an implementation:
[Pure]
private static double Sum(this double[] source, int startIndex, int endIndex)
{
double sum1 = 0, sum2 = 0, sum3 = 0, sum4 = 0;
checked
{
int i;
for (i = startIndex; i < endIndex - Constants.Step + 1; i += Constants.Step)
{
sum1 += source[i];
sum2 += source[i + 1];
sum3 += source[i + 2];
sum4 += source[i + 3];
}
if (i == source.Length)
return ((sum1 + sum2) + (sum3 + sum4));
if (i == source.Length - 1)
return ((sum1 + sum2) + (sum3 + sum4) + source[i]);
if (i == source.Length - 2)
return ((sum1 + sum2) + (sum3 + sum4) + (source[i] + source[i + 1]));
return ((sum1 + sum2) + (sum3 + sum4) + (source[i] + source[i + 1] + source[i + 2]));
}
}
internal static class Constants
{
public const int Step = 4;
public const int SingleThreadExecutionThreshold = 1024;
}
How can it be fixed?
Code example: http://ideone.com/8sD0JL
Okay, think I've fixed it. I've found two major bugs.
the array.Length thing
you were misusing the "finally" delegate. There is no guarantee that that code ever gets run
With these changes I get a difference of -0.000576496124267578, which is within expected for double sum rounding errors.
[Pure]
public static double Sum(this double[] source, int startIndex, int endIndex)
{
double sum1 = 0, sum2 = 0, sum3 = 0, sum4 = 0;
checked
{
int i;
int j = 0;
for (i = startIndex; i < endIndex - Constants.Step + 1; i += Constants.Step)
{
sum1 += source[i];
sum2 += source[i + 1];
sum3 += source[i + 2];
sum4 += source[i + 3];
j += Constants.Step;
}
var segmentLength = endIndex - startIndex;
if (j == segmentLength)
return ((sum1 + sum2) + (sum3 + sum4));
if (j == segmentLength - 1)
return ((sum1 + sum2) + (sum3 + sum4) + source[i]);
if (j == segmentLength - 2)
return ((sum1 + sum2) + (sum3 + sum4) + (source[i] + source[i + 1]));
return ((sum1 + sum2) + (sum3 + sum4) + (source[i] + source[i + 1] + source[i + 2]));
}
}
internal static class Constants
{
public const int Step = 4;
public const int SingleThreadExecutionThreshold = 1024;
}
[Pure]
public static double Sum(this double[] source)
{
if (source.Length < Constants.SingleThreadExecutionThreshold)
return Sum(source, 0, source.Length);
double result = 0;
object syncRoot = new object();
Parallel.ForEach(Partitioner.Create(0, source.Length),
(range) => {
var x = Sum(source, range.Item1, range.Item2);
lock (syncRoot)
result += x;
});
return result;
}
I am trying to use a bubble sort, to order numbers from least to greatest in c#, whats wrong with what i have so far?
private void Order_Click(object sender, EventArgs e) {
value1 = Convert.ToInt32(textBox1.Text);
value2 = Convert.ToInt32(textBox2.Text);
value3 = Convert.ToInt32(textBox3.Text);
value4 = Convert.ToInt32(textBox4.Text);
value5 = Convert.ToInt32(textBox5.Text);
int[] myarray = { value1, value2, value3, value4, value5 };
int n = 0;
bool swapped = true;
int j = 0;
int tmp;
while (swapped) {
swapped = false;
j++;
for (int i = 0; i < n - j; i++)
{
if (myarray[i] > myarray[i + 1]) {
tmp = myarray[i];
myarray[i] = myarray[i + 1];
myarray[i + 1] = tmp;
swapped = true;
order1.Text = Convert.ToString(myarray[0] + "," +
myarray[1] + "," +
myarray[2] + "," +
myarray[3] + "," +
myarray[4]);
}
}
}
}
First thing which is wrong with your code is:
for (int i = 0; i < n - j; i++)
Your check i < n - j will never let the control to fall through the loop. Because you have initialized n to 0 and j is 1 (after j++), so i is not going to be less then -1, Thus your loop will not work as intended. Since you set the swapped to false, the control will fall out of the while loop, hence no sorting.
You need to change your code to:
while (swapped)
{
swapped = false;
for (int i = 0; i < myarray.Length - 1; i++)
{
if (myarray[i] > myarray[i + 1])
{
int t = myarray[i];
myarray[i] = myarray[i + 1];
myarray[i + 1] = t;
swapped = true;
Console.WriteLine(Convert.ToString(myarray[0] + "," + myarray[1] + "," + myarray[2] + "," + myarray[3] + "," + myarray[4]));
}
}
}
the n and j variables seem unnessesary. leave n and j out of your program, and in your for loop, do this:
for (int i = 0; i < (array1.Length - 1); i++)
Bubble Sort
using System;
class AscendingBubbleSort
{
public static void Main()
{
int i = 0,j = 0,t = 0;
int []c=new int[20];
for(i=0;i<20;i++)
{
Console.WriteLine("Enter Value p[{0}]:", i);
c[i]=int.Parse(Console.ReadLine());
}
// Sorting: Bubble Sort
for(i=0;i<20;i++)
{
for(j=i+1;j<20;j++)
{
if(c[i]>c[j])
{
Console.WriteLine("c[{0}]={1}, c[{2}]={3}", i, c[i], j, c[j]);
t=c[i];
c[i]=c[j];
c[j]=t;
}
}
}
Console.WriteLine("Here comes the sorted array:");
// Print the contents of the sorted array
for(i=0;i<20;i++)
{
Console.WriteLine ("c[{0}]={1}", i, c[i]);
}
}
}
source