TA name: Brian Chen

Tutorial TUT0005: Thursday 9-10AM, at IC220.

Email Policy:
Before emailing me, consider attending my practical or posting the question on Piazza. My email address is brianyx.chen[at]mail.utoronto.ca. When emailing me, use your utoronto email and in the subject include "CSCA48".

Expectations:

• Material covered in tutorial sessions are dependent on lecture material, so please be familiar with the content.
• There may be a quiz held in tutorial, so please be on time.

Weekly Notes:   Note - these are not a replacement, but instead a supplement for weekly tutorials

The tutorial this week covered arrays (1D and 2D), array pointer notation, and calloc/free.
We talked about the differences between static (int arr) and dynamicly sized arrays (int arr[y]). We wrote some code to create a pointer array:
int *newArray(int size){
int *array; // A pointer! not an array declaration
array=(int *)calloc(size,sizeof(int)); // Get 'size' items large enough to hold one int each
return array;
}

We discussed the difference between array index notation arr vs array pointer notation *(arr + 10). We talked the fact that arrays must be contiguous - continuous and sequential. If there is not enough memory space, calloc returns NULL. We talked about the need to use free(my_array) and how it pertains to pointer arrays. We discussed the importance of 2D arrays and how they're just stored as large 1D arrays.

Finally we discussed how we approach pointer notation to 2D arrays in C - remember they're in Row-Major notation!
// For array[i][j] the pointer notation equivalent is:
*(array + i +(j*row_size));
The tutorial this week covered structs and their usages.
We covered the differences between a struct (a compound/composite data type, called CDT in notes) and an object.

We went over how to initialize a struct - disc is the object name, Record is the declarable object. An example below:
typedef struct disc{
char name;
int pub_year;
char author;
} Record;

We wrote some basic functions to work with structs. We also talked about accessing members of CDTs through (*struct).property (bad) vs struct->property (good).
void set_record_info(Record *rec, char *name, char *author, int year){
strcpy(rec->name, name);
strcpy(rec->author, author);
rec->pub_year = year;
}
We briefly discussed the difference and use cases between fgets() and scanf().

We also discussed a solution to a problem about: A program which creates a 10 record array, then uses fgets() and scanf() to fill out all records using a function.
The tutorial this week covered strings and basic pointer logic in C.
We covered the differences between:
p = &x;     // Take the address of x and store it in pointer p
*(p) = y;   // Make the contents of the locker (p) equal to y
*(p+2) = z; // Make the contents of locker (p+2) equal to z
y=*(p+5);   // Make y equal to the contents of locker (p+5)
m=*(p)+9;   // Take contents of locker p, increase by 9, store in m

We wrote some basic functions, like strConcat, concatenates new to end of orig:
void strConcat(char *orig, char *new){
while(*orig){
orig++;
}
while(*new){
*orig = *new;
orig++;
new++;
}
*orig = '\0';
}
We also discussed a solution to a problem about: A function which takes a pointer to a string, and a pointer to an array of ints. It counts how many times each character appears in the input string and updates that count in the corresponding entry of the array

We motivated the answer by looping through the string and taking the char-to-int value (ASCII) and storing/incrementing it in the 255 char array. For example, 'H' would be 71. Code was left for the reader to complete.

We also introduced a club - the Computer Science Enrichment Club. Come join us if you're not busy!
The tutorial this week covered how to identify warnings and errors in C.
To avoid nasty undefined behavior such as what we get when we do something like:
float f = 3.14159;
printf("%d \n", f);
The above code will spit out some random number like -394815815, which is not expected behavior. To avoid this, we explained the use of several Flags in our compilation of C code for this course.

This includes two new items that we need to add:
• -Wall short for "warn all" - it turns on (almost) all the warnings that gcc can tell you about
• -Werror every warning is treated as an error and halts compilation
So now for the rest of the course we expect you to compile all your solutions using:
gcc -Wall -Werror program.c
We then solved Ex.0 and Ex.2 from Unit 2 Notes together, discussing the problems and their solutions.
The tutorial this week covered basics in C such as loops, conditionals, comparison operators, and how to use printf in C.
We discovered that the | and & operators are bitwise operators - meaning they actually take the bit-by-bit of two variables (e.g. 2 = 00000010). We then discussed how the || and && are logical operators - meaning that they act the same as (a or b) and (a and b) in Python. Do not use single operators in place of double operators . We also briefly touched upon the fact that anything not 0 is True in C.

We examined the behavior of these different operators through some code:
int a = 2;
int b = 0;
int c = 7;
if((a=b && (c=3) & b==0)){
printf("Printed \n");
}
else{
printf("Not printed \n");
}
return 0;
We also created a program to print all the perfect squares from [1..100] in C:
int is_square(int num){
double ans = sqrt((double)num);
int int_ans = (int) ans;
if(ans == int_ans){
return sqrt(num);
}
return 0;
}

int main(){
for(int i = 1; i < 101; i++){
int ans = is_square(i);
if(ans != 0){
printf("%d = %d * %d \n", i, ans, ans);
}
}
}
There were no tutorials this week.
Instead here's a link to a funny comic - Dilbert Programming Environment.

We will be using C for this course. Consider familiarizing yourself with the C language and get started by downloading a text editor (I recommend Notepad++ or Sublime Text for beginners, and Visual Studio Code for more advanced students). Try to understand how to write and run C code (most text editors have syntax highlighting, which is useful!) - it's quite a bit different than Python. Read Paco's notes on the Quercus site for details.

Looking to store your code? Try not to use Google Drive or Dropbox, instead, consider checking out Git - a version control system widely used by software engineers. I recommend setting up a GitHub (Git hosting service) account - GitHub offers a free student pack loaded with goodies here. Git is a storage system for your code, being able to revert to previous versions, accessed from any computer, and loads of other amazing functionality that makes developing much easier.