CS107: Computer Programming

Announcements

Textbook

Grades

Course Schedule

 WeekChaptersExams
11433/10/14 - 1432/10/18  
21433/10/21 - 1433/10/25Chap. 1: Introduction to Computers 
31433/10/28 - 1433/11/03Chap. 2: Introduction to C++ Programming 
41433/11/06 - 1433/11/10Chap. 3: Control Statements: Part 1 
51433/11/13 - 1433/11/17Chap. 3: Control Statements: Part 1 
61433/11/20 - 1433/11/24Chap. 4: Control Statements: Part 2 
71433/11/27 - 1433/12/01Chap. 4: Control Statements: Part 2 
81433/12/04 - 1433/12/08 Holydays
91433/12/11 - 1433/12/15 Holydays
101433/12/18 - 1433/12/22Chap. 4: Control Statements: Part 2 
111433/12/25 - 1433/12/29Chap. 5: Functions and Recursion
Quiz 1 - Monday 27/12/1433
121434/01/03 - 1434/01/07Chap. 5: Functions and Recursion
Midterm - Monday 05/01/1434 - 15:15
131434/01/10 - 1434/01/14Chap. 6: Arrays
Lab Exam 1
141434/01/17 - 1434/01/21Chap. 6: Arrays
Project Proposal
151434/01/24 - 1434/01/28Chap. 6: Arrays
Quiz 2 - Monday 26/01/1434
161434/02/02 - 1434/02/06Chap. 7: Pointers
Lab Exam 2
171434/02/09 - 1434/02/13Chap. 7: Pointers
Project Validation
181434/02/16 - 1434/02/20 Finals
191434/02/23 - 1434/02/27 Finals

Grading

Slides

Chapter 1 Introduction to Computers [ppt]
Chapter 2 Introduction to C++ Programmming [ppt]
Chapter 3 Control Statements: Part 1 [ppt]
Chapter 4 Control Statements: Part 2 [ppt]
Chapter 5 Functions and an Introduction to Recursion [ppt]
Chapter 6 Arrays [ppt]
Chapter 7 Pointers [ppt]

Dev C++

Solutions

Chapter 2

Exercise 2.19

Exercise 2.21

Exercise 2.23

Exercise 2.24

Exercise 2.25

Exercise 2.28

Exercise 2.30

Past Exams

Midterm Exams

Exams

Quizzes

Midterm Exam

Project
[Due Date: Sunday 24/02/1434]

Battleship is a two-player game where each player deploys his ships secretly on a 10x10 square grid. The ships have different sizes varying from 2 to 5 squares. The grid cells are identified by a letter (column) and a number (row). Each player shoots at the other grid by specifying a cell. There is: The game is over if one player sinks all other player ships. The player shoots again if there is a Hit or a Sink. On a Miss, it will be the turn of the other player.

The goal of the project is to develop a one-player Batteship game where the ships will be randomly deployed by the computer. The player will specify the cell to the computer. The computer will repond with a Hit, Miss, or Sink. The computer should count the number of misses. The game is over when the player sinks all ships or the number of misses is above a giving threshold (25 for our case).

The application should:

  1. Check the player input
  2. Display and update the current state of the grid
  3. Indicate the number of misses
  4. Terminate the game.

Files:

Exercises

Chapters 3 and 4

Exercise 1 [ Solution: Unix - Windows ]

Write a program that computes the sum of integers from 0 to n.

Exercise 2 [ Solution: Unix - Windows ]

Write a program that computes the sum of even integers from 0 to n.

Exercise 3 [ Solution: Unix - Windows ]

Write a program that computes the product of integers from 1 to n.

Exercise 4 [ Solution: Unix - Windows ]

Write a program that computes the greatest common divisor (GCD) of two integers.

Exercise 5 [ Solution: Unix - Windows ]

Write a program that computes the least common multiplier (LCM) of two integers.

Exercise 6 [ Solution: Unix - Windows ]

Write a program that computes the sum and the average of n integers.

Exercise 7 [ Solution: Unix - Windows ]

Write a program that determines the maximum and the minimum of n integers.

Exercise 8 [ Solution: Unix - Windows ]

Write a program that determines whether an integer is prime or not.

Exercise 9 [ Solution: Unix - Windows ]

Write a program that computes the sum of prime numbers smaller than n.

Exercise 10 [ Solution: Unix - Windows ]

Write a program that prints the grade of a student based on the sum of his marks (by using if statement).

Exercise 11 [ Solution: Unix - Windows ]

Write a program that prints the grade of a student based on the sum of his marks (by using switch/case statement).

Exercise 12 [ Solution: Unix - Windows ]

Write a program that computes the sum of digits of a given integer n.

Exercise 13 [ Solution: Unix - Windows ]

Write a program that prints an integer n in the reverse order (7163 => 3617).

Exercise 14 [ Solution: Unix - Windows ]

Write a program that counts the number of digits in a given number n.

Exercise 15 [ Solution: Unix - Windows ]

Write a program that determines whether a number is composed only of even digits or not.

Exercise 16 [ Solution: Unix - Windows ]

Write a program that prints a full square on the screen (side = n).

Exercise 17 [ Solution: Unix - Windows ]

Write a program that prints an empty square on the screen (side = n).

Exercise 18 [ Solution: Unix - Windows ]

Write a program that prints a full rectangle on the screen (length = l, width = w).

Exercise 19 [ Solution: Unix - Windows ]

Write a program that prints an empty rectangle on the screen (length = l, width = w).

Exercise 20 [ Solution: Unix - Windows ]

Write a program that prints an empty square with diagonals on the screen (side = n).

Exercise 21 [ Solution: Unix - Windows ]

Write a program that prints an closed envelope on the screen (side = n).

Exercise 22 [ Solution: Unix - Windows ]

Write a program that prints an opened envelope on the screen (side = n).

Exercise 23 [ Solution: Unix - Windows ]

Write a program that prints an empty diamond on the screen (side = n).

Exercise 24 [ Solution: Unix - Windows ]

Write a program that prints an full diamond on the screen (side = n).

Chapter 5

Exercise 5.1 [ solution ]

Write a function that computes the sum of integers from 0 to n.

Exercise 5.2 [ solution ]

Write a function that computes the sum of even integers from 0 to n.

Exercise 5.3 [ solution ]

Write a function that computes the product of integers from 1 to n.

Exercise 5.4 [ solution ]

Write a function that computes the greatest common divisor (GCD) of two integers.

Exercise 5.5 [ solution ]

Write a function that computes the greatest common divisor (GCD) of two integers using the Euclidean algorithm (GCD(a, 0) = 0, GCD(a, b) = GCD(a, a%b)).

Exercise 5.6 [ solution ]

Write a function that computes the least common multiplier (LCM) of two integers.

Exercise 5.7 [ solution ]

Write a function that computes the sum and the average of 3 integers.

Exercise 5.8 [ solution ]

Write a function that determines the maximum and the minimum of 3 integers.

Exercise 5.9 [ solution ]

Write a function that prints a full square on the screen (side = n).

Exercise 5.10 [ solution ]

Write a function that prints an empty square on the screen (side = n).

Exercise 5.11 [ solution ]

Write a function that prints an empty square with diagonals on the screen (side = n).

Exercise 5.12 [ solution ]

Write a function that prints an closed envelope on the screen (side = n).

Exercise 5.13 [ solution ]

Write a function that prints an opened envelope on the screen (side = n).

Exercise 5.14 [ solution ]

Write a function that prints an empty diamond on the screen (side = n).

Exercise 5.15 [ solution ]

Write a function that prints an full diamond on the screen (side = n).

Exercise 5.16 [ solution ]

Write a function that determines whether an integer is prime or not.

Exercise 5.17 [ solution ]

Write a function that determines the greatest prime number little than n.

Exercise 5.18 [ solution ]

Write a function that determines the smallest prime number greater than n.

Exercise 5.19 [ solution ]

Write a function that computes the sum of digits of a given integer n.

Exercise 5.20 [ solution ]

Write a function that prints an integer n in the reverse order (7163 => 3617).

Exercise 5.21 [ solution ]

Write a function that computes the reverse number of a given integer n (7163 => 3617).

Exercise 5.22 [ solution ]

Write a function that computes the units, the tens, and the hundreds of a given number n.

Exercise 5.23 [ solution ]

Write a function that counts the number of digits in a given number n.

Exercise 5.24 [ solution ]

Write a function that determines whether a number is composed only of even digits or not.

Exercise 5.25 [ solution ]

Write a function that prints the grade of a student based on the sum of his marks (by using if statement).

Exercise 5.26 [ solution ]

Write a function that prints the grade of a student based on the sum of his marks (by using switch/case statement).

Exercise 5.27 [ solution ]

Write an iterative function that computes n!.

Exercise 5.28 [ solution ]

Write a recusive function that computes n!.

Exercise 5.29 [ solution ]

Write a recursive function that computes the greatest common divisor (GCD) of two integers using the Euclidean algorithm (GCD(a, 0) = 0, GCD(a, b) = GCD(a, a%b)).

Exercise 5.30 [ solution ]

Write an iterative function that computes xy.

Exercise 5.31 [ solution ]

Write a recursive function that computes xy.

Exercise 5.32 [ solution ]

Write a recursive function that displays the solution for Hanoi towers with n disks.

Exercise 5.33 [ solution ]

Write a function that returns a random number between 1 and 6 using rand() function.

Exercise 5.34 [ solution ]

Write a function that returns the area and the perimeter of a rectangle.

Chapter 6

Exercise 6.1 [ solution ]

Write a function that reads an array of n integers.

Exercise 6.2 [ solution ]

Write a function that displays an array of n integers.

Exercise 6.3 [ solution ]

Write a function that returns the maximum of an array of n integers.

Exercise 6.4 [ solution ]

Write a function that returns the minimum of an array of n integers.

Exercise 6.5 [ solution ]

Write a function that returns the second maximum of an array of n integers.

Exercise 6.6 [ solution ]

Write a function that computes the sum of the elements of an array of n integers.

Exercise 6.7 [ solution ]

Write a function that sorts an array of n integers.

Exercise 6.8 [ solution ]

Write a function that returns the median of an array of n integers.

Exercise 6.9 [ solution ]

Write a function that performs a p circular right shifts on an array of n integers.

Exercise 6.10 [ solution ]

Write a function that performs a p circular left shifts on an array of n integers.

Exercise 6.11 [ solution ]

Write a function that returns the position of an integer p in an array of n integers.

Exercise 6.12 [ solution ]

Write a function that returns the number of occurences of an integer p in an array of n integers.

Exercise 6.13 [ solution ]

Write a function that returns the most frequent number in an array of n integers.

Exercise 6.14 [ solution ]

Write a function that returns the least frequent number in an array of n integers.

Program 1 [ solution ]

The above program performs the following operations:
  1. reads an array of integers
  2. computes the maximum of an array
  3. computes the second maximum of an array
  4. sorts an array
  5. displays an array

Program 2 [ solution ]

The above program performs the following operations:
  1. reads an array of integers
  2. removes duplicates by replacing them by -1
  3. shifts -1 to the right of the array
  4. performs a circular shift on the array
  5. displays an array

Program 3 [ solution ]

The above program performs the following operations on strings:
  1. strlen
  2. strcpy
  3. strncpy
  4. strcmp
  5. strncmp
  6. strcat
  7. strncat


Last Modified: 1434-02-06