Debug.Log() A 2D array from a custom class to main class - c#

Here's my code.
CustomClass.cs
class ScoreBoard(){
private int m_lastCnt;
public ScoreBoard{
m_lastCnt = 0;
}
public void makeBoard(string history) {
string[] arrPartStr = history.Split(',');
int[] arrPart = new int[arrPartStr.Length];
for (int c = 0; c < arrPart.Length; c++)
{
int temp = 0;
if (arrPartStr[c][0] == 'P') temp = 100;
else if (arrPartStr[c][0] == 'B') temp = 200;
else temp = 300;
if (arrPartStr[c][1] == 'P') temp += 10;
if (arrPartStr[c][2] == 'P') temp += 1;
arrPart[c] = temp;
}
//var strTmp : String = strData;
//strTmp = "311|101|211|211|211|211|211|211|211|211|111|111|111|111|111|111|111|111|111"
//strTmp = strData.replace(/:/g,"");
int[,] arrTmp = new int[6100, 104];
}
}
Main Class i call the void method like this
ScoreBoard sb = ScoreBoard();
string history = "s ,o ,m ,e ,s ,t ,r ,i ,n ,g";
private void Start(){
sb.makeBoard(history);
}
How can i print my 2D array in my console?
I tried doing it like the for(int row){for(int col){}} but its not working i don't know why

Do you mean this?
for (int j = 0; j < arrTmp.GetLength(1); j++)
{
for (int i = 0; i < arrTmp.GetLength(0); i++)
{
var msg = "[" + i.ToString() + ", " + j.ToString() + "] = " + arrTmp[i, j].ToString();
Debug.Log(msg);
}
}

I got it to display:)
CustomClass.cs
int[,] arrTmp = new int[104, 6];
public int[,] GetBoard(){ return arrTmp }
MainClass.cs
int[,] arrayBigRoad = bsb.GetBoard();
for (int j = 0; j < arrTmp.GetLength(1); j++)
{
for (int i = 0; i < arrTmp.GetLength(0); i++)
{
var msg = "[" + i.ToString() + ", " + j.ToString() + "] = " + arrTmp[i, j].ToString();
Debug.Log(msg);
}
}
Thanks though Rodrigo . I'll mark yours as an answer

Related

TimeTable creation using graph colouring in ASP.net

I am working on an ASP application which is about creating time table (scheduling of classes) for my final semester project. I have used the logic from a basic console application program of graph colouring and incorporated into my project. Though the logic for a console application works as required, I am unable to do the same for my asp project.
try
{
int n = 10, i, j, max = 0, col, ct = 0, rt = 0, m, count = 2, g = 0;
int[][] a = new int[20][];
int[] c = new int[20];
int[] arr = new int[20];
for (int k = 0; k < 20; k++)
{
c[k] = 0;
arr[k] = 0;
a[k] = new int[20];
for (int l = 0; l < 20; l++)
{
a[k][l] = 0;
}
}
string num = null, num1 = "";
string display = "SELECT * FROM tbl_Subjects WHERE USN IN(SELECT USN FROM tbl_Student WHERE Semester='" + DropDownList1.Text + "')";
con.Open();
SqlCommand cmdSql = new SqlCommand(display, con);
SqlDataReader rs;
rs = cmdSql.ExecuteReader();
while (rs.Read())
{
//count1++;
for (int k = 1; k <= n; k++)
{
g = k - 1;
num = rs[k].ToString();
arr[g] = Convert.ToInt32(num);
num1 += arr[g] + " ";
}
num1 += "\n";
for (int k = 0; k < n; k++)
{
for (int l = (k + 1); l < n; l++)
{
if (arr[k] == 1 && arr[l] == 1)
{
a[k][l] = 1;
//a[l][k] = 1;
}
}
}
}
rs.Close();
TextBox3.Text = num1;
num1 = "";
for (int k = 0; k < n; k++)
{
for (int l = 0; l < n; l++)
{
num1 += a[k][l] + " ";
}
num1 += "\n";
}
for (i = 0; i < n; i++)
{
c[i] = 0;
//for(j=0;j<n;j++)
//scanf("%d",&a[i][j]);
}
c[0] = 1; c[1] = 2;
for (i = 2; i < n; i++)
{
for (j = 0; j < n; j++)
if (a[i][j] > 0)
{
m = 0;
for (col = 0; col < n; col++)
{ if (a[i][col] > 0)rt++; if (a[col][i] > 0)ct++; }
m = rt; if (ct > rt) m = ct;
if (m < 2) { if (a[0][i] > 0)c[i] = 2; else c[i] = 1; } else { c[i] = count; if (m > max) { max = m; count++; } }
rt = 0; ct = 0;
} if (c[i] < 1) if (c[i - 1] > 1) c[i] = 1; else c[i] = 2;
}
string result = "";
for (i = 0; i < n; i++)
{
result += "Subject[" + (i + 1) + "] = " + c[i] + "\n";
if (c[i]==1)
{
TextBox4.Text += "Subject 1 = 8:15 to 9:15 \n";
}
if (c[i] == 2)
{
TextBox4.Text += "Subject 2 = 9:15 to 10:15 \n";
}
if (c[i] == 3)
{
TextBox4.Text += "Subject 3 = 10:45 to 11:45 \n";
}
if (c[i] == 4)
{
TextBox4.Text += "Subject 4 = 11:45 to 12:45 \n";
}
if (c[i] == 5)
{
TextBox4.Text += "Subject 5 = 1:30 to 2:30 \n";
}
if (c[i] == 6)
{
TextBox4.Text += "Subject 6 = 2:30 to 3:30 \n";
}
if (c[i] == 7)
{
TextBox4.Text += "Lab 1 = 8-15 to 10:15 \n";
}
if (c[i] == 8)
{
TextBox4.Text += "Lab 2 = 10:45 to 12:45 \n";
}
if (c[i] == 9)
{
TextBox4.Text += "Lab 3 = 1:30 to 3:30 \n";
}
if (c[i] == 10)
{
TextBox4.Text += "Lab 4 = 8-15 to 9:15 \n";
}
}
con.Close();
TextBox2.Text = result;
}
catch (Exception ex)
{
Label2.Text = ex.Message;
}
In the above code, I'm reading the data i.e 0's and 1's of the adjacency matrix from the database onto which the graph colouring algorithm is applied. Once the colouring has been performed, results are printed on a textbox for time being.
The problem I am facing is, the colors are not being assigned and not able to generate the time table.

c# line chart. Why does my line go to 0 instead of just staying at the last point?

So i made a button and when you press it displays a graph of the arrays, but it goes to 0 when its finished.
private void button14_Click(object sender, EventArgs e)
{
for (int i = 0; i <= period; i++)
{
xos[i] = i+1;
yos[i] = pot[i];
listBox1.Items.Add(xos[i]);
listBox1.Items.Add("y " + yos[i]);
}
for (int i = 0; i <= period; i++)
{
x2os[i] = i + 1;
y2os[i] = pot2[i];
listBox2.Items.Add(x2os[i]);
listBox2.Items.Add("y "+y2os[i]);
}
chart1.ChartAreas[0].AxisX.Minimum = 0;
chart1.Series["Potražnja"].Points.DataBindXY(xos, yos);
chart1.Series["Predviđanje"].Points.DataBindXY(x2os,y2os);
}
Here is a photo of chart
Don't create the arrays before you know how long they need to be, just declare the references:
public double[] xos;
public double[] yos;
public double[] y2os;
public double[] x2os;
Then when you use them, create the actual arrays:
private void button14_Click(object sender, EventArgs e)
{
xos = new double[period + 1];
yos = new double[period + 1];
x2os = new double[period + 1];
y2os = new double[period + 1];
for (int i = 0; i <= period; i++)
{
xos[i] = i+1;
yos[i] = pot[i];
listBox1.Items.Add(xos[i]);
listBox1.Items.Add("y " + yos[i]);
}
for (int i = 0; i <= period; i++)
{
x2os[i] = i + 1;
y2os[i] = pot2[i];
listBox2.Items.Add(x2os[i]);
listBox2.Items.Add("y "+y2os[i]);
}
chart1.ChartAreas[0].AxisX.Minimum = 0;
chart1.Series["Potražnja"].Points.DataBindXY(xos, yos);
chart1.Series["Predviđanje"].Points.DataBindXY(x2os,y2os);
}
To make the yellow line skip the first zero values, you would need to check for those first, and then create the arrays:
var start = 0;
while (start <= period && pot2[start] == 0) {
start++;
}
x2os = new double[period - start + 1];
y2os = new double[period - start + 1];
for (int i = 0; i <= period - start; i++)
{
x2os[i] = start + i + 1;
y2os[i] = pot2[start + i];
listBox2.Items.Add(x2os[i]);
listBox2.Items.Add("y "+y2os[i]);
}

Binary Sorting logic: not sorting numbers correctly

I am currently trying to implement binary sort logic. I start by generating random numbers. Then create a copy of that array which will be then sorted out with the Binary Sort method. The problem is that the sort is not working properly. I am not sure if my comparisons are being done correctly. Any ideas why is not sorting properly?
namespace binarySort
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Stopwatch sw = new Stopwatch();
Random r = new Random();
OpenFileDialog open1 = new OpenFileDialog();
long operations = 0;
int size;
int max;
int[] createArray;
int[] sortArray;
int[] copyArray;
public void RandomNumber()
{
size = Convert.ToInt32(textBoxSize.Text);
max = Convert.ToInt32(textBoxMax.Text);
createArray = new int[size];
copyArray = new int[size];
sortArray = new int[size];
for (int i = 0; i < size; i++)
{
createArray[i] = r.Next(1, max);
}
textBoxResults.AppendText("-------------------------------------------------------------------------------" + Environment.NewLine + "Random" + Environment.NewLine + Environment.NewLine);
DisplayArrays();
}
public void BinarySort()
{
operations = 0;
sw.Reset();
sw.Start();
int low = 0;
int high = 0;
int temp = 0;
int mid = 0;
for (int i = 0; i < size; i++)
{
copyArray[i] = createArray[i];
}
for (int i = 1; i < size; i++)
{
high = i - 1;
temp = copyArray[i];
while (low <= high)
{
operations++;
mid = (low + high) / 2;
if (temp < copyArray[mid])
{
high = mid - 1;
}
else
{
low = mid + 1;
}
}
operations++;
for (int j = i - 1; j >= low; j--)
{
copyArray[j + 1] = copyArray[j];
}
copyArray[low] = temp;
}
for (int i = 0; i < size; i++)
{
sortArray[i] = copyArray[i];
}
textBoxResults.AppendText("-------------------------------------------------------------------------------" + Environment.NewLine + "Binary Insertion" + Environment.NewLine + Environment.NewLine);
DisplaySorted();
}
private void buttonSortArray_Click(object sender, EventArgs e)
{
BinarySort();
}
}
}
For example:
EDIT: Sorry, your original code is OK, you just need to add this line low = 0; after high = i - 1; so it should be
for (int i = 1; i < size; i++)
{
high = i - 1;
low = 0;
temp = copyArray[i];

Need help to create a c# bubblesort. Whats wrong with what i have so far?

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

C# How to exclude an int from an array?

So I've got an array of integer. I want to use a loop and exclude integers that makes the equation true. So that would be like
for (int n = 0; n < first9char.Length; n++ ) {
if (first9char[n] == clickValue) {
first9char[n] = first9char[n + 1];
But then it only changes the value that is equal to not changing whole array. So is there any way to do this?
I want to use it in this loop.
if (UserSquareMethod.clickedBoxes[0] == -1) {
MessageBox.Show("You have not selected any box");
} else {
int i = 0;
do {
if (textButtonArray[clickedBox[i]].Text == "" || clickValue == "") {
textButtonArray[clickedBox[i]].Text = clickValue;
textButtonArray[clickedBox[i]].Font = new Font(textButtonArray[clickedBox[i]].Font.FontFamily, 14, FontStyle.Bold);
}
else
{
textButtonArray[clickedBox[i]].Text += "," + clickValue;
textButtonArray[clickedBox[i]].Font = new Font(textButtonArray[clickedBox[i]].Font.FontFamily, 5, FontStyle.Regular);
string[] first9char = textButtonArray[clickedBox[i]].Text.Split(new string[] { "," }, StringSplitOptions.None);
for (int j = 1; j < first9char.Length; j++)
{
for (int k = j - 1; k >= 0; k--)
{
if (first9char[j] == first9char[k])
{
if (clearNumberClicked == true)
{
first9char = Array.FindAll(first9char, x => x != clickValue);
label2.Text = first9char[0];
//int n = 0;
//for (int p = 0; p < first9char.Length; p++)
//{
// if (first9char[p] != clickValue)
// {
// first9char[n] = first9char[p];
// n++;
// label2.Text += "," + first9char[n];
// }
// }
//for (int n = 0; n < first9char.Length; n++ ) {
//if (first9char[n] == clickValue) {
// first9char[n] = first9char[n + 1];
// for ( int p = 0; p < n; p++) {
//}
//}
//}
MessageBox.Show("Clear the number" + first9char[(first9char.Length - 1)] + "and " + clickValue + " " + first9char.Length);
}
else {
first9char[j] = "";
textButtonArray[clickedBox[i]].Text = first9char[0];
MessageBox.Show("You cannot enter the same number again!"+ first9char[j]+j);
for (int m = 1; m < (first9char.Length - 1); m++) {
textButtonArray[clickedBox[i]].Text += ","+ first9char[m];
}
}
}
}
}
if (textButtonArray[clickedBox[i]].Text.Length > 9)
{
textButtonArray[clickedBox[i]].Text = first9char[0] + "," + first9char[1] + "," + first9char[2] + "," + first9char[3] + "," + first9char[4];
MessageBox.Show("You cannot enter more than 5 numbers, please clear the box if you want to enter different number." + textButtonArray[clickedBox[i]].Text.Length);
}
}
i++;
}
while (clickedBox[i] != -1);
}
}
I would use LINQ for this:
first9char = first9char.Where(x => x != clickValue).ToArray();
It just means "pick the items that don't match". If you can't use LINQ for some reason, then just keep another counter, and make sure to only loop to n from there on in:
int n = 0;
for(int i = 0; i < first9char.Length; i++) {
if(first9char[i] != clickValue) {
first9char[n] = first9char[i];
n++;
}
}
Clean and efficient.

Categories

Resources