[ EmmaR @ 03.11.2011. 23:12 ] @
Kako se radi sa višedimenzionalnim nizovima u C#? Kreiranje, dodela vrednosti ? Testerasti nizovi? Neki opšti primer?




[ Dejan Carić @ 04.11.2011. 00:02 ] @
http://msdn.microsoft.com/en-us/library/aa288453.aspx
http://msdn.microsoft.com/en-us/library/2yd9wwz4.aspx
http://www.functionx.com/csharp/Lesson23.htm
http://www.dotnetperls.com/multidimensional-array
...
[ EmmaR @ 04.11.2011. 00:48 ] @
Hvala.

Ne nađoh sledeće:
- Da li je moguće uz pomoć foreach naredbe kod višedimenzionih nizova napraviti raspored matrice:
11 12 13 14
21 22 23 24
31 32 33 34
41 42 43 44

Nešto ovako?
Code:
int[,] dvabroja = new int[3,2] { {1,2},{3,4},{5,6} };

            //ne radi
            foreach (int red in dvabroja)
            {
                foreach (int clan in dvabroja[red])
                {
                    Console.Write(clan + "  ");
                }
               Console.WriteLine("");
             }



- Kako odrediti dužinu niza?
[ mmix @ 04.11.2011. 08:24 ] @
foreach nije olaksana for petlja, to je jezicka konstrukcija koja olaksava upotebu enumeratora u .NETu tako da prvo moras razumeti sta su i kako rade enuemratora, za pocetak vidi [ulr=http://msdn.microsoft.com/en-us/library/system.array.getenumerator.aspx]ovde[/url]. Za multidimenzionalni niz enumerator "spljeska" dimenzije u jednu. Da bi uradila to sto hoces, moras da koristis niz nizova, onda svaki za seeb ima svoje enumerator


Code (csharp):

            int[][] dvabroja = new int[][] { new int[] { 1, 2 }, new int[] { 3, 4 }, new int[] { 5, 6 } };

            foreach (int[] red in dvabroja)
            {
                foreach (int clan in red)
                {
                    Console.Write(clan + "  ");
                }
                Console.WriteLine("");
            }
 



[ Qler01 @ 05.02.2012. 16:59 ] @
Traba mi mala pomoc kod nizova
imam jedan zadatak
2. Unesi 10 brojeva, ispisati samo parne.
kako to da uradim da ispisem samo parne?

moj kod je sledeci

int[] niz;
int broj;
int brojac;
Console.Write("Unesi broj clanova niza");
broj = int.Parse(Console.ReadLine());
niz = new int[broj];
for (brojac = 0; 2*brojac==broj; brojac++)
{
Console.Write("Unesi {0} broj clanova niza", brojac);
niz[brojac] = int.Parse(Console.ReadLine());
}
[ Igor Gajic @ 05.02.2012. 19:47 ] @
Code (csharp):

            int[] niz;
            int broj;
            Console.Write("Unesi broj clanova niza: ");
            broj = int.Parse(Console.ReadLine());
            niz = new int[broj];
            for (int i = 0; i < broj; i++) niz[i] = int.Parse(Console.ReadLine());
            Console.WriteLine("Parni brojevi:");
            niz = Array.FindAll(niz, s => s % 2 == 0);
            for (int i = 0; i < niz.Length; i++) Console.WriteLine(niz[i]);
            Console.ReadKey();
 
[ Cerberaspeed @ 13.05.2012. 21:19 ] @
Moze li mi iko ukratko objasniti sta su enumeratori isto je jos bitnije cemu sluze.hvala
[ Igor Gajic @ 13.05.2012. 21:41 ] @
http://www.csharp-station.com/Tutorial/CSharp/lesson17
[ AMD guy @ 23.05.2012. 17:25 ] @
Za Qler01

Svaki paran broj je deljiv sa 2, znači radiš proveru pomoću modulo operatora da vidiš jel ima ostataka, ako nema(vraca nulu) onda je paran broj u suprotnom je neparan

Code (csharp):
private bool IsEven(int value)
        {
            return value % 2 == 0;
        }


private bool IsOdd(int value)
        {
            return value % 2 == 1;
        }

 
[ Mare_TS @ 24.05.2012. 11:11 ] @
Evo i ja da doprinesem malo i uvedem jedan zanimljiv koncept - extenzija :)

Ako postaviš ovakvu metodu u bilo koju tvoju klasu
Code:
        public static bool IsEven(this int broj)
        {
            if (broj % 2 == 0)            
                return true;
            return false;
        }

onda možeš na ovaj način da ispišeš sve parne brojeve
Code:
for (int i = 0; i < niz.Length; i++) 
         if (niz[i].IsEven()) 
             Console.WriteLine(niz[i]);

naravno podrazumeva se da je niz inicijalizovan pre toga ;)
[ Shadowed @ 24.05.2012. 15:20 ] @
Nikako ne kapiram zasto ljudi pisu ovakav kod:
Citat:
Mare_TS:
Code:
        public static bool IsEven(this int broj)
        {
            if (broj % 2 == 0)            
                return true;
            return false;
        }



return value % 2 == 0; je sasvim dovoljno.
Nista licno. Vidjam to cesto. Obicno i sa nepotrebnim else.

Inace, podrzavam extension metode :)
[ Igor Gajic @ 24.05.2012. 15:25 ] @
Iz knjige Java™ Puzzlers: Traps, Pitfalls, and Corner Cases , ali isto tako vazi i za C#

Citat:

Puzzle 1: Oddity
The following method purports to determine whether its sole argument is an odd number. Does the method work?


public static boolean isOdd(int i) {

return i % 2 == 1;

}

Solution 1: Oddity
An odd number can be defined as an integer that is divisible by 2 with a remainder of 1. The expression i % 2 computes the remainder when i is divided by 2, so it would seem that this program ought to work. Unfortunately, it doesn't; it returns the wrong answer one quarter of the time.

Why one quarter? Because half of all int values are negative, and the isOdd method fails for all negative odd values. It returns false when invoked on any negative value, whether even or odd.

This is a consequence of the definition of Java's remainder operator (%). It is defined to satisfy the following identity for all int values a and all nonzero int values b:

(a / b) * b + (a % b) == a


In other words, if you divide a by b, multiply the result by b, and add the remainder, you are back where you started [JLS 15.17.3]. This identity makes perfect sense, but in combination with Java's truncating integer division operator [JLS 15.17.2], it implies that when the remainder operation returns a nonzero result, it has the same sign as its left operand.

The isOdd method and the definition of the term odd on which it was based both assume that all remainders are positive. Although this assumption makes sense for some kinds of division [Boxing], Java's remainder operation is perfectly matched to its integer division operation, which discards the fractional part of its result.

When i is a negative odd number, i % 2 is equal to -1 rather than 1, so the isOdd method incorrectly returns false. To prevent this sort of surprise, test that your methods behave properly when passed negative, zero, and positive values for each numerical parameter.

The problem is easy to fix. Simply compare i % 2 to 0 rather than to 1, and reverse the sense of the comparison:


public static boolean isOdd(int i) {

return i % 2 != 0;

}

If you are using the isOdd method in a performance-critical setting, you would be better off using the bitwise AND operator (&) in place of the remainder operator:


public static boolean isOdd(int i) {

return (i & 1) != 0;

}

The second version may run much faster than the first, depending on what platform and virtual machine you are using, and is unlikely to run slower. As a general rule, the divide and remainder operations are slow compared to other arithmetic and logical operations. It's a bad idea to optimize prematurely, but in this case, the faster version is as clear as the original, so there is no reason to prefer the original.

In summary, think about the signs of the operands and of the result whenever you use the remainder operator. The behavior of this operator is obvious when its operands are nonnegative, but it isn't so obvious when one or both operands are negative.
[ Shadowed @ 24.05.2012. 16:07 ] @
Cini mi se da se nismo razumeli. Nisam mislio na nacin dolazenja do resenja nego na strukturu koda:
Code:

if (uslov)
    return true;
else  //Ili bez else
    return false;


vs.

Code:
return uslov
[ ravni @ 24.05.2012. 16:47 ] @
Citat:
Mare_TS: Evo i ja da doprinesem malo i uvedem jedan zanimljiv koncept - extenzija :)

Ako postaviš ovakvu metodu u bilo koju tvoju klasu
Code:
        public static bool IsEven(this int broj)
...

Ne moze bas bilo koja. Klasa mora biti staticka. Takodje, mora se uvesti namespace klase sa using na mestu koriscenja (sto nekad nije ocigledno).
[ AMD guy @ 29.05.2012. 08:24 ] @
Citat:
Mare_TS: Evo i ja da doprinesem malo i uvedem jedan zanimljiv koncept - extenzija :)

Ako postaviš ovakvu metodu u bilo koju tvoju klasu
Code:
        public static bool IsEven(this int broj)
        {
            if (broj % 2 == 0)            
                return true;
            return false;
        }

onda možeš na ovaj način da ispišeš sve parne brojeve
Code:
for (int i = 0; i < niz.Length; i++) 
         if (niz[i].IsEven()) 
             Console.WriteLine(niz[i]);

naravno podrazumeva se da je niz inicijalizovan pre toga ;)


Jel tako rade ekstenzije, vidim da je IsEven metod ima jedan parametar a kod for petlje se ne prosledjuje nista iako je metod tako definisan da zahteva 1 parametar.
[ smark @ 29.05.2012. 10:25 ] @
Nije to obican metod, vidis da u parametru stoji: this int broj.
[ Shadowed @ 29.05.2012. 10:51 ] @
Citat:
AMD guy: Jel tako rade ekstenzije, vidim da je IsEven metod ima jedan parametar a kod for petlje se ne prosledjuje nista iako je metod tako definisan da zahteva 1 parametar.

To kompajler prepakuje tako da umesto niz[i].IsEven() bude u stvari ImeKlase.IsEven(niz[i]). Pomocu onog "this" u definiciji funkcije odredjujes koji je parametar zapravo to na cemu pozivas funkciju (u ovom slucaju niz[i]).


[Ovu poruku je menjao Shadowed dana 31.05.2012. u 22:41 GMT+1]
[ AMD guy @ 31.05.2012. 11:52 ] @
Hvala.
[ Marko Simulak @ 03.06.2012. 23:35 ] @
Videh da vec ima slicna tema, pa rekoh da ne otvaram novu..
Pozdrav :D, radim neke zadatke sa matricama, i nesto me zeza ispis
Code:
 
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                    Console.Write("{0: 0}", a[i, j]);
                Console.WriteLine();
            }
            p = Convert.ToDouble(z / br);
            Console.WriteLine("Prosek parnih elemenata je: {0:0.00}", p); //nece da mi ispise decimalno, zaokruzi
            Console.WriteLine("{0:0.00}", 12.15465);                     //hoce da mi ispise decimalno (12.15)

            Console.Read();
           

[ Burgos @ 04.06.2012. 01:45 ] @
Ovde već imaš zaokruživanje:

Code:
z / br


Probaj:


Code:
            p = Convert.ToDouble(z) / br; 

[ ravni @ 04.06.2012. 08:39 ] @
Citat:
Marko Simulak: Videh da vec ima slicna tema, pa rekoh da ne otvaram novu..


Molim te, sledeci put otvori novu temu.