an array with char index
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: an array with char index

  1. #1
    Join Date
    Apr 2010
    Posts
    4

    an array with char index

    Hi guys,

    I need some help to create an array with character indexing like this:
    A B C D E ....
    A 0 0 0 0 0
    B 0 0 0 0 0
    C 0 0 0 0 0
    D 0 0 0 0 0
    E 0 0 0 0 0
    .
    .

    so that I can record the occurrence of a particular pair of letters. I tried to search on msdn but found no clue yet.
    my guess is that I can use an alphabet enum to assign index to each letter. But the problem is that I cant use alphabet letter as textual data (ie string, char)
    Can anybody give me a hint?

  2. #2
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    Re: an array with char index

    Hi there.

    You could a two dimensional array of integers. You don't have to index with a 'char'. You can map each index to a character. For example 0 -> 'A' and so on. If you want you can define an enumeration for the alphabet. There is already System.Windows.Forms.Keys enumeration but the values there map to ASCII so you couldn't use them to index into the array. Though you could always subtrat 65 from each value...

  3. #3
    Join Date
    Jun 2008
    Posts
    2,477

    Re: an array with char index

    Don't use arrays, use an associative collection:

    Code:
    Dictionary<char,int> tally = new Dictionary<char,int>();
    foreach( char c in someString.ToCharArray( ) )
    {
        if( !tally.ContainsKey( c ) )
        {
            tally.Add( c, 0 );
        }
    
        tally[ c ]++;
    }

  4. #4
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: an array with char index

    Had time to experiment. Created a CharMatrix class that should do what you want:
    Code:
    public class CharMatrix
    {
        // row, column, value
        private Dictionary<Char, Dictionary<Char, Int32>> _matrix;
    
        public CharMatrix()
        {
            // empty rows with no columns, results in  Get(...) returning values of zero
            _matrix = new Dictionary<Char, Dictionary<Char, Int32>>();
        }
    
        /// <summary>
        /// Get a value from the Matrix
        /// </summary>
        /// <param name="row">The row index</param>
        /// <param name="column">The column index</param>
        /// <returns>The value</returns>
        public Int32 Get(Char row, Char column)
        {
            Dictionary<Char, Int32> rowVector;
    
            // see if row is present
            if (_matrix.TryGetValue(row, out rowVector))
            {
                // return value from column if present
                Int32 matrixValue;
                if (rowVector.TryGetValue(column, out matrixValue))
                    return matrixValue;
            }
    
            // no column and/or no row, so return 0
            return 0;
        }
    
        /// <summary>
        /// Set a value in the matrix
        /// </summary>
        /// </summary>
        /// <param name="row">The row index</param>
        /// <param name="column">The column index</param>
        /// <param name="value">The value</param>
        public void Set(Char row, Char column, Int32 value)
        {
            Dictionary<Char, Int32> rowVector;
    
            // see if row is present
            if (_matrix.TryGetValue(row, out rowVector))
            {
                // update the value if it exists else add a new value
                if (rowVector.ContainsKey(column))
                    rowVector[column] = value;
                else
                    rowVector.Add(column, value);
            }
            else
            {
                // row was not present so create it, with the column value
                Dictionary<Char,Int32> newRow = new Dictionary<Char,Int32>();
                newRow.Add(column, value);
    
                // add it to the matrix
                _matrix.Add(row, newRow);
            }
        }
    
        /// <summary>
        /// Increment a value in the matrix
        /// </summary>
        /// <param name="row">The row index</param>
        /// <param name="column">the column index</param>
        public void Increment(Char row, Char column)
        {
            // neatest way, not the best performance
            Set(row, column, Get(row, column) + 1);   // easy to do Add, Subtract, Multiply etc
        }
    }
    Tested with...
    Code:
    CharMatrix matrix = new CharMatrix();
    
    Int32 w = matrix.Get('R', 'K');    // w = 0
    
    matrix.Set('A', 'B', 10);
    Int32 x = matrix.Get('A', 'B');    // x = 10
    
    matrix.Increment('A', 'B');
    Int32 y = matrix.Get('A', 'B');    // y = 11
    
    matrix.Increment('A', 'D');
    Int32 z = matrix.Get('A', 'D');    // z = 1
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center