CSE399: Homework 1

For this assignment your code should all be compiled with
   gcc -std=c99 -pedantic -Wall -Werror

  1. Hello World
     10 points
     Estimated time: 5 minutes
    
    In a file called hello.c, write a program
    which prints "Hello World" as output.  Compile
    the program so that the resulting binary is
    called hello (using -o hello), and run it
    (./hello).
    
    [Yes, the code for this was given in lecture-
     this problem is just to get you started]
    
    
    
  2. Factorial
     40 points  (Step 0: 0 points, Step 1: 20 points, Step 2: 20 points)
     Estimated time: 15 minutes
    
    Download the files: factmain.c and fact.h
    fact.h provides the following prototype for factorial:
    
      int factorial (int x);
    
    
    As you may recall, factorial(0) should return 1, and factorial(x) should return x * factorial(x-1) for any other positive x. Your factorial function may exhibit any behavior on negative inputs. You may use either recursive or iteration.
    • Step 0: Try compiling factmain.c (just do gcc factmain.c). You should receive an error message from the linker that looks like this:
         /tmp/ccIFxXDl.o: In function `main':
        factmain.c:(.text+0x19): undefined reference to `factorial'
        collect2: ld returned 1 exit status
      
      This error results from the fact that there is no input procividing a definition of factorial. Now recall that compilation can be stopped at simply an object file (gcc -c factmain.c). This command should complete with no errors, and the object file factmain.o should now exist.
    • Step 1: Create a file called fact.c which #include's fact.h, and defines the factioral function described above. Once you have a working implementation compile it to an object file (gcc -std=c99 -pedantic -Wall -Werror -c fact.c). Now link fact.o and factmain.o together to make the binary fact (gcc -o fact factmain.o fact.o). Try running the program (./fact) to test your code.
    • Step 2: The above description of factorial specified that your code does not need to deal with negative inputs, however, factmain.c does not check for this condition. Edit factmain.c so that if the user inputs a negative number, the program prints the error message "Negative numbers are not allowed!" instead of calling factorial. Recompile and test your code. Note that you do not need to recompile fact.c to fact.o since that has no changed. Also note, that you can just do this in one command: (gcc -o fact -std=c99 -pedantic -Wall -Werror fact.o factmain.c) gcc is smart enough to know what to do with each input file.
  3. Some array basics
      40 points  (4 functions, 10 points each)
      Estimated time: 15 minutes
    
    This problem will cover the aspects of arrays that look like Java arrays. For this problem you will be editing the file arrays.c. You must fill in the code for the following 4 functions:
    • sumArray: returns the sum of the elements of the array
    • findMax: returns the largest element of the array
    • printAllElements: prints out the contents of the array in the following format:
           array[0] = 10
           array[1] = 15
           array[2] = 7
      
    • sortArray: sorts the array into ascending order. You may use any sorting algorithm you want. As efficiency does not matter, bubble sort is likely the easiest.
    Recall that unlike java, arrays do not have a length field, so the length of the array is passed as a separate argument.
  4. Feedback
       10 points
       Estimated time: 5 minutes
    
    In a text file called feedback.txt, briefly let me know what you thought of this homework. Was it too hard or too easy? Did you feel like it was useful in getting you acquainted with the basics of C?

Submitting your assignment!

Your assignment will be submitted via email (adhilton@cis.upenn.edu) The subject should be
 CSE399: Homework1 Submission
You should submit a zip file containing all of the code, as well as the binary produced in step 1. Please name your zip file username.hwk1.zip (where username is whatever you login to eniac-l as). You can create the zip file with the following command:
  zip adhilton.hwk1.zip *.c *.h hello feedback.txt
(replace adhilton with your username) Then attach that zip file to an email. You can even send the email straight from enaic-l with the following command:
 mutt -a adhilton.hwk1.zip -s"CSE399: Homework1 Submission" adhilton@cis.upenn.edu < /dev/null
(yes, if that completes with no errors, it will just give back a prompt with no output)

STRONG recommendation (and very good habit):

After you create your zip file, make a new directory, copy the zip file into it, and unzip it. Then compile and check all of your code quickly in that directory before submitting it.