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);
}
}
}