Day 27: ++i vs. i++ and 2-D Array Examples

Worked on copying some example programs from an article on multi-dimensional arrays. I was surprised to learn a few really cool things about the C language. For example, you don’t need to use curly braces for for loops or if statements but only the first statement that immediately follows will be considered. Also, there is a difference between i++ and ++i for some C compilers.

TLDR;

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

  • Started on the “Technical Questions” section of the book “Cracking the Coding Interview” by Gayle Laakmann McDowell. I decided to get a quick intro to the C and Java programming languages before I jump into building the core data structures and algorithms that will be covered in most all technical interviews. I continued with the C for beginners video on YouTube. I am currently working on copying out some example programs that use multi-dimensional arrays. I have learned some cool features of the C programming language in the process. Here is the first example.

Example Code:

// C program to store a week of temperature data for two different cities
// Note: I had to add the fflush to ensure that the output from the program was being printed when it was meant to since my terminal is weird with the buffer and I don't know why. lol

#include <stdio.h>
const int CITY = 2;
const int WEEK = 7;

int main(){
  int temperature[CITY][WEEK];

  // Using a nested loop to store the values in a 2D array
  // Note: There is no difference between ++i and i++ in a for loop.
  //       ++i is more efficient for some C compilers.
  //       ++i vs i++ matters when more than one operation is taking
  //       place on the same line of code e.g. b = ++x; vs b = x++;
  for (int i = 0; i < CITY; ++i){
    fflush(stdout);
    for (int j = 0; j < WEEK; ++j){
      printf("City %d, Day %d: ", i + 1, j + 1);
      fflush(stdout);
      scanf("%d", &temperature[i][j]);
    }
  }
  printf("\nDisplaying values: \n\n");

  // Using a nested loop to display the values of a 2D array
  for (int i = 0; i < CITY; ++i){
    for (int j = 0; j < WEEK; ++j){
      printf("City %d, Day %d = %d\n", i + 1, j + 1, temperature[i][j]);
      fflush(stdout);
    }
  }
  return 0;
}
// C program to find the sum of two matrices of order 2*2

#include <stdio.h>
int main(){
  float a[2][2], b[2][2], result[2][2];

  // Taking input using a nested for loop
  printf("Enter elements of 1st matrix\n");
  fflush(stdout);

  // Note: for loops that have no curly braces only considers the
  //       first statement immediately below the initialization as
  //       being a part of the for loop block. This is similar with
  //       if statements as we will see later on in this code.
  for (int i = 0; i < 2; ++i)
    for (int j = 0; j < 2; ++j){
      printf("Enter a%d%d: ", i + 1, j + 1);
      fflush(stdout);
      scanf("%f", &a[i][j]);
    }


  // Taking input using nested for loop
  printf("Enter elements of 2nd matrix\n");
  fflush(stdout);
  for (int i = 0; i < 2; ++i)
    for (int j = 0; j < 2; ++j){
      printf("Enter b%d%d: ", i + 1, j + 1);
      fflush(stdout);
      scanf("%f", &b[i][j]);
    }


  // adding corresponding elements of two arrays
  for (int i = 0; i < 2; ++i)
    for (int j = 0; j < 2; ++j)
      result[i][j] = a[i][j] + b[i][j];

  // Displaying the sum
  printf("\nSum Of Matrix:");
  fflush(stdout);

  for (int i = 0; i < 2; ++i)
    for (int j = 0; j < 2; ++j)
    {
      printf("%.1f\t", result[i][j]);
      fflush(stdout);

      // Note: Only the first statement directly under the "if"
      //       statement is consider a part of the "if" block.
      //       THis is because there are not curly braces used
      //       with the "if" statement. This is similar to the
      //       for loops scenario mentioned above.
      if (j == 1){
        printf("\n");
        fflush(stdout);
      }
    }

  return 0;
}

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.