displaying a board with 2D array

This is a discussion on displaying a board with 2D array within the C++ Programming forums, part of the General Programming Boards category; hey guys, I'm trying to display a board with a multidimensional array...but it seems not to be working. The array ...

  1. #1
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572

    displaying a board with 2D array

    hey guys, I'm trying to display a board with a multidimensional array...but it seems not to be working. The array is supposed to grab a certain random letter and the board should like the following, where the letters are from the array:
    Code:
       1 2 3 4 5
    - - - - - - -
    A|A O E A E
    B|H E Y F U
    C|D T A A O
    D|L E S B I
    E|O W O S N
    now here is what I have been playing with: (I'm inlcluding the get random letter function, which I am certain works in my code:

    =============EDIT===================
    CODE POSTED A FEW REPLIES DOWN
    =============EDIT===================
    any suggestions?

    axon
    Last edited by axon; 03-07-2003 at 03:34 PM.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    What is not working?

    Kuphryn

  3. #3
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    I don't see anywhere where you enter the values into boardArray. I see it declared but never changed or initialized so my guess is that it is printing out gibberish.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    for( i='A'; i<='E'; i++)
    //this means for i = 65 and as long as i <= 70, or something like 
    //that.  I suspect you need something more like:
    // for(i = 0; i < 5; ++i)
    
    {
      cout << i << " ";
      for(j='A'; j<='E'; j++)
      //same here, see above
    
      {
         randomLetteri = getCharacter(totalPercentArray);
         //randomLetteri is never declared, but may well be typo
    
        cout << boardArray[randomLetter][randomLetter] << endl;
        //when the indexes of a 2D array are the same you will only 
        //see the values on a diagonal of the table (eg A1, B2, C3).
        //Use i for first index and j for the second.
      }
    }

  5. #5
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572
    >>What is not working?
    Its only printing one weird character in column a1

    >>//this means for i = 65 and as long as i <= 70, or something >>like
    >>//that. I suspect you need something more like:
    >>// for(i = 0; i < 5; ++i)

    Yes, but I want the first column to be A B C D E, the second loop should be chnaged


    >>I don't see anywhere where you enter the values into >>boardArray. I see it declared but never changed or initialized >>so my guess is that it is printing out gibberish.

    What should the indexes of boardArray be then? should I initialize it to zero?

    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Tried to fix yours, but I ended up hacking it around alot, so ..... see what you make of this version:
    Code:
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    char getCharacter(void)
    {
      int theRandomNumber;
      theRandomNumber = (int) rand() % 26;
      theRandomNumber += 'A';
      return theRandomNumber;
    }
    
    void displayTheBoard(void)
    {
      const int SIZE = 5;
      char      boardArray[SIZE][SIZE];
      int      i;
      int      j;
      
      cout << "  1 2 3 4 5\n" << "  - - - - -\n";
      for (i = 0; i < SIZE; i++)
      {
        cout << (char)(i + 'A') << " ";
        for (j = 0; j < SIZE; j++)
        {
          boardArray[i][j] = getCharacter();
          cout << boardArray[i][j] <<" ";
        }
        cout <<endl;
      }
    }
    
    int main()
    {
      displayTheBoard();
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572
    ==============EDIT 2=================
    I SOLVED THE PROBLEM THANKS TO HAMMER, THERFORE
    THE FINAL EDIT. I CUT THE WHOLE CODE (AGAIN) ONLY
    LEAVING THE FUNCTION THAT WAS IN QUESTION. THE SOLUTION
    IS POSTED IN THE NEXT REPLY.
    ==============EDIT2=================

    thanks a lot Hammer, I'll see what I can do with it. I read up on multidimensional arrays a bit today, so I'm getting a better handle on it.
    The reason I had this:
    Code:
    theRandomNumber = (float)rand()/RAND_MAX;
    is that I want the random number between 0-1. The reason for this is that earlier in my program I set a running percentage for some letters. So my function getChar() was to make a random number and the match it to the appropriate letter. I'm posting the whole thing, and if you have any more hints/suggestions I REALY do appreciate them:

    Code:
    void displayTheBoard(float totalPercentArray[ ])
    {
    	const int SIZE = 5;
    
    	//declaring variables
    	char randomLetter;
    
    	char i; 
    	int j;
    
    
    	//declaring arrays
    	char boardArray[SIZE][SIZE];
    
    	cout << "  1 2 3 4 5\n"
    		 << "  - - - - -\n";
    	for( i='A'; i<='E'; i++)
    	{
                    randomLetter = getChar(totalPercentArray);
    		cout << i << " ";
    		for ( j=0; j<5; j++ )
    		{
    			cout << boardArray[randomLetter][randomLetter] << endl;
    		}
    		
    	}
    }
    sorry for the length. The program begins by main calling countAllTheLetters();

    thanks a bunch,

    axon

    ------------------------------------edit----
    I also deleted the code from my original post, as this is the same with an extra function
    Last edited by axon; 03-07-2003 at 04:49 PM.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  8. #8
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572

    finally got it

    Thanks to our self proclaimed Super Moderator - Hammer, I solved that part. Here is the code for the solution. Again, program starts with---> int main() calling countAllTheLetters()

    thanks again for all your help!

    axon

    Code:
    void countAllTheLetters()
    {
    	cout.setf(ios::fixed);
    	cout.setf(ios::showpoint);
    	cout.precision(4);
    
    	const int LIMIT = 128;
    		
    	//declaring arrays
    	int lettersArray[LIMIT];
    	float totalPercentArray[25];
    	
    	//declaring variables
    	char c;
    	int i;
    	int sum = 0;
    	ifstream inStream;
    	
    	inStream.open("darwin.txt"); //opening file
    
    	if( inStream.fail() )
    	{
    		cout << "The input file, failed to open. Exiting....\n\n";
    		exit(-1);
    	}	
    
    	for ( i=0; i<LIMIT; i++ )
    	{
    		lettersArray[i] = 0;
    	}
    	
    	while( inStream >> c)
    	{
    		lettersArray[toupper(c)]++; //count each instance of every character
    	}
    
    	for( i='A'; i<='Z'; i++ )//to get only alphabetical characters
    	{ 
    		//count all letters
    		lettersArray[i];
    		//get total of all letters
    		sum += lettersArray[i]; 
    	}
    
    	for( i='A'; i<='Z'; i++ )
    	{
    		//running total of percentages
    		totalPercentArray[i] = lettersArray[i]/(double)sum+totalPercentArray[i-1];
    	}
    	
    	cout << "\nTotal number of characters: " << sum ; //display total characters [ERASE THIS]
    	cout << endl;
    
    		//display starting board
    	displayTheBoard(totalPercentArray);
    }
    
    //==============================================
    
    char getCharacter(float totalPercentArray[ ])
    {
    	//declaring variables
    	float theRandomNumber;
    	int i;
    	
    	//division by RAND_MAX to obtain value between [0, 1]
    	theRandomNumber = (float)rand()/RAND_MAX;
    	
    	for( i='A'; i<'Z'; i++)//set random numbers generated in order 
    	{
    		if ( theRandomNumber < totalPercentArray[i] )
    		{
    			return (i); //return matched letter from array
    		}
    	}	
    }
    
    //==============================================
    void displayTheBoard(float totalPercentArray[ ])
    {
    	const int SIZE = 5;
    
    	//declaring variables
    	char randomLetter;
    
    	int i; 
    	int j;
    
    	//declaring arrays
    	char boardArray[SIZE][SIZE];
    
    	cout << "  1 2 3 4 5\n"
    		 << "  - - - - -\n";
    	for( i = 0; i < SIZE; i++)
    	{
    		cout << (char)(i + 'A') << " ";
    		for ( j = 0; j < SIZE; j++ )
    		{
    			boardArray[i][j] = getCharacter(totalPercentArray);
    			cout << boardArray[i][j] << " ";
    		}
    		cout << endl;
    	}
    }
    Last edited by axon; 03-07-2003 at 07:00 PM.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: finally got it

    >>self proclaimed Super Moderator - Hammer
    There's nothing self proclaimed about it. Like most mods, I was voted in, and the "Super" is a vBulletin term. Anyway, back to the topic:

    >>void main()
    Read this.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572
    OFCOURSE I MENT INT MAIN()

    SORRY....Salem has a cool avatar about it! hehe. And by the way...myprofessor would bail me right away if he saw void main, in my school work.

    axon

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D array of threads?
    By shorinhio in forum C Programming
    Replies: 2
    Last Post: 01-04-2007, 03:02 PM
  2. Dictionary into a 2d Char Array... Problems.
    By Muzzaro in forum C Programming
    Replies: 10
    Last Post: 12-02-2006, 11:34 AM
  3. passing/pointing to a 2d array
    By jamie85 in forum C Programming
    Replies: 7
    Last Post: 10-28-2005, 10:16 PM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Pick a number....
    By Salem in forum A Brief History of Cprogramming.com
    Replies: 39
    Last Post: 01-19-2003, 06:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21