-
Representation of Numbers: hexadecimal, binary, floating point
Week 1 readings: Chapter 1.
Generic CPS125 Slides :
PDF
and
PPT.
They are prepared by the course coordinator.
Barak Obama asks America
to learn computer science.
Change The World: join Hour of Code.
Learn how to program using
Garden Robot example.
Try to solve all the five exercises in order. Caution: the 4th and 5th exercises
may require some thinking. Delivered by the Made-With-Code Web project.
Several examples of signed binary/decimal conversion using the two's
complement representation are available from
this Web page. You can also use this online
binary-decimal converter.
-
Introduction to C. Week 2 readings: Chapter 2, Sections 2.1-2.4 and 2.6.
We will review the last 8 slides from Week 1
(floating point numbers, ascii codes, solving problems, implementation).
After that, we will discuss the new slides:
PDF
and
PPT.
These are generic CPS125 slides (prepared by a course coordinator).
The following Web page provides examples of
floating-point conversion from Decimal Floating-Point to
32-bit and 64-bit binary representation. For example, enter
-12 or -14 or -22 then click on the "Not Rounded" button and check the results.
Sample C programs for this week
-
Read Lab Manual
for detailed instructions how to use Quincy-C to write, edit, build and run
computer programs.
-
sample0.c (a short introductory program)
- Experimenting with formatted double placeholder. In the
attached C program samplePrintf.c
we are consecutively printing same double number using different formats.
Try to modify any of these formats using integers of your choice before
and after the decimal point. Once you have changed anything in this C program,
compile it, build executable, and then execute it. Read
Lab Manual
if you need help with Quincy C.
-
You can find another
simple program illustrating printf and scanf functions
here.
-
sample1.c (a longer example illustrating
structure of a C program with several functions).
-
Variables, Expressions and Operators.
Week 3 readings: Chapter 2 (Sections 2.5, 2.7, 2.8),
Chapter 3 (Sections 3.1, 3.2), Chapter 7 (Sections 7.1, 7.2).
We will discuss the slides:
PDF
and
PPT
prepared by the CPS125 course coordinator. The sample programs for this week:
-
readFromFileWithErrors.c
A corrected version:
readFromFile.c
-
redirection.c programs reads data from this file:
data1
-
pointers1.c
-
printingFormats.c
-
pointersPuzzle.c
-
Shortcut operators in C
(slides are prepared by Dr. Kosta Derpanis)
-
A C program to
demo mixed expressions, explicit casting and shortcut operators in C.
-
Week 4 readings: Chapter 4. Logical Operations and Selection Statements.
We will discuss the slides:
PDF
and
PPT
prepared by the CPS125 course coordinator.
- Mathematical functions in C, examples:
math1.c (sin function)
and
math2.c (random numbers generator).
-
Sample programs illustrating logical expressions in C:
logic1.c (error: incorrect comparison)
and
logic2.c (complex comparison of ASCII character codes )
- Conditional execution
A branching program with an error:
branchError.c
Warning: this program has a number of errors (at least 6).
Correct them, compile this program, and then run it.
- A wrong program with a pointer:
pointerError.c
Correct all errors, and then compile this program.
-
Week 5 readings: Chapter 5 (5.1 to 5.3). Repetition and Loops: Part 1.
We will discuss the following slides
PDF
and
PPT prepared by the CPS125 course coordinator.
Project 1
is available. Start working this week!
Submit your project before 11pm on Friday February 17.
Several of the following sample
programs are taken from King's textbook "C programming: a modern approach",
see the link above. Here is the link to
additional sample programs on using loops.
Examples of while-loops
- power2.c
- sumNumbers.c In the condition of
this while-loop, the number 0 is the sentinel. It determines when
the loop has to terminate.
Modify the program above so that it computes an average of all
entered numbers.
- squares.c
Modify the program above so that it prints squares of only odd numbers
which do not divide 5, i.e., those for which remainder from division
by 5 is not 0.
- commission.c
- The fuel tank program
Notice that the termination condition in this while-loop depends on the variable supply that can either decrease or increase between
iterations.
Example of do-while loop
numberDigits.c
Input validation loop (do-while). How would you simplify this program
if the number of different inputs is 4 or less?
In-class exercise: Write a code snippet that computes the
average of the integers of each row in a file.
The first number in each row indicates the number of integer data in the row.
Use input redirection from the file
dataToBeAveraged.txt
If the name of your program is averageEachRow, then you have to
redirect input to this program as
averageEachRow < dataToBeAveraged.txt
-
Week 6 readings: Chapter 5 (5.4 to 5.11). Repetition and Loops: Part 2
We will discuss some of the following exercises
-
Midterm preparation:
Practice writing programs.
Find practice programming exercises in your textbook and
on this Web page. They are similar to questions from
the midterm and final exams. Read
official information about midterm.
Note this midterm exam tests your knowledge of
topics covered in lessons from 2 to 5. The following
review slides prepared by Dr. Kosta Derpanis are strongly recommended
as preparation to the midterm test: read slides from 60 until the end.
In each quiz, try first to solve it yourself, before checking a solution
in the next slide.
On the midterm,
there will be no multiple choice questions, only C programs to write.
You will be developing your C programs on a computer using Geany.
You might wish to install Geany development environment on your Windows
computer at home. Then, you can see that its graphical user interface looks
similar to Quincy. However, there is no C compiler bundled with Geany.
Therefore Geany may not be able to compile and execute C programs on
your computer unless you have already installed a C compiler there.
In any case, installation of any software at home is your
responsibility. The following two links are provided by the CPS125 course
coordinator:
Geany Portable text editor and basic IDE.
Geany Running
on Windows.
Note that the following topics discussed at
the beginning of this course are NOT included in this midterm, but
they will be included in the multiple-choice section on the final exam.
- Convert decimal numbers into signed 32-bit integer
(2s complement for negative numbers)
- Convert the 32-bits signed binary numbers into signed decimal numbers
- Convert the signed decimal numbers into signed floating point binary
(IEEE-754 32-bits) numbers
- Convert the binary signed 32-bit floating point numbers into
signed decimal numbers
- Convert the 8-bit sequences into hexadecimal numbers
-
Reading Week. No classes.
If you did not work on
Project 1, it is a good idea to complete this project yourself.
Practice writing C programs during the reading week. You will need this
skill to pass the midterm test with a high mark.
-
Week 7 and 8 readings: Chapter 3 (3.3 to 3.6). Modular programming and functions.
We will discuss the following slides
PDF
and
PPT
prepared by the course coordinator. We also discussed in class a generic
prototype of a C function.
A couple of the following examples are dicussed in K.N.King's book
"C programming: a modern approach".
Other examples are well-known programming folklore.
- pun.c (from King's book)
- noReturnValue.c
- prime.c (from King's book)
- This is yet another function that has no return value,
but nevertheless it is handy thanks to what it does:
stars.c
Examples of functions called by main() function.
- myAverage1.c
In this example, a new function is defined before main().
In the following example, a new function is defined after main():
myAverage2.c
(a similar example is discussed in King's book).
- Recall two different usage of the "star":
(1) to declare a pointer variable.
For example, int *p; means p is a variable suitable to keep address in memory where an integer number is located.
(2) as the indirection operator which accesses a value stored in memory at an address.
For example, *p = (2 + *p); means take a number stored at address pointed by p, add to this number 2, and save it in the memory location pointed by p .
The following examples demonstrate how to implement a function
that is supposed to return two results. Implement it as a void function
working with pointers to arguments. Using these pointers the function
can modify both: see
swap.c
- Another example of a function with multiple results:
splitSignWholeFraction.c
This is a revised version of the example from slides: the function is void,
but it computes 3 different results by using 3 pointers to variables as its parameters.
- The following slides about
recursion
are prepared by Dr. Kosta Derpanis.
- This example demonstrates how addition and power functions
can be implemented recursively:
recursion.c
-
sample1.c
This is a longer example illustrating structure of a C program with several functions;
one of them, the factorial function, is recursive.
-
Week 9 readings: Chapter 8 (8.1 to 8.6).
Arrays I: Numerical arrays of one dimension.
Passing such arrays to functions (pointers and arrays).
Dynamic allocation of 1D numerical arrays.
We will discuss the following slides
PDF
and
PPT
prepared by the course coordinator. We will also discuss a few examples
and exercises from K.N.King's book
"C programming: a modern approach".
Week 10 readings: Chapter 8 (8.7 to 8.9). Chapter 9.
Arrays II: Multidimensional arrays. Passing such arrays to functions
(pointers and arrays). Dynamic allocation of 2D arrays. Basic strings.
Case studies.
We will discuss the following slides
PDF
and
PPT
prepared by the course coordinator. We will also discuss
a few examples and exercises from K.N.King's book
"C programming: a modern approach".
-
Week 11 readings: Chapter 11 (11.1 to 11.5).
Structures: The typedef construct. Operations on structures.
Structures and functions. Pointers on structures (arrow operator).
We will discuss the following slides prepared by Professor
Kosta Derpanis. Also, we will discuss
the following slides
PDF
( PPT)
prepared by the course coordinator as well as
a few examples and exercises from K.N.King's book
"C programming: a modern approach".
- An example of a program
exampleStructElemSwapWrong.c
that is trying (incorrectly) to swapvalues of elements within
a structure. Explain what is wrong in this implementation.
- C program that maintains an inventory:
inventory.c
This C program uses this
readline.h header file and this
readline.c program to read characters
into a string.
Project 2
is available. Submit your project before 11pm on Friday, March 31st.
-
Week 12 readings: Chapter 12.
Linked Lists. Command line arguments.
We will discuss the following slides prepared by Professor
Kosta Derpanis. Also, we will discuss
the following slides
PDF
and
PPT
prepared by the course coordinator.
-
Week 13: Final Exam review.
We will discuss the following sets of slides prepared by Professor Kosta Derpanis:
Part 1 and
Part 2.
Review
practice questions for Final Exams from previous years.
- pointersPuzzle.c
- This examples demonstrates how to implement a function
that is supposed to return two results as a void function
taking two pointers as arguments:
swap.c
- This example demonstrates how addition and power functions
can be implemented recursively:
recursion.c
- The following program reverses an array of numbers (using pointers)
reverse3.c
- C program that reads strings using different functions:
readString.c
uses scanf, gets fgets with/without replacing \n with \0.