Classwork 4: Expressions


practice with arithmetic expressions and more practice using printf() and scanf()


Step 1: Submit "Something"

As per class policy (see Syllabus), you must be in class to submit classwork. If you just submit to "finish" outside of class, it doesn't count. You MUST submit something during class.

  1. Use the Unix cat command to create a file called secret:

       cat >secret

    Don't forget the > symbol before secret.
  2. Type in the secret number written on the board and press ctrl-D to end the cat command.
  3. Check that the secret number was saved. Type cat secret
  4. Now, submit the secret file to cw04:

       submit cs104_chang cw04 secret

Step 2: Linear Equations

Note: this is a program to help middle schoolers do their math homework.

In algebra, a linear equation written in the form:

ax + by = c

is said to be in standard form. Another common form is the slope-intercept form:

y = mx + d

You can easily convert the standard form to the slope-intercept form, using:

m = −a/b
d = c/b

Your assignment is to write a program that asks the user for a linear equation in standard form and convert it to slope-intercept form. You will ask the user for the constants a, b and c and report the values of m and d.

Finally, you will let the user check the program's answer by specifying a point x. The program then displays the computation of y using the slope-intercept form and plugs y back into the standard-form equation and displays the computation that the two sides are equal.

A sample run of your program might look like the following. (The user's responses are in orange.)

PT[130]% gcc -Wall slope.c

PT[131]% ./a.out
Specify your linear equation in standard form: a * x + b * y = c
Enter a: -4
Enter b: 2
Enter c: 8

In slope-intercept form, your equation is:

   y = 2.000000 * x + 4.000000

Let's check our answer. Enter an x: 101

Using the slope intercept form:

   y = 2.000000 * 101.000000 + 4.000000
   = 202.000000 + 4.000000
   = 206.000000

Let's check if this is consistent with the original equation

a * x + b * y
   = -4.000000 * 101.000000 + 2.000000 * 206.000000
   = -404.000000 + 412.000000
   = 8.000000

Is this equal to c = 8.000000?


  1. Please name your C program slope.c

  2. You can use any of the programs we have seen in class as a starting point. These are on the topics page. You might also use your previous classwork as a starting point.

  3. Remember that you must declare a variable before you use it. For this program, all of your variables should have type double.

  4. Implement your program in steps. So, you always have a working version that does something. For this assignment, make sure that the first part that computes the slope-intercept form is working before you do the "check your answer" part.

What to submit

Use the script command to record yourself compiling your program and running your program 3 times. Use exit to terminate the recording. Then submit your program and typescript file:

submit cs104_chang cw04 slope.c typescript

Note: If you do not complete the program, you should submit whatever you have accomplished. (Do record yourself compiling the program with script.) The classwork will be closed out from further submission at the end of class, but you may improve your score by 10% if you submit a better version by next class. As described in the syllabus, only people who have submitted classwork in class can submit an improved version.

To submit an improved version of your classwork after class, use finish as the project name in place of cw04:

submit cs104_chang finish slope.c typescript