Thread: Convert int array to string!

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Question Convert int array to string!

    HI!
    I have a 2D array. It is 20 columns of 20 ints (20x20).It contains only values from 0 to 9.
    I am/have been attempting to use this 2D array of int to initialise a 20x21 array of char. I also want to replace the associated integer (0 to 9) with a corresponding character ('A' to 'J'). ie) A = 0 etc.
    I am kinda on my way to achieving this part but I also need to have a null terminating character as the 21st character of each column.

    This is what I've done so far... It isn't working yet - Please Help!!!

    Code:
    #include <stdio.h>
    
    #define MAX 20
    
    main()
    {
    	int my_array[MAX][MAX] = {
                                                              {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
                                                              {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
                                                              { etc. etc. etc.                  }
                                                              };
    	
    
                    int i = 0, j, x;
    	char my_arraystr[MAX][MAX + 1];
    	
    
    for (i=0; i < MAX; ++i)
    	{
    		for (j=0; j < MAX; ++j)
    		{
    			my_arraystr[i][j] = my_array[i][j];
    				for(x='A'; x < 'K'; ++x)
    			{	
    				my_arraystr[i][j] = x;
    			}
    
    		}
    
    	}
    
    	for (i=0; i < MAX; ++i)
    	{
    		for (j=0; j < MAX; ++j)
    			printf("%c", my_arraystr[i][j]);
    			putchar('\n');
    	}
    }

    Anyone have any help or advice as what I can do to solve this problem and to add the null character. As it stands, this will compile and run but it doesn't display (0 - 9) as (A - J) and I don't know what to do about the 21st element of each line?

    Help please
    Thanks

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    int i = 5;
    char a = 'A' + i;
    try to examine a using different values for i

    Code:
    for (j=0; j < MAX; ++j)
    {
    	my_arraystr[i][j] = ...
    }
    my_arrstr[i][MAX] = 0;
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Unhappy

    Hi!

    Ive been playin round withit! gettin really frustrated. This is my latest version - (lol still not working tho!)


    Code:
    	for (i=0; i < MAX; i++)
    	{
    		for (j=0; j < MAX; j++)
    		{
    			my_arraystr[i][j] = my_array[i][j];
    				for(x='A'; x < 'K'; x++)
    			{	
    				my_arraystr[i][j] = x;
    			}
    
    		}
    		my_arraystr[MAX][MAX + 1] = '\0';
    
    	}

    HELP PLEASEEEE!!

    Thanks 4 input

    The problem part is this part....

    Code:
    my_arraystr[i][j] = my_array[i][j];
    				for(x='A'; x < 'K'; x++)
    			{	
    				my_arraystr[i][j] = x;
    			}
    As all i get in every element of the string is the letter J.

    When I run the program... 20 rows of 20 J's are displayed on screen.

    What I want is that when the element of the array is a zero the letter 'A' is displayed, when it is 1 - the letter 'B' is displayed and so on up until when the element value is 9 - the letter 'J' is displayed! :S i hope thats clear
    Last edited by strokebow; 12-01-2006 at 09:34 AM.

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    75
    Hope the following code helps:
    Code:
       for (i=0; i<MAX; i++)
       {
          for (j=0; j<MAX; j++)
             my_arraystr[i][j] = my_array[i][j]+'A';
          my_arraystr[i][j] = '\0';
       }
    
       for (i=0; i<MAX; i++)
          puts(my_arraystr[i]);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  5. Converted from Dev-C++ 4 to Dev-C++ 5
    By Wraithan in forum C++ Programming
    Replies: 8
    Last Post: 12-03-2005, 07:45 AM