COL100 lecture notes: 20-21 Sem 2
Instructors: Ragesh Jaiswal and Aaditeshwar Seth
You will learn the following aspects in the course:
We hope that with these skills and strong concepts, you will be able to learn how to use new tools on your own, write your own programs to solve basic computation requirements in your disciplines, and hopefully learn new programming languages on your own.
- What is computing, translation from a problem to an algorithm to a program
- How a computer works, how is a program compiled and executed by a computer, binary numbers, computing using 0s and 1s
- Programming in C. We are choosing C because it is close to the metal and will help you relate basic programming constructs to how they are implemented in a computer
- The importance of good algorithms, how the same problem can be solved in different ways with different degrees of efficiency
- Writing moderately complex programs. Planning first on paper how the program should be structured, and then coded in a programming language
Key reference book for C programming: Programming in C, by Stephen G. Kochan. Available online here.
Thanks to Prof. Partha Pratim Das from IIT Kharagpur for his original powerpoint slides that we have adapted for this course.
Lecture hours: Mondays and Thursdays, 9:30-11:00am, delivered on Impartus.
The lab assignments and lab schedule can be found here. Assignments will be given weekly. We will use Piazza (class code = COL100) for discussions and Gradescope (class code = 86X7JZ) for assignment submissions.
Tentative dates for the 4 lab exams are as follows:
Course policies: Read here.
- Lab exam 1: March 27th, 3:30-5:00pm
- Lab exam 2: April 25th, 3:30-5:00pm
- Lab exam 3: May 22nd, 3:30-5:00pm
- Re-minor/re-lab exam: May 29th, 3:30-4:30pm
Class 1, 2: Introduction to computing and computers [slides]
Class 3, 4: Number systems, data types, and expression evaluation [slides]
- The datatypes.c, places.c, and round.c programs we used to understand type casting. You can also take a look at Chapter 4 from the Kochan book.
- The printmaxvals.c program we discussed in the tutorial to check for overflow when adding or subtracting two integer numbers.
- If you are curious about how mathematical functions are implemented, such as the ones in sqrt.c, take a look at the glibc source code such as for the sqrt 32-bit floating point implementation on the IEEE-764 architecture. Browse around and read the comments to understand different implementation methods on different architectures and for different precisions.
- Somebody came up with this interesting example with increment operators, and we understood what was going on by looking at the assembly language code to which the program was compiled by gcc. Details here.
Class 5, 6: Conditionals [slides]
Class 7, 8, 9: Loops [slides]
Class 10, 11: Number representation and bit operations [slides]
- Consult Chapter 12 in the Kochan book for bit operations
- Programs we discussed in class, to print the binary representation of a number, and the hexa representation, through bit operations. A twos-complement implementation to understand how negative numbers are stored and re-converted when to be displayed.
- With some pointer type conversations, we can also print the floating point representation of a number. Test with loss of precision when you go beyond 2^23.
- Floating point numbers have a representation for infinity. Check out this program with a division by zero with floats and integers - when does the program crash?
Class 12, 13: Arrays [slides]
- You can also take a look at Chapter 7 in the Kochan book
- Programs we discussed in class, to show the histogram of a series of numbers, and to create sample inputs using random number generators. We also looked at redirection of input and output, and some nice tools like cat, head, and tail.
- Updated program to generate random numbers with a random seed.
- A simulation for forest fires in a 1D forest. Run the program for different values of fire and growth probabilities, and see whether the forest is able to sustain itself. Also try to simulate a circular forest, i.e. the left boundary of the forest connects with the right boundary. Later when we study 2D arrays, you can try changing the program to a 2D forest, and a circular 2D forest which is called a toroid.
Class 14: 2D arrays and strings
- The matrix multiplication program we understood in class.
- The fun game of life program, with sample inputs for a glider, gun, and other shapes. If you are interested, you can check out more ANSI control characters here, note that you would substitute the escape character ^[ with \033 when using it in a printf in C, e.g. printf("\033[2J") to clear the entire screen (code is ^[[2J). You can also print in colour as described here, with common codes here.
- Here is also a 1D version of game of life and a sample input file.
- Strings are nothing but a 1D array of characters, terminated with a null '\0' character (decimal value 0). The program we discussed in class. ASCII codes 0..31 are control codes.
- Also check out the initial portion of Chapter 10 from the Kochan book.
Class 15, 16: Functions [slides]
- The simple programs we used to demonstrate passing by value in function calls, and passing by reference for manipulating arrays.
- The GCD program we did in class, to read arguments from the command line, convert the string arguments to integers, and compute the GCD of a series of integers.
- Also look up Chapter 8 from the Kochan book
Class 17, 18, 19, 20: Recursion, sorting, searching [slides]
- Variants of the fibonacci program discussed in class.
- Also look up Chapter 8 from the Kochan book
Class 21, 22: Pointers [slides]
Class 22: Structures [slides]
Extra: Dynamic memory allocation [slides]
- Also look up Chapter 17 from the Kochan book
- An implementation of command line parsing: trim spaces, count the number of strings, allocate an array of pointers, allocate space for each string linked from the array of pointers.