CSC 115.005/006 Sonoma State University Spring 2022
Scribbler 2
CSC 115.005/006:
Programming I
Scribbler 2
Instructor: Henry M. Walker

Lecturer, Sonoma State University
Professor Emeritus of Computer Science and Mathematics, Grinnell College


Course Home References Course Details: Syllabus, Schedule, Deadlines, Topic organization MyroC Documentation Project Scope/
Acknowledgments

Notes:

Laboratory Exercise on Elementary C Programming

Introduction

This laboratory exercise provides practice with basic elements of writing, editing, compiling, and running programs written in the C programming language. This will use the quarts.c program, complete with annotations.

Program Examples

Work Started in Class

Introduction to the Aquamacs Editor

Open a terminal window and move to your Desktop/115---- subdirectory:

cd Desktop/115---- 
  1. Working within a terminal window, start aquamacs.
    Starting at your home directory, use these commands:

       cd Desktop/115---- 
       aquamacs quarts.c &
    
  2. Use the links for the reading (the quarts.c program) to open the program quarts.c in your browser. Then copy and paste it into the aquamacs window.

Compiling and Running

  1. Compile and run the program in your terminal window by typing:

       gcc -o quarts  quarts.c
       ./quarts
    
  2. Run the program several more times by typing just ./quarts. (You need not compile the program each time — unless you have changed quarts.c.)

Experimenting with Compiling

  1. Make the following typographical errors in quarts.c, recompile, and observe what, if anything, happens.
    In each case, check whether the program compiles, and whether the program runs. If the program does not compile, what happens if you try to run quarts?

    • Type a few characters into your program BEFORE any of the code.

    • Type a few characters into your program AFTER the code.

    • Type some extra words inside of your main method.

    • Misspell your variables.

    • Misspell your printed output.

    • Misspell the name of your main() method.

    • Misspell the name of the included library.

Homework

Writing Your Own Program

  1. Write a C program that computes the total number of liters corresponding to a combination of pints, quarts, and gallons. For example, your program might compute the number of liters equivalent to 3 gallons, 2 quarts, and 1 pint (i.e., 14.5 quarts total).

    To organize this program, begin by declaring pints and gallons as variables in addition to quarts and liters in the existing program. Next, assign values to these variables, such as:

       gallons = 3;
       quarts = 2;
       pints = 1;
    

    In computing the total value of liters, one approach would be to compute the total number of quarts from pints, quarts, and gallons (possibly using another variable, such as total_quarts). From this total_quarts, you could compute the total number of liters.

    In computing the total number of quarts, you should use 4 quarts per gallon and 2 pints per quart. (Be sure that 3 gallons (given in the example above) translates to 12 quarts, not 3/4 or 0.75 quarts.)

    You should reference the annotations on quarts.c to properly print out your output.

  2. Although you can name this new program whatever you like, you should end the file name with .c for two reasons:

    • You can identify the C programs quickly when you list files in your directory with the ls command.

    • aquamacs recognizes the .c extension as indicating a C program, and aquamacs adjusts its setting to aid your editing for that type of file.

Writing More C

  1. Write a C program that uses a value for the radius of a circle and computes the circle's area and circumference.


created 7 April 2008 by Henry M. Walker
last full revision 18 July 2011 by Dilan Ustek and April O'Neill
minor editing 24 August 2011 by Henry M. Walker
edited 18 September 2011 by Erik Opavsky
minor editing 25 September 2011 by Henry M. Walker
steps 6, 9 revised 7 September 2012 by Henry M. Walker
restructured 14-16 January 2014 by Henry M. Walker
readings added 1 September 2014 by Henry M. Walker
adjusted for general distribution 7 May 2016 by Henry M. Walker
adjusted for CS 115 at Sonoma State University 16 January 2021 by Henry M. Walker
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.