Skip to content

Exercise 0: Echo, Divide, Quadratic

This is your first programming exercise. An exercise is something that you do on your own. You can submit them but it will not be graded. Test cases are provided for the exercises so that you can test and check on your own if your code is correct. This is in contrast to an assignment, where you need to submit for grading and credits.

Prerequisite

Learning Outcomes

  • Be comfortable writing simple C programs that involve arithmetic operations, long and double types, and standard I/O.

One-Time Setup

Before going into their first programming exercise, you need to do a one-time setup of your account on PE. You need to create a file called .gitconfig in your home directory and with the following content:

1
2
3
4
5
[user]
  name = Your Name
  email = Your Email
[github]  
  user = Your GitHub ID

Your email should be whatever you used to sign up Github.

For example, a sample .gitconfig looks like this:

1
2
3
4
5
[user]
  name = Elsa
  email = queen@arendelle.gov
[github]  
  user = elsasnow16

After saving this file, run:

1
git config --get github.user

It should return your GitHub user id.

It should print your GitHub user id as already set. If there is a typo, you need to edit .gitconfig again and reload it by repeating the command above.

Setup

1
~cs1010/get-ex00
  • You should see a new subdirectory ex00-<githubid> in your current working directory, where githubid is your GitHub ID.
  • We will call this directory your exercise directory or assignment directory.

  • Inside that directory, you should see a bunch of files:

    • echo.c, divide.c, and quadratic.c are the most important files. They are the skeleton C code that you should edit to solve the exercise.
    • inputs and outputs are subdirectories that contain test inputs and test outputs. We use the convention problem-name.test-id.in for input test data, and problem-name.test-id.out for output test data. So, you will see echo.1.in, divide.1.out, etc. The expected output for echo.1.in is in echo.1.out. You can look at the content of these files if you wish (which UNIX command should you use to do this?). You can edit these files to change the test input and output.
    • Makefile: The configuration for the tool make that we use to automate the compilation and testing of the programs. You do not have to understand how to write a Makefile for CS1010. If you are interested to learn how to write a Makefile, talk to either Wei Tsang or Google.
    • test.sh: A bash script for testing your code. You do not have to edit this file nor call it directly. It is invoked by make. If you are interested to learn how to write bash script, talk to either Wei Tsang or Google.

Identifying Yourself

In every C file that you submit to CS1010, you need to identify yourself by writing your name and tutorial group. Marks will be deducted if you fail to do so. You need to edit the line:

1
@author XXXX (Group YYYY)

and change it to something like:

1
@author Elsa of Arendelle (Group B10)

Solving The Assignments

  • Edit the files echo.c, divide.c, and quadratic.c to solve the corresponding question as described below.
  • To compile and run the given tests with the sample inputs and outputs, run on the command line,
1
make

This command will compile both C files. If there is no compilation error, it will run the test scripts.

Submission

When you are ready, run the following command while you are in the exercise directory:

1
~cs1010/submit-ex00

The files add.c, divide.c, and quadratic.c will be uploaded to GitHub. You can submit multiple times.

Grading

This assignment is not graded.


Question 1: Echo

Write a program echo (source file echo.c) that reads in an integer and print that integer to the standard output.

Sample run:

1
2
3
4
5
6
ooiwt@pe111:~/ex00-ooiwt$ ./echo
123
123
ooiwt@pe111:~/ex00-ooiwt$ ./echo
-1
-1

The text ooiwt@pe111:~/ex00-ooiwt$ is the command prompt. Yours will look different, of course. echo is the executable you created. The next line, 123, is the input you provide. Press enter after the input. 123 is the output printed by echo.

Question 2: Divide

Write a program divide (source file divide.c) that reads in two integers, \(x\) and \(y\), and print the value of \(x\) divided by \(y\). You can assume that \(y\) is never 0.

Sample run:

1
2
3
4
5
6
ooiwt@pe111:~/ex00-ooiwt$ ./divide
10 2
5.0000
ooiwt@pe111:~/ex00-ooiwt$ ./divide
1 2
0.5000

Question 3: Quadratic

Write a program quadratic (source file quadratic.c) that reads in three integers, \(a\), \(b\), and \(c\), that represent the quadratic equation \(ax^2 + bx + c = 0\), and prints its two roots. Assume that \(a \not = 0\) and \(b^2 > 4ac\). Recall that the roots are:

\[ \frac{-b \pm \sqrt{b^2 - 4ac}}{2a} \]

Print the root \(\frac{-b + \sqrt{b^2 - 4ac}}{2a}\) first on one line, followed by the other root \(\frac{-b - \sqrt{b^2 - 4ac}}{2a}\) on the next line.

Sample run:

1
2
3
4
5
6
7
8
ooiwt@pe111:~/ex00-ooiwt$ ./quadratic
1 -2 1
1.0000
1.0000
ooiwt@pe111:~/ex00-ooiwt$ ./quadratic
2 -7 3
3.0000
0.5000