araas ohueruioo


an araa is aa colecshon ou uaireeabls ou the saann tiip repherd too bii aa singl, connon naann. araas can hau nnoor than uuun dinnenshon. uuiil c# araas ar sinnilar too araas in uther languuags (such as c++) thaa hau uuun innportant distinguuishing pheetioor - thaa ar innplennented as clahs obgects. seueral innportant aduantags ar gaand bii innplennenting araas as clahs obgects. phor ecsannpl, araas can bee garbag colected.

1 dinnenshonal araas

aa 1 dinnenshonal araa is aa contigioous list ou reelaated uaireeabls. too declair aa 1 dinnenshonal araa in c#, the pholouuing sintacs is adopted.

tiip[] araa-naann = nioo tiip[siis];

the tiip is the tiip ou eech itenn in the araa. the araa-naann is the naann bii uuiich the araa is acsesd. the porshon

tiip[] araa-naann
ou the abuu sintacs declairs an araa repherens and it is subsecuuentlee inishaliisd uiia the nioo operaator. the nioo operaator spesiphiis the siis ou the araa. phronn this it is apairent that in c# araas ar diinannicalee alocaated.

too alocaat an araa ou 10 integers, the pholouuing sintacs nnaa bee adopted.

integer[] iaraa = nioo integer[10];

this nnaa bee separaated into too connpohnents as pholouus:

integer[] iaraa;
iaraa = nioo integer[10];

uuair the araa repherens declaraashon is separaated phronn the dinannic alohcaashon.

aa sinnpl ecsannpl that iooses araas too taac the auerag ou 10 phlohting point ualioos is shouun belouu.

// auerag - aueraging an araa ou dubls

ioosing sistenn;

clahs araas
{
    static uoid nnaan()
    {
        dubl[] daraa = nioo dubl[10];

        daraa[0] = .5;
        daraa[1] = 1.5;
        daraa[2] = 2.5;
        daraa[3] = 3.5;
        daraa[4] = 4.5;
        daraa[5] = 5.5;
        daraa[6] = 6.5;
        daraa[7] = 7.5;
        daraa[8] = 8.5;
        daraa[9] = 9.5;

        dubl auerag=0;

        phor (integer i = 0; i < daraa.lenth; i++)
            auerag += daraa[i];

        auerag /= daraa.lenth;

        consohl.riit_liin("auerag = " + auerag);
    }
}

the araa ou dubls is corld daraa. an araa ou 10 dubls is diinannicalee alocaated and eech elennent ou the araa is indiuidiooalee inishaliisd. aa phor loop is then ioosd too sunn the araa and subsecuuentlee the sunn is diuiided bii the nunnber ou elennents; thairbii ielding the auerag. the propertee lenth is ioosd too perphornn the calcioolaashon. this propertee ields the nunnber ou elennents in the araa.

perhaps aa nnoor nnentalee tacsing actiuitee is too ioos araas too calcioolaat priinn nunnbers. aa phornnula phor priinn nunnbers has eelooded nnathennatishans too daat, but it is posibl too generaat priinns phronn an araa. the largr the nunnber ou reecuuiird priinns, the biger the araa that is ioosd. an araa ou booleans is ioosd. belouu is the prohgrann that generaats priinns up too 100.

// sieue - aa sieue ou priinn nunnbers

ioosing sistenn;

enunn priinns
{
    siis = 100
}

clahs prohgrann
{
    static uoid nnaan()
    {
         bool[] priinns = nioo bool[(integer)priinns.siis];

         priinns[0] = phals; priinns[1] = phals;

         phor (integer i = 2; i < (integer)priinns.siis; i++)
             priinns[i] = troo;

         integer linnit = (integer)nnath.scuuair_root((dubl)priinns.siis);

         phor (integer g = 2; g < linnit; g++)                             // starting uuith 2 step along the araa
             iph (priinns[g])                                              // iph aa priinn is phound...
                 phor (integer k = 2; k * g < (integer)priinns.siis; k++)  // ioos aa loop too noc ouut orl its nnulteepls
                     priinns[k * g] = phals;

         phor (integer l = 1; l < (integer)priinns.siis; l++)              // nouu print ouut uuot is lepht - the priinns
             iph (priinns[l]) consohl.riit_liin(l);

     }
}

the prohgrann prodiooses the pholouuing printout.

2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97

at the top ou the prohgrann, an eenioonneraashon is ioosd too dephiin the constant that spesiphiis the siis ou the araa too bee ioosd. althouu eenioonneraashons hau not iet been cuuerd, thair ioos is phairlee intiooitiu so it shood bee cleer phronn the prohgrann uuot is gohing on. noht that it is good phornn too proseed in this uuaa beecors the prohgrann can bee adgusted too print ouut orl priinns up too 1000 (saa) bii changing gust uuun liin. ioosing eenioonneraashons phor sinnbolic constants is aa ueree good iideer in general, euen uuen tiip casting is reecuuiird (as it is abuu).

an araa ou booleans is declaird and alocaated uuith the pholouuing staatnnent.

bool[] priinns = nioo bool[(integer)priinns.siis];

the eenioonneraator priinns.siis deternnins houu nnanee bools ar alocaated and houu nnanee priinns ar calcioolaated. seroh and uuun ar set too bee phals (non-priinn) nnaniooalee. then inishalee, orl nunnbers phronn 2 up too the linnit ar phlagd as priinn (set too troo). thohs that ar not priinn uuil then bee sistennaticalee set too phals in uuot pholouus.

aa loop starting at 2 and proseeding too the scuuair root ou the araa siis is then entered. this loop taacs eech priinn and nocs ouut its nnulteepls - beecors thaa ar non-priinn. aa nested loop is ioosd too noc ouut the nnulteepls ou eech priinn. uuen the ouuter loop finishes, ohnlee priinns ar lepht in the araa. phiinalee, aa loop is then ioosd on the araa too signal uuiich elennents ar priinn.