need help not too urgent

This is a discussion on need help not too urgent within the C Programming forums, part of the General Programming Boards category; so here the deal im pretty new at this programming lark and one of my lectures set the task of ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    27

    need help not too urgent

    so here the deal im pretty new at this programming lark and one of my lectures set the task of designing a puzzle block game (DONT WORRY NOT ASKING ANYONE TO DO IT).

    using an array of 16 you got to rearrange to array so that its in ascending order, and one of the elements is a gap so orginally i set it to be zero, now here comes the intresting part when you reprint the block using a function you got to change the 0 to be an X so i did an if statement to rectify that problem

    now i had an idea to change the int array of 16 to make it a char array and ive changed to rest of the program to allow this but the printing out of the array wont work because some of the numbers are double digit and that an char can only hold one number ie the character '12' will only print out '2' because of that fact mentioned above. i will show the code in a second but what im hoping to find out if the is a way of using some thing like atoi that will convert the integer value for the double digit numbers coz if there is no way to do that i will probably just go back to the integer array that i had in the beginning

    Code:
    #include <stdio.h> 
    #include <stdlib.h>
    #include <string.h>
    char print_board (char array[16]) ; //function to print out the board
    
    char board[16] = {'1','2','3','4','5','6','8','12','9','10','7','X','13','14','11','15'};
    
    int main (void)
    {
    
    print_board(board);
    
    }
    
    
    char print_board (char array[16]) 
    {
    int index = 0;
    	printf("\n\n\t");
    	for(index = 0;index<=15;index++)
    	{
    		if (index% 4 == 0)
    		{
    			printf("\n\t");
    		}
    		if ((board[index] >='10') && (board[index]!='X'))
    			printf("%c   ", board[index]);
    
    		else
    		printf ("%c    ", board[index]);
    		
    		
    	}
    	return board[16];
    }
    that is only a selected sample
    also you may find that this is very similar to something that i posted before but i know now that i asked the wrong question earlier

  2. #2
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    '12' isn't a character, it's a string. Try using 12 (without the single quotes) instead.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    27
    no i just took out the single quotes and all the single didgit numbers printed fine as usual but i got a load of symbols where the double digit numbers should be i also just tried to change the %c to %s for the string but no go and error about linking

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    Try char board[16][3] = { "1", "2" ... "15" }; Then treat every board[index] as a string instead of a character.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    27
    that sounds like ill give it a try but later on in the program im swapping the elements and at the moment (before the string bit by itsme86 ) its

    Code:
                 temp = board[pos1];
    	(board[pos1] = board[pos2]);
    	(board[pos2] = temp);
    the error is coming in the second and third line of that about the equals is left operand or something along those lines would that be to do with the string or could that be something else in the program

  6. #6
    Registered User
    Join Date
    Oct 2004
    Posts
    27
    so here me taking back

    that sounds like ill give it a try but later on in the program im swapping the elements and at the moment (before the string bit by itsme86 ) its


    Code:
                 temp = board[pos1];
    	(board[pos1] = board[pos2]);
    	(board[pos2] = temp);
    the error is coming in the second and third line of that about the equals is left operand or something along those lines would that be to do with the string or could that be something else in the program
    because im stupid and ive got it to work (nearly but i can work the rest out)

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Since your board array now contains strings, you'll have to use strcpy() to copy them:
    Code:
    	strcpy( temp,  board[pos1] );
    	strcpy(board[pos1], board[pos2] );
    	strcpy(board[pos2], temp );
    Or you could use a for-loop.

  8. #8
    Registered User
    Join Date
    Oct 2004
    Posts
    27
    Since your board array now contains strings, you'll have to use strcpy() to copy them:

    Code:
    	strcpy( temp,  board[pos1] );
    	strcpy(board[pos1], board[pos2] );
    	strcpy(board[pos2], temp );
    Or you could use a for-loop.
    i didnot think of that so would that mean that i would have to when searching the array which before i including of strings i had

    Code:
    for (index = 0; index <=15; index++)
    {
           if (board[index] == num);
                   break;
    }
    where i had num to be an integer being passed in to the array, should i change it to be a a string of size 3 and do strcmp to make that code look like this

    Code:
    for (loop same as above)
    if (strcmp (board[index], num == 0)
    {
          break;
    }
    would that do the same thing

    sorry if it looks like im lazy its just i have to go home and try it as my floppy disk im using is not working and i want as many ideas as possible

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    where i had num to be an integer being passed in to the array, should i change it to be a a string of size 3 and do strcmp to make that code look like this
    Yes, or if it's being used as an int somewhere else, you could always convert it before the comparsion. And yes, you are correct, you need to use strcmp(). Use what you posted, or if num needs to be left as a int:
    Code:
    char snum[10];
    sprintf( snum, "%d", num );
    for (index = 0; index <=15; index++)
    {
       if (strcmp( board[index], snum ) == 0)
       {
          break;
       }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. display character size...(quite urgent..)
    By karthi in forum C Programming
    Replies: 10
    Last Post: 07-11-2007, 10:42 PM
  2. beginner plzz help urgent
    By sara101 in forum C Programming
    Replies: 11
    Last Post: 01-14-2007, 10:38 PM
  3. Linked List Need Help Urgent
    By ykchua in forum C Programming
    Replies: 5
    Last Post: 08-17-2004, 03:57 PM

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