Day 77: Looping Through a 2-D Array in C

I reviewed my notes and got a better grasp of what was going on with 2-D arrays in C. The pointing is sort of weird. What I thought was an array of pointers to other arrays is actually not what is happening by default. That only exists if we make a look-up table… I think. At least that’s what I understood from the videos I have watched on the subject. Either way. I have met my goal of working with an 2-D array. I can now get back to pretty printing my Binary tree data structures values.

TLDR;

Okay, so here are the highlights of what I did:

  • Continued to work on the “Technical Questions” section of the book “Cracking the Coding Interview” by Gayle Laakmann McDowell. Within that, I continued my work on Tree data structures. Started trying to replicate the example pretty print functions provided in the article I am reading through. I was able to successfully loop through a 2-D array. This is an indication that I have at least a basic comfort with the topic. I think I will try to work with a 2-D arrays of characters in C just like this and finally move on to finishing my horizontal pretty print function for a binary tree. I now have all the pieces of knowledge I need. Sheesh what a journey LOL. But hey, I’ve learned a ton about pointers and C.
  • Reviewed some more Git commands alongside covering the basics of YAML. This has been filled with stress and a crash course in DevOps LOL. It took a while for me to make the connection between build tasks and all my studies in the shell. I still have so much to learn but at least I completed the project I was working on. All my time spent trying to get comfortable with the terminal was definitely not wasted. This knowledge has repeatedly carried over to new topics I have had to learn. Best investment so far. I use JavaScript depending on the stack, I use the terminal almost all the time regardless of the stack.

Sample Code -> Looping Through 2-D Array

# include <stdio.h>
# include <stdlib.h>

int main(){
  int doubleArr[][3] = { {2, 3, 6}, {4, 5, 8} };

  int i = 0, j = 0;
  while(i < 2){
    printf("Array: %i\n", i+1);
    while(j < 3){
      printf("Address: %i, ", (*(doubleArr + i) + j) );
      printf("Value: %i\n", *(*(doubleArr + i) + j) );
      ++j;
    }
    j=0; ++i;
  }
  return 0;
}
# Output:

Array: 1
Address: 6422288, Value: 2
Address: 6422292, Value: 3
Address: 6422296, Value: 6
Array: 2
Address: 6422300, Value: 4
Address: 6422304, Value: 5
Address: 6422308, Value: 8

Conclusion

That’s all for today. This is my sixth round of the “#100daysofcode” challenge. I will be continuing my work from round five into round six. I am currently working through the book “Cracking the Coding Interview” by Gayle Laakmann McDowell. My goal is to become more familiar with algorithms and data structures. This goal was derived from my goal to better understand operating systems and key programs that I use in the terminal regularly e.g. Git. This goal was in term derived from my desire to better understand the fundamental tools used for coding outside of popular GUIs. This in turn was derived from my desire to be a better back-end developer.

I have no idea if my path is correct but I am walking down this road anyways. Worst case scenario I learn a whole bunch of stuff that will help me out on my own personal projects.