Backtracking algorithm times out for difficult Sudoku puzzles - c#

This is my code for the back tracking logic.
private void backtrackLogic()
{
if (m == 0 && n != 0)
{
n--;
m = 8;
if(sudokuArray[n, m] == 0)
{
counter = copyArray[n, m];
copyArray[n, m] = 0;
if(counter == 9)
{
backtrackLogic();
}
}
else
{
backtrackLogic();
}
}
else if (m != 0)
{
m--;
if (sudokuArray[n, m] == 0)
{
counter = copyArray[n, m];
copyArray[n, m] = 0;
if (counter == 9)
{
backtrackLogic();
}
}
else
{
backtrackLogic();
}
}
else
{
Environment.Exit(0);
}
}
This is my code for the rest of the algorithm.
private void SolveButton_Click(object sender, RoutedEventArgs e)
{
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
copyArray[i, j] = sudokuArray[i, j];
Console.Write(sudokuArray[i, j]);
}
}
counter = 1;
bool noOptions = false;
for (n = 0; n < 9; n++)
{
for (m = 0; m < 9; m++)
{
for (counter = 1; counter < 10; counter++)
{
if (sudokuArray[n, m] == 0)
{
if (checkRow(n, m, counter) && checkColumn(n, m, counter) && checkBox(n, m, counter))
{
copyArray[n, m] = counter;
printTextBox(n, m, counter);
Console.Write(counter);
break;
}
else if (counter == 9)
{
noOptions = true;
}
}
if (noOptions)
{
backtrackLogic();
noOptions = false;
}
}
}
}
for (int n = 0; n < 9; n++)
{
for (int m = 0; m < 9; m++)
{
Console.Write(copyArray[n, m]);
}
}
}
When running this code on difficult Sudoku problems, the program times out because of how long it takes to execute. Any help in the execution of the algorithm would be very helpful!

Related

Calculate datagridview Cell Sepratly based on data c#

Datagridview calculate Amount each cell Differently based on data from database if addition then addition should be done if subtraction then subtraction to be done.
i have created this method but it affects all cell with answer. only the above cell should be calculated.
public void datagridviewone()
{
int addvalue = 0, subractvalue = 0;
ds = cs.Select("select ChargesProperty from FrieghtGridview");
list.Clear();
addition.Clear();
subtraction.Clear();
for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
{
if(ds.Tables[0].Rows[j][0].ToString() == "Answer")
{
list.Add(j);
}
if(ds.Tables[0].Rows[j][0].ToString() == "Plus")
{
addition.Add(j);
}
if(ds.Tables[0].Rows[j][0].ToString() == "Minus")
{
subtraction.Add(j);
}
}
if (list.Count > 0)
{
for(int j = 0; j < list.Count; j++)
{
if(addition.Count > 0)
{
for(int adval = 0; adval <= addition.Count-1; adval++)
{
if
(dataGridView1.Rows[addition[adval]].Cells[3].Value.ToString()
!= "")
{
addvalue +=
Convert.ToInt32(dataGridView1.Rows[addition[adval]].Cells[3].Value);
}
}
}
if (subtraction.Count > 0)
{
for (int adval = 0; adval <= subtraction.Count - 1;
adval++)
{
if
(dataGridView1.Rows[subtraction[adval]].Cells[3].Value.ToString() != "")
{
subractvalue +=
Convert.ToInt32(dataGridView1.Rows[subtraction[adval]].Cells[3].Value);
}
}
}
var finalans = addvalue - subractvalue;
dataGridView1.Rows[list[0]].Cells[3].Value = finalans;
addvalue = 0;
subractvalue = 0;
finalans = 0;
}
}
}

2d array to 1d array C#

I've got two algorithms converting random 2d arrays (m х n or m х m) to 1d array. I'm wondering if there is a way to make them work als in the opposite direction and convert the result to 1d array saving the order of numbers. Here is the full code of my program and a picture to see how both of my algorithms work.enter image description here
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using static System.Math;
namespace MyProgram
{
public class Program
{
public static void Main(string[] args)
{
Console.Write("Enter number of rows:");
int n = int.Parse(Console.ReadLine());
Console.Write("Enter number of columns:");
int m = int.Parse(Console.ReadLine());
int[] arr1 = new int[n * m];
int[,] arr2 = new int[n, m];
int choice;
do
{
Console.WriteLine("Select option:");
Console.WriteLine("\t1: Diagonal");
Console.WriteLine("\t2: Spiral");
Console.WriteLine("\t3: Exit");
Console.Write("Your selection: ");
choice = int.Parse(Console.ReadLine());
switch (choice)
{
case 1:
{
SetArray(arr2);
PrintArray(arr2);
Diagonal(arr2, arr1);
PrintArray(arr1);
break;
}
case 2:
{
SetArray(arr2);
PrintArray(arr2);
Spiral(arr2, arr1);
PrintArray(arr1);
break;
}
}
Console.WriteLine();
Console.WriteLine();
} while (choice != 5);
}
static void Diagonal(int[,] array2, int[] array1)
{
int k = 0;
int row = 0;
int col = 0;
while (k < array1.Length)
{
array1[k] = array2[row, col];
if ((row + col) % 2 == 0)
{
if ((row == 0) && (col != array2.GetLength(1) - 1)) { col++; }
else
{
if (col == array2.GetLength(1) - 1) { row++; }
else { row--; col++; }
}
}
else
{
if ((col == 0) && (row != array2.GetLength(0) - 1)) { row++; }
else
{
if (row == array2.GetLength(0) - 1) { col++; }
else { row++; col--; }
}
}
k += 1;
}
}
private static void Spiral(int[,] array2, int[] array1)
{
int lengthX = array2.GetLength(0);
int lengthY = array2.GetLength(1);
int Product = lengthX * lengthY;
int CorrectY = 0;
int CorrectX = 0;
int Count = 0;
while (lengthX > 0 && lengthY > 0)
{
for (int j = CorrectY; j < lengthY && Count < Product; j++)
{
array1[Count] = array2[CorrectX, j];
Count++ ;
}
CorrectX++;
for (int i = CorrectX; i < lengthX && Count < Product; i++)
{
array1[Count] = array2[i, lengthY - 1];
Count++ ;
}
if (lengthY > 0 && lengthX > 0) lengthY-- ;
else break;
for (int j = lengthY - 1; j >= CorrectY && Count < Product; j--)
{
array1[Count] = array2[lengthX - 1, j];
Count++ ;
}
if (lengthY > 0 && lengthX > 0) lengthX-- ;
else break;
for (int i = lengthX - 1; i >= CorrectX && Count < Product; i--)
{
array1[Count] = array2[i, CorrectY];
Count++ ;
}
CorrectY++;
}
}
public static void SetArray(int[,] arr)
{
Random r = new Random();
for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j = 0; j < arr.GetLength(1); j++)
{
arr[i, j] = r.Next(11, 99);
}
}
}
public static void SetArray(int[] arr)
{
Random r = new Random();
for (int i = 0; i < arr.Length; i++)
{
arr[i] = r.Next(11, 99);
}
}
public static void PrintArray(int[] arr)
{
Console.Write("print 1d array:");
for (int i = 0; i < arr.Length; i++)
{
Console.Write(arr[i] + " ");
}
Console.WriteLine();
}
public static void PrintArray(int[,] arr)
{
Console.WriteLine("print 2d array:");
for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j = 0; j < arr.GetLength(1); j++)
{
Console.Write(arr[i, j] + " ");
}
Console.WriteLine();
}
}
}
}
Actually what you are asking is quite easy. Just change your algorithm methods to receive int rows, int cols and delegate like this
delegate void Apply(int row, int col, int index);
Then replace arr2.GetLength(0) with rows, arr2.GetLength(1) with cols, and array element assignments with delegate call.
Here is the updated Diagonal method (you can do the same with the other):
static void Diagonal(int rows, int cols, Apply action)
{
int k = 0;
int row = 0;
int col = 0;
int length = rows * cols;
while (k < length)
{
action(row, col, k);
if ((row + col) % 2 == 0)
{
if ((row == 0) && (col != cols - 1)) { col++; }
else
{
if (col == cols - 1) { row++; }
else { row--; col++; }
}
}
else
{
if ((col == 0) && (row != rows - 1)) { row++; }
else
{
if (row == rows - 1) { col++; }
else { row++; col--; }
}
}
k += 1;
}
}
and the usage:
SetArray(arr2);
PrintArray(arr2);
Diagonal(n, m, (r, c, i) => arr1[i] = arr2[r, c]);
PrintArray(arr1);
// Inverse
var arr3 = new int[n, m];
Diagonal(n, m, (r, c, i) => arr3[r, c] = arr1[i]);
PrintArray(arr3);
This seems to be working for me for backward Diagonal:
static void BackwardDiagonal(int[,] array2, int[] array1) {
int k = 0;
int row = 0;
int col = 0;
while (k < array1.Length) {
array2[row, col] = array1[k]; // just swap sides of the assignment...
if ((row + col) % 2 == 0) {
if ((row == 0) && (col != array2.GetLength(1) - 1)) { col++; } else {
if (col == array2.GetLength(1) - 1) { row++; } else { row--; col++; }
}
} else {
if ((col == 0) && (row != array2.GetLength(0) - 1)) { row++; } else {
if (row == array2.GetLength(0) - 1) { col++; } else { row++; col--; }
}
}
k += 1;
}
}
For backward Spiral:
private static void BackwardSpiral(int[,] array2, int[] array1)
{
int lengthX = array2.GetLength(0);
int lengthY = array2.GetLength(1);
int Product = lengthX * lengthY;
int CorrectY = 0;
int CorrectX = 0;
int Count = 0;
while (lengthX > 0 && lengthY > 0)
{
for (int j = CorrectY; j < lengthY && Count < Product; j++)
{
array2[CorrectX, j] = array1[Count]; // just swap sides of the assignment...
Count++ ;
}
CorrectX++;
for (int i = CorrectX; i < lengthX && Count < Product; i++)
{
array2[i, lengthY - 1] = array1[Count];
Count++ ;
}
if (lengthY > 0 && lengthX > 0) lengthY-- ;
else break;
for (int j = lengthY - 1; j >= CorrectY && Count < Product; j--)
{
array2[lengthX - 1, j] = array1[Count];
Count++ ;
}
if (lengthY > 0 && lengthX > 0) lengthX-- ;
else break;
for (int i = lengthX - 1; i >= CorrectX && Count < Product; i--)
{
array2[i, CorrectY] = array1[Count];
Count++ ;
}
CorrectY++;
}
}
I also added this to the switch statement to implement it:
case 4:
{
SetArray(arr2);
PrintArray(arr2);
Diagonal(arr2, arr1);
PrintArray(arr1);
int[,] arr3 = new int[n, m]; // new blank array to fill with arr1
BackwardDiagonal(arr3, arr1); // fill arr3 from backward Diagonal algorithm
PrintArray(arr3);
break;
}
case 5:
{
SetArray(arr2);
PrintArray(arr2);
Spiral(arr2, arr1);
PrintArray(arr1);
int[,] arr3 = new int[n, m]; // new blank array to fill with arr1
BackwardSpiral(arr3, arr1); // fill arr3 from backward Spiral algorithm
PrintArray(arr3);
break;
}
While you're at it, also make sure to have } while (choice != 3); at the end of your do loop so that you can exit the program!

C# program for printing numbers triangle?

class Triangle
{
static void Main(string[] args)
{
int i,j,k,odd=1,size,s=0;
Console.Write("Enter the Size:");
size = Convert.ToInt32(Console.ReadLine());
int nofSpaces=size-1;
for (i = 1; i <= size; i++)
{
for (k = 1; k <= nofSpaces; k++)
{
Console.Write(" ");
}
for (j = 1; j <= odd; j++)
{
if (i >= j)
{
s = s + 1;
}
else
{
s = s - 1;
}
Console.Write(s);
}
Console.Write("\n");
odd = odd + 2;
nofSpaces = nofSpaces - 1;
}
Console.ReadKey();
}
}
This is the code and it gives the following result:
1
232
34543
4567654
56789875
But I need the result like this:
1
121
12321
1234321
---------
Any help would be greatly appreciated. Thank you.
Add
s = 0;
at the right line in your code.
class Triangle
{
static void Main(string[] args)
{
int i,j,k,odd=1,size;
Console.Write("Enter the Size:");
size = Convert.ToInt32(Console.ReadLine());
int nofSpaces=size-1;
int s = 0;
for (i = 1; i <= size; i++)
{
int g = 0;
for (k = 1; k <= nofSpaces; k++)
{
Console.Write(" ");
}
for (j = 1; j <= odd; j++)
{
if (i >= j)
{
Console.Write(j);
g = j;
}
else
{
//for (int n = j-1; n >= i; n--)
//{
// Console.Write(n - 1);
//}
Console.Write(--g);
}
}
Console.Write("\n");
odd = odd + 2;
nofSpaces = nofSpaces - 1;
}
Console.ReadKey();
}
}
}
Thank you All!... The above code is working....
You should try this...
This is a simple example with for loops
class Triangle
{
static void Main(string[] args)
{
int size;
Console.Write("Enter the Size:");
size = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < size; i++)
{
for (int j = size ; j > i; j--)
{
Console.Write(" ");
}
for (int x = 1; x <= i; x++)
{
Console.Write(x);
}
for (int j = i-1; j > 0; j--)
{
Console.Write(j);
}
Console.WriteLine();
}
Console.ReadKey();
}
}
Please write as below
static void Main(string[] args)
{
int i, j, k, odd = 1, size, s = 0;
Console.Write("Enter the Size:");
size = Convert.ToInt32(Console.ReadLine());
int nofSpaces = size - 1;
for (i = 1; i <= size; i++)
{
for (k = 1; k <= nofSpaces; k++)
{
Console.Write(" ");
}
s = 0;
for (j = 1; j <= odd; j++)
{
if (i >= j)
{
s = s + 1;
}
else
{
s = s - 1;
}
Console.Write(s);
}
Console.Write("\n");
odd = odd + 2;
nofSpaces = nofSpaces - 1;
}
Console.ReadKey();
}

My two dimensional array does not want to accept all my data

I am giving an array a size by what is contained in the text file. So when I look at my array it shows [4,7]. But when I read data into the array it only goes up until [1,6].
Take note that the last name is a position of the data in the array, the other entries then shows a ? sign and a no-entry sign on the right.
What is wrong with the below code?
static void buildArray()
{
int rowCount = 0;
int colCount = 0;
int placeHolder = 0;
double devideNumber = 0;
int tempNumber = 0;
string typeOptimum;
string[] objValue;
string[] constraintValues;
List<string> testNumbers;
List<double> ratioList;
List<double> rhs;
foreach (string item in constraintsList)
{
if (rowCount > 0)
{
colCount++;
}
rowCount++;
}
rowCount = colCount + 1;
colCount = colCount + 4;
string[,] arrayConstraints = new string[8, 9];
//Console.WriteLine("row {0} col {1}", colCount+1, colCount+4);
for (int i = 0; i < rowCount; i++)
{
for (int j = 0; j < colCount; j++)
{
arrayConstraints[i, j] = "0";
}
}
arrayConstraints[0, 0] = "1";
objValue = constraintsList[0].Split(' ');
typeOptimum = objValue[0].ToUpper();
arrayConstraints[0, 1] = (int.Parse(objValue[1]) * -1).ToString();
arrayConstraints[0, 2] = (int.Parse(objValue[2]) * -1).ToString();
for (int i = 1; i < rowCount; i++)
{
constraintValues = constraintsList[i].Split(' ');
arrayConstraints[i, 1] = constraintValues[0];
arrayConstraints[i, 2] = constraintValues[1];
arrayConstraints[i, i + 2] = "1";
arrayConstraints[i, colCount - 1] = constraintValues[3];
}
//for (int i = 0; i < rowCount; i++)
//{
// Console.WriteLine(" ");
// for (int j = 0; j < colCount; j++)
// {
// Console.Write(arrayConstraints[i, j] + " ");
// }
//}
do
{
//Console.WriteLine(testNumbers[entryPosition]);
//Console.WriteLine(arrayConstraints[0,entryPosition+1]);
//Console.WriteLine(entryPosition+1);
testNumbers = new List<string>();
for (int i = 1; i < colCount - 1; i++)
{
testNumbers.Add(arrayConstraints[0, i]);
}
ratioList = new List<double>();
rhs = new List<double>();
for (int i = 1; i < rowCount; i++)
{
ratioList.Add(double.Parse(arrayConstraints[i, entryPosition + 1]));
rhs.Add(double.Parse(arrayConstraints[i, colCount - 1]));
}
placeHolder = findRatioTest(ratioList, rhs);
#region multiplyArray
for (int i = 0; i < rowCount; i++)
{
if (i == placeHolder)
{
devideNumber = double.Parse(arrayConstraints[entryPosition + 1, placeHolder]);
for (int j = 0; j < colCount; j++)
{
tempNumber = int.Parse(arrayConstraints[placeHolder, j]);
arrayConstraints[placeHolder, j] = (tempNumber / devideNumber).ToString();
}
}
else
{
for (int k = 0; k < colCount; k++)
{
arrayConstraints[i, k] = (double.Parse(arrayConstraints[i, k]) - (double.Parse(arrayConstraints[i, entryPosition + 1])) * double.Parse(arrayConstraints[placeHolder, k])).ToString();
}
}
}
#endregion
foreach (string item in arrayConstraints)
{
Console.WriteLine(item);
}
} while (findNumber(typeOptimum, testNumbers) == true);
//while (findNumber(typeOptimum, testNumbers) == true)
//{
// testNumbers.Clear();
// for (int i = 1; i < colCount - 1; i++)
// {
// testNumbers.Add(arrayConstraints[0, i]);
// }
//}
}
static Boolean findNumber(string type, List<string> listNumbers)
{
Boolean found = false;
int tempInt, count = 0;
tempInt = int.Parse(listNumbers[0]);
if (type == "MIN")
{
#region MIN
foreach (string item in listNumbers)
{
count++;
if (int.Parse(item) > 0 || int.Parse(item)> tempInt)
{
entryPosition = count - 1;
tempInt = int.Parse(item);
found = true;
}
}
#endregion
}
else
{
#region MAX
foreach (string item in listNumbers)
{
count++;
if (int.Parse(item) < 0 || int.Parse(item) < tempInt)
{
entryPosition = count - 1;
tempInt = int.Parse(item);
found = true;
}
}
#endregion
}
return (found);
}
static int findRatioTest(List<double> listRatio, List<double> rhs)
{
int placeHolder = 0;
List<double> ratioTest = new List<double>();
int count = 0;
double tempSmall;
int tempPlace = 0;
foreach (double item in listRatio)
{
try
{
ratioTest.Add(rhs[count]/ item);
}
catch (Exception)
{
ratioTest.Add(double.Parse("-1"));
throw;
}
count++;
}
count = 0;
tempSmall = ratioTest[0];
foreach (double item in ratioTest)
{
if (item != 0 && item > 0)
{
if (item < tempSmall)
{
tempSmall = item;
tempPlace = count;
}
}
count++;
}
placeHolder = tempPlace + 1;
ratioTest.Clear();
return (placeHolder);
}

Declaring of arrays in C#

I have written the following code in an effort to try and compute the values down there below, but all my arrays do not work; especially the ones in the for loops. Can someone help teach me how to declare an array inside a loop? They keep showing errors like "Did you miss declaring a new object?"
Thanks
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
public class seasonal
{
public float mTotal;
public float movingAverage;
public int y;
public char quarter;
public char ssq;
public int rank;
public float forecast;
public float centralMovingAverage;
public float cmTotal;
public float sSeasonal;
}
public static int i;
public static int j;
public static int k = 0;
public static int n;
static void Main(string[] args)
{
int x; int r; int m; int c; int u = 0;
seasonal temp = new seasonal();
int n1; int n2; int n3; int n4; int sumr1 = 0; int sumr2 = 0; int sumr3 = 0; int sumr4 = 0;
float h; float ss; float sum; float sums1 = 0; float sums2 = 0; float sums3 = 0; float sums4 = 0; float tsums;
Console.WriteLine("Enter the no. of observations");
string nObservations = Console.ReadLine();
n = Convert.ToInt32(nObservations);
seasonal[] seasonal = new seasonal[n];
seasonal[] s = new seasonal[n];
for (i = 0; i < n; i++)
{
Console.Write("{0:D}:", (i+1) );
string value = Console.ReadLine();
int observation = Convert.ToInt32(value);
seasonal thisSeasonal = new seasonal();
thisSeasonal.y = observation;
seasonal[i] = thisSeasonal;
if (i>=0 && i<3)
{
seasonal[i].quarter = '1';
}
if (i>=3 && i<6)
{
seasonal[i].quarter = '2';
}
if (i>=6 && i<9)
{
seasonal[i].quarter = '3';
}
if (i>=9 && i<12)
{
seasonal[i].quarter = '4';
}
if (i>12)
{
r = i % 12;
if (r>=0 && r<3)
{
seasonal[i].quarter = '1';
}
if (r>=3 && r<6)
{
seasonal[i].quarter = '2';
}
if (r>=6 && r<9)
{
seasonal[i].quarter = '3';
}
if (r>=9 && r<12)
{
seasonal[i].quarter = '4';
}
}
for (i = k; i < n-3; i++)
{
sum = 0;
for (j = u+k; j < 4+k; j++)
{
sum += seasonal[j].y;
seasonal[i].mTotal = sum;
seasonal[i].movingAverage = seasonal[i].mTotal / 4;
Console.Write("{0:f}", seasonal[i].movingAverage);
k++;
}
}
for ( i = 0; i < (n-4); i++)
{
ss = 0;
for (j = 0; j < (2+i); j++)
{
ss += seasonal[j].movingAverage;
}
seasonal[i].cmTotal = ss;
seasonal[i].centralMovingAverage = seasonal[i].cmTotal / 2;
seasonal[i].sSeasonal = (seasonal[i+2].y)/(seasonal[i].centralMovingAverage);
if (i == 0 || i % 4 == 0)
{
seasonal[i].ssq = '3';
}
if (i == 1 || i % 4 == 1)
{
seasonal[i].ssq = '4';
}
if (i == 2 || i % 4 == 2)
{
seasonal[i].ssq = '1';
}
if (i == 3 || i % 4 == 3)
{
seasonal[i].ssq = '2';
}
Console.Write("\n{0:f}", seasonal[i].centralMovingAverage);
Console.Write("\n {0:f}", seasonal[i].sSeasonal);
}
}
for (m= 0; m < n; m++)
{
s[m] = seasonal[m];
}
for ( i = 0; i < (n-5); i++)
{
for ( j = 0; j < (n-4); j++)
{
if (s[i].sSeasonal > s[j].sSeasonal)
{
temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
}
for ( k = 0; k < (n-4); k++)
{
s[k].rank = k + 1;
Console.Write("\n\t {0:D}", s[k].rank);
}
for ( i = 0; i < (n-4); i++)
{
if (s[i].ssq == '1')
{
sumr1 += s[i].rank;
sums1 += s[i].sSeasonal;
//n1 ++;
}
if (s[i].ssq == '2')
{
sumr2 += s[i].rank;
sums2 += s[i].sSeasonal;
//n2++;
}
if (s[i].ssq == '3')
{
sumr3 += s[i].rank;
sums3 += s[i].sSeasonal;
//n3++;
}
if (s[i].ssq == '4')
{
sumr4 += s[i].rank;
sums4 += s[i].sSeasonal;
//n4++;
}
}
tsums = ((sums1/4)+(sums2/4)+(sums3/4)+(sums4/4));
Console.Write("\n\n\n{0:f}",tsums);
Console.Write("\n\n\n\n\n{0:D}",sumr1);
Console.Write("\n\n\n\n{0:D}",sumr2);
Console.Write("\n\n\n\n{0:D}",sumr3);
Console.Write("\n\n\n\n\n{0:D}",sumr4);
Console.Write("\n{0:f}",sums1/4);
Console.Write("\n\n{0:f}",sums2/4);
Console.Write("\n\n{0:f}",sums3/4);
Console.Write("\n\n{0:f}",sums4/4);
Console.Write("\n{0:f}",((sums1/4)/tsums)*4);
Console.Write("\n\n{0:f}",((sums2/4)/tsums)*4);
Console.Write("\n\n{0:f}",((sums3/4)/tsums)*4);
Console.Write("\n\n{0:f}",((sums4/4)/tsums)*4);
}
}
}
You need to initialise the objects in your arrays:
Seasonal[] seasonal = new Seasonal[n];
for (int l = 0; l < seasonal.Length; l++)
{
seasonal[l] = new Seasonal();
}
Seasonal[] s = new Seasonal[n];
for (int l = 0; l < s.Length; l++)
{
s[l] = new Seasonal();
}
This only solves the initialisation problem, though. You may want to look at naming conventions for readability, and then the index off by 1 you'll experience at roughly line 105.
instead of working with
seasonal[] seasonal = new seasonal[n];
seasonal[] s = new seasonal[n];
do work with
seasonal[] s1 = new seasonal[n];
seasonal[] s2 = new seasonal[n];
But when I see code like, this, where you just copy your array:
for (m= 0; m < n; m++)
{
s[m] = seasonal[m];
}
why would you do that? copy the entire array instead of every single entry..
why do you not use any c# constructs?
The problem is this line:
sum += seasonal[j].y;
But there isn't a simple fix. You are creating each object individually through the loop instead of before you enter the loop so each iteration is looking at null objects. Also, the loop this line is in reads past the end of the array. The code is a bit complex to easily see what you're trying to do and how to fix it.
Just an example for to simplify some of your code:
you wrote the following:
if (i>=0 && i<3)
{
seasonal[i].quarter = '1';
}
if (i>=3 && i<6)
{
seasonal[i].quarter = '2';
}
if (i>=6 && i<9)
{
seasonal[i].quarter = '3';
}
if (i>=9 && i<12)
{
seasonal[i].quarter = '4';
}
if (i>12)
{
r = i % 12;
if (r>=0 && r<3)
{
seasonal[i].quarter = '1';
}
if (r>=3 && r<6)
{
seasonal[i].quarter = '2';
}
if (r>=6 && r<9)
{
seasonal[i].quarter = '3';
}
if (r>=9 && r<12)
{
seasonal[i].quarter = '4';
}
}
you could write that instead:
if(i >= 0)
seasonal[i].quarter = (((i % 12)/3) + 1).ToString();
I don' think this code
seasonal[] seasonal = new seasonal[n];
seasonal[] s = new seasonal[n];
is correct. Try
seasonal[] seas = (seasonal[])Array.CreateInstance(typeof(seasonal), n);

Categories

Resources