Originally Posted by
Robbie
Ok so Question 1:
if I have an an array for the board like
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.