multidimensional array

1. Junior Member
Join Date
Dec 1969
Posts
6

## multidimensional array

Please read the codes below. I can&#039;t achieve the objectives stated.Thanks<BR><BR>vivi<BR><BR>/*<BR> Assuming 3 students in a class and 4 subjects, write a program which will satisfy the objectives below:<BR> a) Display table of student x subject (5rows x 4columns)<BR> b) Compute the CPA of each student<BR> b) Compute the class average and standard deviation CPA for each subject<BR> c) Determine the average CPA of the whole class for all subjects put together.*/<BR><BR>using System;<BR>using System.IO;<BR><BR>class FindGrades<BR>{<BR> public static void Main()<BR> {<BR> String [] Student = new string[2]<BR> {"Peter","John","Sally"};<BR> <BR> int [][] Peter=new int[4][];<BR> Peter[1]=new int[67];<BR> Peter[2]=new int[63];<BR> Peter[3]=new int[43];<BR> Peter[4]=new int[57];<BR><BR> int [][] John=new int[4][];<BR> John[1]=new int[63];<BR> John[2]=new int[62];<BR> John[3]=new int[89];<BR> John[4]=new int[90];<BR><BR> int [][] Sally=new int[4][];<BR> Sally[1]=new int[33];<BR> Sally[2]=new int[68];<BR> Sally[3]=new int[80];<BR> Sally[4]=new int[90];<BR><BR> int i;<BR> for(i=0;i&#060;Peter.Length;i++)<BR> Console.WriteLine("Peter[i][j]");<BR> <BR> }<BR> <BR>}

2. Senior Member
Join Date
Dec 1969
Posts
1,424

## This looks like fun.

I&#039;d love to do it for you because I love messing around with Arrays, especially multi-dimensional arrays and jagged arrays - but because this is obviously your homework, I&#039;ll just point you toward the following article:<BR><BR>http://msdn.microsoft.com/library/en-us/dnguinet/html/drguinet6_update.asp<BR><BR>If you get stuck with [hl="yellow"]specific[/hl] parts of it feel free to e-mail me at:<BR><BR> showusyourcode AT hotmail DOT com<BR><BR>But do take the time to read that article, it&#039;s fascinating!!

3. Junior Member
Join Date
Dec 1969
Posts
6

## RE: This looks like fun.

Hi Thanks for the url. I been working on this for the past 6 hours. Reading, doing and making mistakes, learning from mistakes as well. But I am completely new to programming.Its really tough.One question here:<BR><BR>Case 1: int[] num=new int[]{1,2,3};<BR>Case 2: int[] num new int[3]{1,2,3);<BR><BR>which case is correct to declare and initialise an array?<BR><BR>Thank u<BR>Viv

4. Senior Member
Join Date
Dec 1969
Posts
1,424

## They're both the same.

Although, if I&#039;m initializing the array at the same time I&#039;m declaring it, I do not include the array size in brackets:<BR><BR> int[] num = new int[] { 1, 2, 3 } ;<BR><BR>as opposed to:<BR><BR> int[] num = new int[3] { 1, 2, 3 } ;<BR><BR>However, obviously if you are initializing the array at a later stage, then you will need to declare its size:<BR><BR> int[] num = new int[3] ;<BR> for( int i = 1; i &#060; 4; i++ ) <BR> num[i] = i ;

5. Junior Member
Join Date
Dec 1969
Posts
6

## How do we do sorting for array ?

Thanks.<BR><BR>Another question here:<BR>How can I do sorting using array?<BR>It&#039;ll be great if you can give me some tips on this.<BR>ASsuming I want to sort in ascending order for 6 int.<BR><BR>Viv

6. Senior Member
Join Date
Dec 1969
Posts
1,424

## Depends!

It depends on what interfaces are supported by the datatype that you are sorting on. Look at the documentation for the static Sort() method of the Array class.<BR><BR>Here&#039;s an example that would probably work:<BR><BR>----------------------------------<BR><BR> Int32 myInts[] = new Int32[] {2, 6, 8, 1, 3, 9, 4} ;<BR> Array.Sort(myInts) ;<BR> System.Text.StringBuilder sb = new System.Text.StringBuilder() ;<BR><BR> for( int idx = 0 ; idx &#060; myInts.GetUpperBound(0); idx++ )<BR> sb.AppendFormat("Position {0} value is {1}
", idx, myInts[idx]) ;<BR><BR> MessageBox.Show(sb.ToString()) ;<BR>----------------------------------<BR><BR>I actually wrote it and compiled it using VB so I hope that I managed to convert all the bits to C#, here&#039;s the original code that I compiled:<BR><BR>----------------------------------<BR><BR> Dim myInts As Int32() = {2, 6, 8, 1, 3, 9, 4}<BR> Array.Sort(myInts)<BR> Dim idx As Int32<BR> Dim sb As New System.Text.StringBuilder()<BR> For idx = 0 To myInts.GetUpperBound(0)<BR> sb.AppendFormat("Position {0} value is {1}{2}", idx, myInts(idx), ControlChars.NewLine)<BR> Next<BR> MessageBox.Show(sb.ToString())<BR><BR>----------------------------------

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•