Wednesday, February 9, 2011

Working with Multi Dimensional Arrays

Today I have been practicing multi-dimensional arrays. Its probably one of my favorite concepts in programming with C. What I like about it is the fact that there can be many dimensions of arrays depending on what you want to do with them. For example for character arrays if I wanted a one dimensional array it would hold a bunch of characters making one string. If I wanted more strings within an array I'd need a two dimensional array. A way I've thought about it is this.

char columns[50];                 /* a  one dimensional array holding characters of that first dimension */
char rows[60][50]                /* a  two dimensional array holding rows of one dimensional arrays or columns
char pages[900][60][50]      /* a three dimensional array that looks a lot like a book now.
char books[1000][900][60][50]  /* a four dimensional array that could hold a bunch of books or a library */
char libraries[1000][1000][900][60][50]  /* a five dimensional array that could hold a bunch of libraries */
char towns[50000][1000][1000][900][60][50]  /* a 6 dimensional for towns and all their libraries */
char states[70][50000][1000][1000][900][60][50]  /* a 7 dimensional array to hold states/provinces */
char states[300][70][50000][1000][1000][900][60][50] /* an 8th dimension for countries */
/* to go further we'd need to travel faster than light which is not possible right now */
/* to go even further we'd need to travel between galaxies */
/* if more than one universe exists than this could be possible */

I made a small program that only shows 3 dimensions as it would require more time to go deeper into them.

#include <stdio.h>

void display(char str[][3][50]);

int main(){



 char pages[][3][50] = {{"First row of page 1",
                         "Second row of page 1",
                         "Third row of page 1"},
                        {"First row of page 2",
                         "Second row of page 2",
                         "Third row of page 2"}};
 display(pages);

 return 0;

}

void display(char str[][3][50]){

  int i;
  int j;
  int k;

  printf("\n");

  for (i = 0; i < 2; i++){               /* pages */
    for (k = 0; k < 3; k++){             /* rows */
       for (j = 0; str[i][k][j]; j++){   /* cols */
          printf("%c",str[i][k][j]);
       }
       j = 0;
       printf("\n");
    }
    k = 0;
    printf("\n");
  }
}




I won't go into detail explaining what it does, but put it here for people who are interested to try out. The more dimensions you go into the more for loops it takes to access the data. There may be a better way to access the data, but haven't learned it yet.

Thats all I have to say for now about multi-dimensional arrays.

Sunday, January 30, 2011

Efficiency can be fun and make your code look cleaner.

Well it's been over two weeks in OOP344 and I haven't blogged about anything. Well today I do have something I'd like to share. Something I learned in class while listening to Fardad's lectures.

When I'd use a for loop for the purpose of character arrays for strings I'd have a habit of using the null-byte in the condition to terminate the loop once found like this:

for (i = 0; str[i] != '\0'; i++){
    /* do something */
}

The for loop would end when the null-byte is found. However Fardad showed the class how to use a more efficient way to terminate the loop without the use of using the null-byte in the condition. Heres how its done:

for (i = 0; str[i]; i++){
   /* do something */
}

At first I didn't get it until I remembered that the null-byte is really a 0 in integer terms even though its a char. 0 being false.

Its like saying:

if (str_is_true){
  /* do something */
}

str_is_true being an integer with a non zero value would be true, instead of doing this:

if (str_is_true != 0){
  /* do something */
}

Which is an inefficient way to find out if str_is_true is not zero. If I were to use the inefficient way I'd probably want to change the variable name to suit the condition. Instead of str_is_true I'd just call it str. I'm getting carried away but thats how I see it.

Even though this may have been obvious to a lot of students in the CPA program in this semester, I thought it was amazing to see something so subtle change my view of how to program with for loops with strings.

Thursday, January 13, 2011

This is my first blog ever.

I don't usually do blogging on the net as I value privacy, but OOP344 (Computer course)  required me to have one so I'll give it a shot. This blog is primarily for programming only and I'd like to keep it that way.