Description
- Follow the Textbook Appendix B and the Unix Tutorial PPT given in the homework folder to complete the following tasks:
- Create a new directory called CSE240. Use cd CSE240 to enter the new directory. Use pwd command to find the path from the Unix root directory to your current directory.
- Create three new subdirectories inside CSE240. Name the directories d1, d2, and d3 respectively.
- Enter the directory d1 and use vi or vim to create a new program called hello.c and enter the following code into the program. You must use your name to replace the name “John Doe”. Save the file.
Use gcc hello.c -o hello to compile the program. Fix any compilation errors if any.
- Use ls, ls -l, and ls -al respectively to list files in d1 directory. Use command ./hello to execute the compiled code. .
Take screenshots of each command in this question. You may take one screenshot with all commands in it.
- Enter d2 directory and then use one command to copy (not move) the files hello.c and hello from d1 directory into d2 directory. Use ls -al to view all the files in d2.
Take screenshots of each command in this question. You may take one screenshot with all commands in it.
- In d2 directory, use chmod 660 hello to change the permission. Use ls -l to view the files. Use command ./hello to execute the compiled code. Take a screenshot of this output. Use chmod command again to make hello an executable, but not readable and not writeable for all users. Use command ./hello to execute the compiled code.
Take a screenshot of this output.
- Enter CSE240 directory. Use ls -l to view all the files. Then, use one command to delete d2 directory and all the files in d2 directory. Use ls -l to view all the files.
Take screenshots of each command in this question. You may take one screenshot with all commands in it.
Screenshots needed for questions 1.4 through 1.7. Put all the screenshots along with their question numbers in a word file and convert to pdf. Submit the file as hw01q1.pdf
- In this question, you will use Unix tools to edit, debug, and execute a simple C program. The purpose of the homework is to learn the Unix programming environment. Read textbook Section 2.2.3, the tutorials, and the Unix tutorial in text Appendix B.2.
- Use a text editor to enter the following program. Variations and more discussions of this program can be found in textbook Section 2.1.3. Use GNU GCC to compile, debug (find and fix any syntax errors), and execute the program and fix any semantic errors, for example, by adding “break” statements in the required places and by fixing incorrect characters copied into the programming environment, if any, and by type-changing to print a floating point number. The code is supposed to perform one math operation in each switch-case. The ‘ch’ variable is assigned a new math operator before each switch-case. Submit the corrected program as hw01q2.c [8 points]
| /* This C program demonstrates the switch statement without using breaks. */
#include <stdio.h> main() { char ch = ‘+’; int a = 10, b = 20; double f; printf(“ch = %c\n”, ch); switch (ch) { case ‘+’: f = a + b; printf(“f = %d\n”, f); case ‘-‘: f = a – b; printf(“f = %d\n”, f); case ‘*’: f = a * b; printf(“f = %d\n”, f); case ‘/’: f = a / b; printf(“f = %d\n”, f); default: printf(“invalid operator\n”); } ch = ‘-‘; printf(“ch = %c\n”, ch); switch (ch) { case ‘+’: f = a + b; printf(“f = %d\n”, f); case ‘-‘: f = a – b; printf(“f = %d\n”, f); case ‘*’: f = a * b; printf(“f = %d\n”, f); case ‘/’: f = a / b; printf(“f = %d\n”, f); default: printf(“invalid operator\n”); } ch = ‘*’; printf(“ch = %c\n”, ch); switch (ch) { case ‘+’: f = a + b; printf(“f = %d\n”, f); case ‘-‘: f = a – b; printf(“f = %d\n”, f); |
| case ‘*’: f = a * b; printf(“f = %d\n”, f);
case ‘/’: f = a / b; printf(“f = %d\n”, f); default: printf(“invalid operator\n”); } ch = ‘/’printf(; “ch = %c\n”, ch); switch (ch) { case ‘+’: f = a + b; printf(“f = %d\n”, f); case ‘-‘: f = a – b; printf(“f = %d\n”, f); case ‘*’: f = a * b; printf(“f = %d\n”, f); case ‘/’: f = a / b; printf(“f = %d\n”, f); default: printf(“invalid operator\n”); } ch = ‘%’; printf(switch “ch = %c(ch) { \n”, ch); case ‘+’: f = a + b; printf(“f = %d\n”, f); case ‘-‘: f = a – b; printf(“f = %d\n”, f); case ‘*’: f = a * b; printf(“f = %d\n”, f); case ‘/’: f = a / b; printf(“f = %d\n”, f); default: printf(“invalid operator\n”); } } |
- Edit the code above or write a new code to use a for-loop (that runs 5 times) to ask the user for a math operation as input ( +, – , * , / ). Use an input statement such as ch = getchar(); or scanf(“%c\n”, &ch); to replace the assignment statement ch = ‘+’ in the code above. Expected result shown in the figure below. Submit the revised
program as hw01q2_2.c.
Note, when you add the loop, you may need to use a fflush(stdin) or a getchar() to flush the newline ‘\n’ character left behind by a previous operation; You can use one ch = getchar() at the beginning of the loop and one before the end of the loop: for ( … )
{
printf(“please enter a char”); ch = getchar(); // read a char
other code; ch = getchar(); // This line will flush ‘\n’ character left behind by the previous getchar().
ch = getchar(); // You may need this one to keep the console window open
}
Please read text Section 2.1.3 for more details.
3. You are given a file named “hw01q3.c”. All instructions are given in the form of comments in the file. You should correct the errors and identify which error type they are. Please read all instructions carefully, then complete and submit the updated file




