Click here to Skip to main content
15,887,585 members
Home / Discussions / C#
   

C#

 
GeneralRe: Not able to use Modern UI Charts for Windows 8 Pin
Pete O'Hanlon20-May-13 22:54
mvePete O'Hanlon20-May-13 22:54 
AnswerRe: Not able to use Modern UI Charts for Windows 8 Pin
Raghavendra Reddy C20-May-13 22:53
professionalRaghavendra Reddy C20-May-13 22:53 
AnswerRe: Not able to use Modern UI Charts for Windows 8 Pin
Richard MacCutchan20-May-13 22:54
mveRichard MacCutchan20-May-13 22:54 
QuestionPROBLEM with SEND DATA to LOCAL PORT C# Pin
Andrew Nguyen20-May-13 17:18
Andrew Nguyen20-May-13 17:18 
AnswerRe: PROBLEM with SEND DATA to LOCAL PORT C# Pin
Ron Beyer20-May-13 17:56
professionalRon Beyer20-May-13 17:56 
QuestionUsage of IDataErrorInfo Pin
radkrish20-May-13 8:11
radkrish20-May-13 8:11 
AnswerRe: Usage of IDataErrorInfo Pin
Ron Beyer20-May-13 14:55
professionalRon Beyer20-May-13 14:55 
GeneralUnderstanding Jagged Arrays Pin
N8tiv20-May-13 3:29
N8tiv20-May-13 3:29 
Okay, declaring arrays is fairly simple… Just like declaring any other variable with the exception of the square brackets.
Initializing one-dimensional arrays also seems fairly simple enough… Initializing two-dimensional arrays is a bit more complicated but still semi-sort of simple if you pay attention to what you're doing.
Jagged arrays, like two-dimensional arrays… Can be a little tricky if you're not paying attention.
I get and understand the declaration and initialization of the jagged arrays, and that you can change the value of any element by re-declaring your array variable…

I'll use the example from the book I'm reading:
(never mind some of the missing closing curly brackets, before I add any more code I want to understand and learn this part first)

C#
// Example 4-10.cs
// Jagged array example

using System;
class JaggedClass
{
    static void Main ()
{
    int[][] myJaggedArray = { new int[] {2, 3, 4}, new int[] {5, 6, 7, 8, 9} };
myJaggedArray[0][0] = 11; // This is changing the first element in both arrays to 11, right?
myJaggedArray[1][2] = 22; //this is changing the first element in the first array to 22 and changing the second element in the second array to 22, right?



Okay, you old pros… Here is the drill/exercise I was supposed to work on:

Drill 4-3
Display the contents of the two-dimensional array called “grades”
that was discussed in this section. Make sure you get the following
result:

Grade=Pass Score=55%
Grade=Good Score=65%
Grade=VeryGood Score=75%
Grade=Distinct Score=85%


The book was showing us three different ways to declare and initialize a two-dimensional arrays.
This is the first way it showed us:
C#
string[,] grades = new string[2, 4] { {"Pass"," Good", "VeryGood", "Distinct"}, {"55%", "65%", "75%", "85%"} };

this is the second way:
C#
string[,] grades = new string[,] { {"Pass", "Good", "VeryGood", "Distinct"}, {"55%", "65%", "75%", "85%"} };

third and final way:
C#
string [,] grades = { {"Pass", "Good", "VeryGood", "Distinct"}, {"55%", "65%", "75%", "85%"} };


I know I probably could have achieved the same result with a "for loop, or a for each loop, maybe even the while loop", but… I couldn't quite figure it out,Confused | :confused: I beat my head against the wall for almost half an hour trying to figure the loop out…Mad | :mad: So, I went a different route… The long and hard way… Roll eyes | :rolleyes:

It took me about 15 min., maybe even 20 min. to even figure that out… My ah-ha moment came when I accidentally printed to the screen the word "Good"… Don't laugh… Cry | :(( I said don't laugh at! Poke tongue | ;-P

I wrote it and I achieved the way it looks in the book, you'll have to compile it in debug it yourself to see what it looks like… I haven't figured out how to format things here in the forum yet.

C#
using System;
using System.Text;

namespace Two_Dimensional_Array
{
    class Program
    {
        static void Main(string[] args)
        {
            string[,] grades = { { "Pass", "Good", "VeryGood", "Distinct" }, { "55%", "65%", "75%", "85%" } };
            string[] gradeScore = {"Grade=", "Score="};
            Console.WriteLine(gradeScore[0] + grades[0, 0] + "         " + gradeScore[1] + grades[1, 0]);
            Console.WriteLine(gradeScore[0] + grades[0, 1] + "         " + gradeScore[1] + grades[1, 1]);
            Console.WriteLine(gradeScore[0] + grades[0, 2] + "     " + gradeScore[1] + grades[1, 2]);
            Console.WriteLine(gradeScore[0] + grades[0, 3] + "     " + gradeScore[1] + grades[1, 3]);
            Console.Read();
        }
    }
}



modified 22-May-13 21:06pm.

GeneralRe: Understanding Jagged Arrays Pin
Richard Deeming20-May-13 3:44
mveRichard Deeming20-May-13 3:44 
GeneralRe: Understanding Jagged Arrays Pin
N8tiv20-May-13 4:00
N8tiv20-May-13 4:00 
GeneralRe: Understanding Jagged Arrays Pin
Richard Deeming20-May-13 4:19
mveRichard Deeming20-May-13 4:19 
GeneralRe: Understanding Jagged Arrays Pin
Richard MacCutchan20-May-13 5:12
mveRichard MacCutchan20-May-13 5:12 
GeneralRe: Understanding Jagged Arrays Pin
N8tiv20-May-13 6:04
N8tiv20-May-13 6:04 
GeneralRe: Understanding Jagged Arrays Pin
Richard MacCutchan20-May-13 7:19
mveRichard MacCutchan20-May-13 7:19 
GeneralRe: Understanding Jagged Arrays Pin
N8tiv20-May-13 7:30
N8tiv20-May-13 7:30 
GeneralRe: Understanding Jagged Arrays Pin
Matt T Heffron20-May-13 8:49
professionalMatt T Heffron20-May-13 8:49 
GeneralRe: Understanding Jagged Arrays Pin
Richard MacCutchan20-May-13 22:26
mveRichard MacCutchan20-May-13 22:26 
GeneralRe: Understanding Jagged Arrays Pin
N8tiv23-May-13 15:24
N8tiv23-May-13 15:24 
GeneralRe: Understanding Jagged Arrays Pin
Richard MacCutchan23-May-13 20:42
mveRichard MacCutchan23-May-13 20:42 
GeneralRe: Understanding Jagged Arrays Pin
N8tiv23-May-13 21:15
N8tiv23-May-13 21:15 
GeneralRe: Understanding Jagged Arrays Pin
Richard MacCutchan23-May-13 22:22
mveRichard MacCutchan23-May-13 22:22 
GeneralRe: Understanding Jagged Arrays Pin
Jasmine250120-May-13 10:44
Jasmine250120-May-13 10:44 
GeneralRe: Understanding Jagged Arrays Pin
N8tiv20-May-13 10:47
N8tiv20-May-13 10:47 
GeneralRe: Understanding Jagged Arrays Pin
Jasmine250120-May-13 10:52
Jasmine250120-May-13 10:52 
GeneralRe: Understanding Jagged Arrays Pin
N8tiv20-May-13 10:57
N8tiv20-May-13 10:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.