Thread: A couple questions...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    16

    A couple questions...

    Ok so I have a project where I read in a file containing a 3 columns of different numbers/letters. The three columns are [stone color] [column] [row]. For example
    Code:
    b A 1
    b B 4
    B b 1
    B C 2
    B C 3
    W A 3
    w A 1
    W B 3 
    W B 2
    B/b and W/w are the stone colors. A-I are the rows and 1-9 are the columns.
    This is my code so far.
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    int main(void)
    {
    	char stone_color[82];
      	char column[82];
      	int row[82];
      	int i,x, j;
     
    
      	FILE *fp;
      	fp = fopen("goboard1.txt", "r");
    
      	if (fp == NULL) {
        		printf(" Not a valid pointer! \n");
    			exit(1);
    		}
      	
    		i = 0;
    		while (fscanf(fp," %c %c%d", &stone_color[i], &column[i],&row[i]) == 3) {
    		
    		i++;
    	}
      	 while ( stone_color[j] != NULL && column[j] != NULL) {
    			stone_color[j] = tolower(stone_color[j]);
    			column[j] = toupper(column[j]);
    			j++;
    	}
    	
    	
    	
    		for(x=0;x<i;x++) {
    			printf("%c  %c  %d\n",stone_color[x],column[x],row[x]);
    	}	
    	return (0);
    }
    The end result of the project would display the stone colors at the respective coordinates assigned to them in the file on the 9x9 board such as a "b" at (A,1).

    Ok so Question 1:

    if I have an an array for the board like
    Code:
    char array[8][8];
    Is there any way to designate that my columns should be A-I instead of 0-8
    so that I can just read in a point like array[A][1]?
    Or would I have to convert my letters to numbers and if so how would I go about doing that?

    Question 2:
    Is it possible to use an array element as an index value for another array? For example, if I had
    Code:
    array[column[0]][row[0]];
    would this use (A,1) as the indexes for the array (assuming that A is stored in column[0] and 1 is stored in row[0])? Then if I wanted to use the element for stone_color[0] as the element for array[column[0]][row[0]] would it be as simple as
    Code:
    array[column[0]][row[0]] = stone_color[0];
    or would I have to do it another way?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you want a 9x9 board, declare a 9x9 board, not an 8x8 board.
    Code:
    int ninebynine[9][9];
    As to assigning values, just subtract 'A' from your letter.

    'A' - 'A' = 0


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    You could also do something like
    Code:
    #define A 0
    #define B 1
    and so on, but I advise against doing that for single character names.
    Code:
    while(!asleep) {
       sheep++;
    }

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    16
    Quote Originally Posted by TheBigH View Post
    You could also do something like
    Code:
    #define A 0
    #define B 1
    and so on, but I advise against doing that for single character names.
    I was thinking about doing that actually. If I did that would it be legal to change column from a character array to a integer array and still read in the letters to that array?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That's not going to work how either of you think it will. If the file has the letter 'A' in it, you still have to do something to tell it that the 'A' you read really should be a number.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Robbie View Post
    Ok so Question 1:

    if I have an an array for the board like
    Code:
    char array[8][8];
    Is there any way to designate that my columns should be A-I instead of 0-8
    so that I can just read in a point like array[A][1]?
    Or would I have to convert my letters to numbers and if so how would I go about doing that?
    Don't confuse the index values (which will always be zero to number of column-1), with the values that the array[atIndex], is currently holding. Indexes will always start at 0 and increment by one, thereafter. What you're describing is an array "slice" I believe, and C doesn't support that.

    Usually, with a problem like this, you want to use a struct (a record), with struct members (fields), and in this way, group these characteristics together, with the thing that they are all describing. Then, declare an array (just one) of those struct, and now everything becomes easier.

    Yes, you can subtract 'a' from 'a' to change lowercase letters to their numerical equivalent, starting with zero. Uppercase 'A' would be used to change uppercase letters to their zero basis, naturally.

    Question 2:
    Is it possible to use an array element as an index value for another array? For example, if I had
    Code:
    array[column[0]][row[0]];
    would this use (A,1) as the indexes for the array (assuming that A is stored in column[0] and 1 is stored in row[0])? Then if I wanted to use the element for stone_color[0] as the element for array[column[0]][row[0]] would it be as simple as
    Code:
    array[column[0]][row[0]] = stone_color[0];
    or would I have to do it another way?
    I don't see any way to make that work.

    Stone color is not an index, nor is [color] or [row]. Yes, smart indexing may freely use any integral value for the index. In a 2D array of strings, for example:

    Code:
    int i = 0;
    if(arrayString[i][strlen(arrayString[i]-1) == '\n')
       array[i][strlen(arrayString[i]-1) = '\0';
    Could be used to replace a newline char, overwriting it with an end of string, char.

    Perhaps a better example would be in using an index file:
    Code:
    #include <stdio.h>
    
    int main(void) {
    
    int index[10];
    int data[10] = {5,2,1,9,6,7,3,8,0,4};
    int i, j, temp;
    
    //set up the index array values:
    for(i = 0;i<10;i++)
       index[i] = i;
    
    //sort the index array, by referencing the data array
    for(i=0;i<9;i++) {
       for(j= 0;j<10;j++) {
           if(index[data[i]] > index[data[j]]) { //ascending order
              temp = index[data[i]];
              index[data[i]] = index[data[j]];
              index[data[j]] = temp;
           }
       }
    }
    printf("\nThe original data order:\n");
    //print the original data array - unchanged:
    for(i=0;i<10;i++)
       printf("%d ", data[i]);
    
    putchar('\nNow in sorted order:\n);
    
    //print out the data in sorted order, using the index array:
    for(i=0;i<10;i++)
       printf("%d ", data[index[i]]);
    
    return 0;
    }
    BTW, this sort through an index, is a handy trick to know. It lets you show data in sorted order, but yet keep it in it's original order, as well.

    It's poor form to calculate the strlen() twice as shown above, (since it could be calculated just once, and saved), but it illustrates that an index can be more than just a number.
    Last edited by Adak; 09-12-2011 at 09:36 PM.

  7. #7
    Registered User
    Join Date
    Sep 2011
    Posts
    16
    Ok so maybe I've gone at this completely wrong. What would be the best way to read in that file and store the color at the specified index? Like 'b' at array[A][1]?

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I already told you how to do it.
    Code:
    scanf( "%c %c %d", &color, &row, &column )
    row -= 'A';
    if( row < 0 || > 8 )
        printf( "You screwed up. Got %c\n", 'A' + row );
    else
        foo[ row ][ column ] = color;
    As much as I like to see myself type, I do actually type a little for your benefit.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Sep 2011
    Posts
    16
    Quote Originally Posted by quzah View Post
    I already told you how to do it.
    Code:
    scanf( "%c %c %d", &color, &row, &column )
    row -= 'A';
    if( row < 0 || > 8 )
        printf( "You screwed up. Got %c\n", 'A' + row );
    else
        foo[ row ][ column ] = color;
    As much as I like to see myself type, I do actually type a little for your benefit.


    Quzah.
    Haha ok thank you. I think I'm starting to picture it a little better. So for reading everything in the file and sorting it in the array would I do this:
    Code:
    while (fscanf(fp," %c %c %d", &stone_color[i], &column[i],&row[i]) == 3) {
    
    	row[i] -= 'A';
    if( row[i] < 0 || > 8 )
        printf( "You screwed up. Got %c\n", 'A' + row );
    else
        foo[ row[i] ][ column[i] ] = color[i];	
    		i++;
    Am I at least warm? or completely off still? lol

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Robbie View Post
    Haha ok thank you. I think I'm starting to picture it a little better. So for reading everything in the file and sorting it in the array would I do this:
    Code:
    while (fscanf(fp," %c %c %d", &stone_color[i], &column[i],&row[i]) == 3) {
    
    	row[i] -= 'A';
    if( row[i] < 0 || > 8 )
        printf( "You screwed up. Got %c\n", 'A' + row );
    else
        foo[ row[i] ][ column[i] ] = color[i];	
    		i++;
    Am I at least warm? or completely off still? lol
    Here's how you find out... type it up, compile it... and see what happens.

    That's what I'd do...

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    Here's how you find out... type it up, compile it... and see what happens.

    That's what I'd do...
    That way you can see the bug in my example.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A couple questions
    By Flakster in forum C++ Programming
    Replies: 7
    Last Post: 08-09-2005, 01:22 PM
  2. A couple questions
    By fenixataris182 in forum C++ Programming
    Replies: 5
    Last Post: 07-26-2005, 05:13 PM
  3. couple questions...
    By Rune Hunter in forum C# Programming
    Replies: 4
    Last Post: 10-03-2004, 10:57 AM
  4. Couple of questions
    By bobjohnson in forum C++ Programming
    Replies: 4
    Last Post: 07-29-2004, 09:43 AM
  5. Couple Questions
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 11-04-2001, 05:14 PM