Week 3 Code Examples

/*
z3534499
A program which reads from the user, into an array
and prints the results out using loops
*/
#include <stdio.h>
#define NUM_ELEMENTS 7

int main(void) {
  // 0-out my array
  int should_count_sandwhich[NUM_ELEMENTS] = {0, 1, 0, 1, 0, 0, 0};
  int sandwiches_made_per_lecture[NUM_ELEMENTS] = {};

  // first, read in the ints in a loop
  int i = 0;
  while (i < NUM_ELEMENTS) {
    printf("Enter week %d's sandwiches: ", i + 1);
    scanf("%d", &sandwiches_made_per_lecture[i]);
    i++;
  }

  // print out the sandwiches made
  i = 0;
  while (i < NUM_ELEMENTS) {
    printf("Week %d, %d sandwiches.\n", i + 1, sandwiches_made_per_lecture[i]);
    i++;
  }

  // average out the number of sandwiches eaten
  // average, we sum then divide by n
  // get some stats
  i = 0;
  int sum = 0;
  int t_day_sum = 0;
  while (i < NUM_ELEMENTS) {

    if (should_count_sandwhich[i] == 1) {
        t_day_sum += sandwiches_made_per_lecture[i];
    }

    sum += sandwiches_made_per_lecture[i];
    i++;
  }
  double average_sandwiches = sum / NUM_ELEMENTS;
  printf("Average sandwiches eaten = %lf\n", average_sandwiches);
  printf("Total sandwiches eaten = %d\n", sum);
  printf("Total sandwiches eaten on T days = %d\n", t_day_sum);

  return 0;
}
#include<stdio.h>

int main(void) {

    // creates a single int!
    // int temperature;

    // int array of 7 elements
    // 32bit * 7 elements = 224 bits
    //                      0   1  2   3  4   5  6
    int temp_per_week[7] = {5, 10, 12, 6, 12, 5, 10};

    printf("The temperature is: %d\n", temp_per_week[3]);
  
    return 0;
}
#include <stdio.h>

#define NUM_ELEMENTS 10

int main(void) {
  int ice_cream_per_day[NUM_ELEMENTS] = {3, 2, 1, 2, 1, 3, 5};

  // my array with 10 elements (NUM_ELEMENTS SIZED)
  // this will error
  // I am accessing data outside the length of my array
  printf("%d\n", ice_cream_per_day[10]);

  // read/print each element
  // BAD
  printf("PRINTING BAD WAY:\n");
  printf("%d\n", ice_cream_per_day[0]);
  printf("%d\n", ice_cream_per_day[1]);
  printf("%d\n", ice_cream_per_day[2]);
  printf("%d\n", ice_cream_per_day[3]);
  printf("%d\n", ice_cream_per_day[4]);
  printf("%d\n", ice_cream_per_day[5]);
  printf("%d\n", ice_cream_per_day[6]);

  printf("PRINTING COOL WAY:\n");

  // GOOD
  int i = 0;
  while(i < NUM_ELEMENTS) {
    // printf("i is: %d\n", i);
    printf("%d\n", ice_cream_per_day[i]);
    i++;
  }

  return 0;
}
#include<stdio.h>

enum weekday {
    MON, TUE, WED, THU, FRI
};

// function prototypes
void print_weekday(enum weekday day);
enum weekday get_weekday(void);
int get_my_number(void);

int get_my_number(void) {
    int result = 5;
    result += 5;

    return result;
}

// Scans in an int from user and returns a mapped weekday
enum weekday get_weekday(void) {
    enum weekday day;
    printf("Enter day of week (Mon = 0):" );
    scanf("%d", &day);
    return day;
}

// create a day of week enum
// as the user for the day of week
// return it via a function
int main(void) {
    enum weekday day = get_weekday();
    print_weekday(day);

    printf("%d\n", get_my_number());
   
    return 0;
}

// prints the weekday
void print_weekday(enum weekday day) {
    if (day == MON) {
        printf("Monday\n");
    } else if (day == TUE) {
        printf("Tuesday\n");
    } else if (day == WED) {
        printf("Wednesday\n");
    } else if (day == THU) {
        printf("Thursday\n");
    } else if (day == FRI) {
        printf("Friday\n");
    }
}