Worksheet: C0 | CS 2113 Software Engineering - Fall 2022

Worksheet: C0

Worksheets are self-guided activities that reinforce lectures. They are not graded for accuracy, only for completion. Worksheets are due by Sunday night before the next lecture.

Github Classroom Link: https://classroom.github.com/a/BTQM0tzF

Setup

In order to compile your solutions, you’ll need to have gcc installed. However, this semester we will all be using the same version of gcc on the same operating system, that is, your accounts through SEAS at ubuntu-vlab01.seas.gwu.edu. So, instead of having you install gcc on your local machine now, we’ll lay some groundwork for what will be covered in lab this week in terms of setting up an environment to run your C code on this SEAS cluster.

For now, either open any programming text editor and create a blank file called work0.c (or whatever you wish to call it), or use the starter files you downloaded from the link above. There is where you will test your solutions to this worksheet. Once you’re ready to compile and run some C code, you’ll need to 1) copy that file over to the SEAS cluster and 2) log in to the cluster to access it and compile/run it. Here are the two relevant terminal commands:

scp work0.c kinga@ubuntu-vlab01.seas.gwu.edu:/home/ead/kinga/work0.c
will copy a file called work0.c from the current terminal directory (which you may have to cd to depending on where you saved your file) to the SEAS cluster. This step will require you to enter your password.

Once you copy your file with the above command (which stands for “secure copy”) to the SEAS cluster, you can connect to the cluster using your terminal with the following command (which will also ask for your password):
ssh kinga@ubuntu-vlab01.seas.gwu.edu

Once there, you can compile-and-run your C file that you just copied over a minute ago. To exit the remote connection to the SEAS cluster, type the logout command.

All this scping and ssh-ing will get old, very fast. In lab this week we’ll provide a much more efficient way to develop your code on the SEAS cluster; feel free to check out Lab 0 if you finish this worksheet early!

Questions

  1. Write a program called colors.c that prints out the following rainbow of colors:

    Here is a rainbow:
    .
    .
    .
    red
    orange
    yellow
    green
    blue
    purple
    

    Add the program to your repo. [Note: in the file you downloaded from github classroom, the solution to this problem is given for free to get you started.]

    Reveal Solution

  2. Compile the program above with gcc.

    • What gcc command did you use?
    • Did you have any errors? If so, note them and how you fixed them.
    • If you had no errors, introduce one (like removing a ;) and note the error message.

    Reveal Solution

  3. The following program should not compile… fix it and explain why.

    int main() {
      printf("Why oh why does this program fail to compile?!?!\n");
    }
    

    Reveal Solution

  4. We were expecting the following output

    They're not equal :-| 
    They're not equal :-| 
    They're not equal :-| 
    They're not equal :-| 
    They're not equal :-| 
    They're equal :-) 
    They're not equal :-| 
    They're not equal :-| 
    They're not equal :-| 
    They're not equal :-| 
    

    But it’s not working :( … Can you fix the program below to get it to work?

    int main() {
      int a = 10;
      int b = 5;
      while(a > 0) {
        if(a = b) {
          printf("They're equal :-) \n");
        }else {
          printf("They're not equal :-| \n");
        }
        a -= 1;
      }
    }
    

    Describe what was wrong?

    Reveal Solution

  5. What value(s) is/are true in C? What value(s) is/are false in C?

    Reveal Solution

  6. Convert the following code snippet into a function declaration and definition with proper formatting.

    int minus(int a, int b){return a-b;}
    

    Reveal Solution

  7. The following three programs will not compile when trying

    gcc main.c one.c two.c
    

    Fix the issues. The programs are below:

    //funcs.h
    void printOne();
    void printTwo();
    
       //one.c
       void printOne() {
         printf("one\n");
       }
    
       //two.c
       void printTwo() {
         printf("two\n");
       }
    
       //main.c
    
       int main() {
        for(int i = 0; i < 10; i++){
          if(i % 2) {
            printOne();
          }else {
            printTwo();
          }
        }
       }
    

    Reveal Solution

  8. Below are Java print statements. Write the C equivalent print statement.

    int a = 5;
    int b = 2;
    float c = 4.4;
    System.out.println("a = " + a + " b=" + b + " c=" + c);
    

    Reveal Solution

  9. What is the differences between fopen() file mode w and w+?

    Reveal Solution

  10. The C program below doesn’t write anything to a file.

    int main() {
      FILE * stream = fopen("helloworld.txt", "r");
      fprintf(stream, "Hello World!\n");
      fclose(stream);
    }
    

    Fix the program and describe the error(s).

    Reveal Solution

  11. What is the output of this C program?

    int main() {
       float f = 3.14;
       int n = 10;
       printf("%d %f\n", f, n);
    }
    

    Was it what you expected or not? Try and describe the process by which the output was achieved.

    Reveal Solution

  12. Consider the format directive %.3g, use man 3 printf to describe what the output would be if the input was 3.141592

    Reveal Solution

  13. You’re opening a file, and you get an error!?! Provide two preferred ways to report the error to stderr, as in …

    if(fopen( /* ... */) == NULL) {
      // WHAT GOES HERE?!
    }
    

    Reveal Solution

  14. Fix and describe the error in the code below.

    printf("Enter a number: ");
    
    int num;
    scanf("%d", num);
        
    printf("You entered %d\n", num);
    

    Reveal Solution

  15. Fix and describe the error in the code below.

    printf("Enter a number: ");
    
    int num;
    scanf("%f", &num);
        
    printf("You entered %d\n", num);
    

    Reveal Solution