tags:

views:

124

answers:

5

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

        }
    }
}
+6  A: 

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.

Darksider
A: 

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);
Raj
`new MyClass[numberOfItemsInArray]` is correct.
icktoofay
Hmm, probably still thinking with my VB.NET 2003 brain :-(
Raj
A: 

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?

SwissCoder
I copy single entries because I need to use each single entry on its own for a computation...where can I read about constructs? Do u have any good ebooks you can advise me to read?
akalmas
sorry I dont want to mislead you. I meant with constructs, the use of available syntax-elements like the foreach loop and such..
SwissCoder
A: 

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.

Adam Ruth
A: 

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(); 
SwissCoder