Classwork 3: Our first C program
Thursday, July 12, 2012[Previous Classwork] [Next Classwork]
Complete the following:
- Ensure the machine is booted into Linux
- Login to Linux
- Open a termimal from the top left Applications menu -> System Tools -> Terminal
- Create a directory (using mkdir) called cw03 to store this classwork
- Go into your new cw03 directory (using cd)
- 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
- Create a new file using nano called cw03.c
- 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 */
- 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); }
- Compile your program as shown below
linux2[107]% gcc -Wall cw03.c -o cw03
- Run your program as shown below
linux2[108]% ./cw03 Starting my program
- Starting with the program code provided, add statements to get 3 values from the user, add those values together, and then print the result.
- Make sure you change the file description comment to say what your file will do now
- Test the program as you are writing it.
- 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.
- Once you are sure the program is done and working well, start a script using script
- Once the script is running, show you compiling your program, and then running the program three times, typing in different values each time.
- Exit from the script, which will save the log typescript
- 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
- Once you are done, use submit to submit two files, cw03.c and typescript
linux3[26]% submit cs104 cw03 cw03.c typescript
- After submitting your files, you can verify your files were submitted by using the submitls command as shown below
linux3[26]% submitls cs104 cw03
- MAKE SURE YOU LOGOUT AFTER SUBMITTING THE ASSIGNMENT AND BEFORE LEAVING CLASS!