Week 5 Code Examples

#include <stdio.h>

void double_array_of_ints(int data[], int size) {
  for (int i = 0; i < size; i++) {
    data[i] = data[i] * 2;
  }
}

int main(void) {
  int data[5] = {1, 2, 3, 4, 5};
  double_array_of_ints(data, 5);
  // is data doubled?
  for (int i = 0; i < 5; i++) {
    printf("%d\n", data[i]);
  }

  printf("data is: %p\n", data);
  printf("data[0] is: %d\n", data[0]);
  printf("*data is: %d\n", *data);

  printf("data[1] is: %d\n", data[1]);
  printf("*data + 1 is: %d\n", *(data + 1));

  printf("data[2] is: %d\n", data[2]);
  printf("*(data + 2) is: %d\n", *(data + 2));

  *(data + 2) = 50;

  for (int i = 0; i < 5; i++) {
    printf("%d\n", data[i]);
  }
  return 0;
}
#include <stdio.h>

// our main function now accepts two parameters
int main(int argc, char *argv[]) {
  // argc -> an integer representing how many arguments were passed in
  // argv -> the actual array of strings of arguments (the data passed in)

  printf("There were %d args passed in\n", argc);

  // loop through each argc and access the element
  int i = 0;
  while (i < argc) {
    printf("Argument %d is: ", i + 1);
    fputs(argv[i], stdout);
    printf("\n");
    i++;
  }

  //   do my copy work...
  // code code code

  if (argv[0] == "mv") {
    // delete the source file
  }

  return 0;
}
#include <stdio.h>

#define ROWS 6
#define COLS 7

enum tile { EMPTY, RED, YELLOW };

void print_board(enum tile grid[ROWS][COLS]);
void place_tile(enum tile grid[ROWS][COLS], int col, enum tile turn);

int main(void) {
  enum tile grid[ROWS][COLS] = {};

  print_board(grid);

  // loop and alternate drops
  int input_col;
  int i = 0;
  while (scanf("%d", &input_col) == 1) {
    // when it's even, it's Y
    // when it's odd, it's R

    if (i % 2 == 0) {
      place_tile(grid, input_col, YELLOW);
    } else {
      place_tile(grid, input_col, RED);
    }
    print_board(grid);
    i++;
    printf("\n");
    printf("Enter your col: ");
  }

  return 0;
}

// pass in the grid, where the drop is, and who is dropping
void place_tile(enum tile grid[ROWS][COLS], int col, enum tile turn) {
  // star row at the highest row level (5)
  int row = ROWS - 1;
  while (grid[row][col] != EMPTY) {
    row--;
  }
  // here, it's either EMPTY or FULL COL
  if (row == -1) {
    // it was full
  } else {
    grid[row][col] = turn;
  }
}

void print_board(enum tile grid[ROWS][COLS]) {
  for (int i = 0; i < ROWS; i++) {
    for (int j = 0; j < COLS; j++) {
      char print_char;
      if (grid[i][j] == EMPTY) {
        print_char = '0';
      } else if (grid[i][j] == RED) {
        print_char = 'R';
      } else {
        print_char = 'Y';
      }
      printf("%-4c", print_char);
    }
    printf("\n");
  }
}
#include <stdio.h>
#include <stdlib.h>

int main(void) {
  char age_str[] = "5";

  //   this is an error, we can't do this...
  //   age_str - 1;

  //   so we need to convert it to an int
  int age = atoi(age_str);

  //   now we can do int things with it
  age--;

  printf("%d\n", age);

  return 0;
}
int main(void) {
  
  // create an integer in memory called i
  // the OS goes off, requests the memory and gives it to us
  // we don't deal with any addresses!!!
  // instead, we use our identifier in-code to refer back
  int i;
  for (i = 0; i < 5; i++) {
    // ...
  }

  //   i does not exist here
  return 0;
}
#include <stdio.h>

void change_value(int *x) { 
    *x = *x * 2;
}

int main(void) {
  int x = 5;
  change_value(&x);
  printf("%d\n", x);

  return 0;
}
int main(void) {

    // & -> when placed before a variable, this means address-of
    // int x;
    // &x; -> address_of

    // * -> when placed after a type in a new variable declaration, it means create a pointer
    // int *x; -> pointer to an int

    // * -> when placed before a variable, means dereference
    // *x -> dereference

    return 0;
}
#include <stdio.h>

int main(void) {
    int age = 50;
    printf("age is: %d\n", age);
    // we print an address with %p -> (p for pointer)
    // &i -> address of variable that follows
    printf("age is stored at: %p\n", &age);

    int *age_ptr = &age;

    // HOW DO I PRINT THE VALUE!?
    printf("%d\n", *age_ptr);

    return 0;
}
#include<stdio.h>

struct student {
    int age;
    double WAM;
};

int main(void) {

    // how many bits/bytes
    // 4 bytes, 32 bits
    int x = 5;
    int *x_ptr;
    printf("size of x is: %d bytes\n", sizeof(x));
    printf("size of x_ptr is: %d bytes\n", sizeof(x_ptr));

    // how many bits/bytes
    // 8 bytes, 64 bits
    double y = 5.6;
    printf("size of y is: %d bytes\n", sizeof(y));
    
    double *y_ptr;
    printf("size of y_ptr is: %d bytes\n", sizeof(y_ptr));

    // how many bits/bytes
    // 12 bytes, 96 bits
    struct student my_student = {25, 88.3};
    printf("size of a struct student is: %d bytes", sizeof(struct student));

    return 0;
}