Classwork 3: Our first C program

Thursday, July 12, 2012     


[Previous Classwork] [Next Classwork]


Complete the following:

  1. Ensure the machine is booted into Linux
  2. Login to Linux
  3. Open a termimal from the top left Applications menu -> System Tools -> Terminal
  4. Create a directory (using mkdir) called cw03 to store this classwork
  5. Go into your new cw03 directory (using cd)
  6. Use ls and pwd to make sure you are in a directory called cw03

    linux2[95]% mkdir cw03
    linux2[96]% cd cw03
    linux2[97]% pwd
    /afs/umbc.edu/users/d/s/dsheets/home/cw03
    linux2[98]% ls
    

  7. Create a new file using nano called cw03.c
  8. Type a comment for the header based on the template below:

    /*   File: cw03.c
         Name: A. Student
         Username: astudent1
         Date: 10/24/07
         Description: A sample program
    */
    

  9. Copy or type the following code into your cw03.c file following your header

    #include <stdio.h>
    
    int main(void)
    {
      printf("Starting my program\n");
      return(0);
    }
    

  10. Compile your program as shown below

    linux2[107]% gcc -Wall cw03.c -o cw03
    

  11. Run your program as shown below

    linux2[108]% ./cw03
    Starting my program
    

  12. Starting with the program code provided, add statements to get 3 values from the user, add those values together, and then print the result.
  13. Make sure you change the file description comment to say what your file will do now
  14. Test the program as you are writing it.
  15. NOTE: Testing often is best. Don't write all the code then test. It is very useful to have two terminal windows open, one to edit and one to compile and run your progam.
  16. Once you are sure the program is done and working well, start a script using script
  17. Once the script is running, show you compiling your program, and then running the program three times, typing in different values each time.
  18. Exit from the script, which will save the log typescript
  19. An example of me running the script, compiling, and running my program is below

    linux2[120]% script
    Script started, file is typescript
    linux2[1]% gcc -Wall cw03.c -o cw03
    linux2[2]% ./cw03
    Starting my program
    Input value 1: 1
    Input value 2: 2
    Input value 3: 3
    Total is: 6
    linux2[3]% ./cw03
    Starting my program
    Input value 1: 6
    Input value 2: 7
    Input value 3: 2
    Total is: 15
    linux2[4]% ./cw03
    Starting my program
    Input value 1: 100
    Input value 2: 200
    Input value 3: 300
    Total is: 600
    linux2[5]% exit
    exit
    Script done, file is typescript
    

  20. Once you are done, use submit to submit two files, cw03.c and typescript

    linux3[26]% submit cs104 cw03 cw03.c typescript
    

  21. After submitting your files, you can verify your files were submitted by using the submitls command as shown below

    linux3[26]% submitls cs104 cw03
    

  22. MAKE SURE YOU LOGOUT AFTER SUBMITTING THE ASSIGNMENT AND BEFORE LEAVING CLASS!