Homework 1

Introduction

The purpose of this homework is to apply what you and your team have learned in the lab to write a few more-useful programs for engineering computations. You will use your knowledge of arithmetic expressions, functions and loops to implement these tasks. You should also use the 5-step design process we have been using in class, and include all five steps in your approach to the programs:

        1. Understand the problem
        2. Do a hand example
        3. Write the algorithm for solving the problem
        4. Translate your algorithm into C
        5. Test your program

I STRONGLY advise you NOT TO SKIP ANY OF THESE STEPS. In fact, for this homework you will be required to submit your algorithms for all tasks in a single file called "algorithms" for 2 points.

You will be doing the tasks in this homework in your teams, and submit only ONE set of files per team. ALL members of the team should contribute to the homework, and understand how all of the programs work. I suggest you meet as a team right away to discuss understanding the problems and develop the algorithms as a group. One or more members of the team can then write the code and begin testing it. When a program compiles, you can exchange code with your teammates to let them understand, test, critique, and possibly improve it. You may use your Google Drive to share files within your team. (Sharing files between teams is not allowed and will result in no credit for either team). For this, and future homework assignments, each team should choose one member to act as the "coordinator" for the assignment. The coordinator is the member of the team who is responsible for seeing that the homework gets done correctly, professionally (representing the quality of your team's work), and on time. The coordinator may assign the coding and checking task to the members of the team and collect the final versions of the files and submit them to be graded. (Only one member of each team should submit files to the grader account). The job of coordinator should be rotated among the members of the team in future homeworks.

Remember, this is a TEAM effort. You should help and learn from each other.

In addition to the comments expected to be in your code describing the steps of your algorithms, each file you turn in should have a heading comment at the beginning of the file with the following information:

          /*     file:    the name of the file
                 by:      the name of the programmer who wrote this file
                 login:   the login id of the programmer who wrote this file
                 date:    the date
                 team:    your team name
                 members: your teammates' names
            */

Make sure your source code files are well commented, and well formatted for easy reading. Indent your code similar to the sample lecture code. Make sure your code lines are not too long (fit within a standard 80 column ssh window).

Tasks

1. Rounding to the nearest integer (3 Points).

Write a function that can be useful for future programs (like the next problem) which rounds any POSITIVE floating point value to the nearest integer. The function should "round up"; i.e. fractional parts of 0.5 and above should round to the next higher whole number. The function has the following prototype:

            int round_to_int(float value);
            /*  Given: a positive float value
             *  Returns: the value rounded to the nearest integer (rounding up)
             */

The function is given a positive float value and returns an integer which is the rounded value of its arguement. NOTE: you may have noticed that if you restrict the decimal places printed with %f, printf() rounds to the last decimal place printed - this is NOT what we want here. We want an integer value returned by the function that can be used in future calclulations. Other than debug lines (which you should disable when you turn in your files), your function should not print anything; it should just return the correct integer value. (Hint: to round positive floats you do NOT need an if statement).

You will need a main() function to test your round_to_int() function. This main() is called a "test driver", or just "driver". It's sole purpose is to prompt the user for data, one at a time, give the data to the function, and print the result it gets back to let the user check that the function is operating correctly for different input. Your driver should continue to prompt the user until a 0 is entered, when it should terminate. Call the file containing your code round.c.

For up to 1 additional bonus point, you can modify your program to work correctly for any float value, positive or negative.


2. Excess Data! (5 Points).

You have decided to look in to changing your cell phone service because you keep going over the free data limit. The two service providers you have been looking at are Jog and Holo-T. Both services have the same number of free data, but charge differently for data exceeding those limits. Your phone lets you download the data for data you use in excess of your free data to your computer, so you decide to write a program to calculate how much it would cost you for a typical month using each of these services. (You can find a sample data file in ~ee160/Homework/Hw1/cell.dat).

Jog charges you $0.05 per MB for excess data. However, they calculate your excess data per month by rounding your daily totals to the nearest MB, then they add all the daily totals in a month. (Rounds Daily excess, then sums up the whole month)

Holo-T charges you $0.07 per MB for excess data. However, they calculate your excess minutes by adding the total excess data per month, and then rounding the total to the nearest MB. (Sums Monthly Excess, then rounds Excess)

Write a program that reads the data from a file (redirected on the command line), and computes the cost for each of the cell companies. You will run the program with a command like:

            a.out < cell.dat

Call the file with your source code cell.c. Use the round_to_int() function from problem 1 to do the appropriate rounding in your program. (Hint: you can copy the round.c file to cell.c and then edit it to change the driver, main(), to implement the algorithm for this program). You can find a sample executable for this program at ~ee160/Homework/Hw1/cell.


3. Linear Regression (10 Points).

In some engineering experiments, a set of measurements is taken to be plotted and we expect the relationship to be linear; i.e. the equation of a line. However, due to flaws in the measurement instruments and other inaccuracies, the data does not exactly fit a line. So we would like to find the equation for the line that "best fits" the measured data.

For example, consider the data shown below for the cylinder head temperature of an engine measured every second as the engine warms up:

         Time, s   Temperature, F

            0         0
            1         20
            2         60
            3         68
            4         77
            5         110

When we plot this data it looks like:

The best fit line looks like this:

As you may recall from your Math courses, the equation of a line is:

                  y = m * x + b


Given the set of data points, our task is to find the values of m and b which describes the line which fits "best" to this set of data. Equations for m and b can be derived, but here, I just give them to you:

Your task is to write a program that reads the data points redirected from a file (i.e. no prompts), computes the equation of the best fit line, and produces output that we can use to plot the results. We can use MATLAB to plot the result. You do not need to know MATLAB, you just need to produce a redirected output file that looks exactly like:

            p = [ 0.00,  20.00,  60.00,  68.00,  77.00, 110.00 ];
            x = linspace( 0.00,   5.00, 6 );
            y =   20.83 * x +   3.76;
            plot(x, y ,x ,p, 'go')
            text(0.5,100,'y = 20.83 * x +   3.76');

Note, the bolded text above will be different for different data files; the unbolded text will always be the same.

I have provided a data file for the above set of points in the ~ee160/Homework/Hw1 directory called temps.dat. Your program should produce a file called graph.m, which is a file MATLAB will recognize to show your results. You will run your program with a command like:

            a.out < temps.dat > graph.m

Once you have a correct graph.m file, you can see your plot by transferring your file to a peecee with MATLAB and run the .m file there. We will only be looking at the format of your output file.

Your program should produce the correct equation for the best fit line and the correct MATLAB file for any set of data. For example, try removing any of the points in the above data file, and you will get a different line and a different plot. You should call the source code file for your program regress.c.

Advice: You will have to think about this program before you start writing ANY code. Begin at the first step of the design process, do a hand example, THINK about the algorithm, and then write and test the code. I would suggest you take a bottom-up approach to this program. First get the program to work to find the correct values for m and b, then add the features to make the output come formatted correctly for the MATLAB file. MOST IMPORTANT: don't wait till the last minute to begin this program. You will need several iterations to get this one to work.

BONUS (1 Point max)

This is not really available for Virtual Machine users.

To see what you can find out about Unix on your own, for up to 1 Bonus point on this homework, see if you can find if there are any files in the ~ee160 account that you have write permission for - i.e. that you can change and save in that account. Tell me, in your algorithms file, which file(s) are writable AND how you know that.

What You Turn In

For this first homework, I have provided some sample executables for the above 3 programs in the ~ee160/Homework/Hw1 directory. You can run those executables to see how your program should run and the output they should produce. I have also provided the temps.dat file for the Linear Regression program in that directory.

I would STRONGLY suggest you create a separate directory for each of the homework assignments in your EE160 directory, just like you do for the Labs.

For this homework, you will turn in four files: algorithms, round.c, cell.c, and regress.c. To turn them in use one of the grade commands below:


If you are in Section 001 use:

          grade -1s1,ee160 algorithms round.c cell.c regress.c


If you are in Section 002 use:

          grade -1s2,ee160 algorithms round.c cell.c regress.c


If you are in Section 003 use:

          grade -1s3,ee160 algorithms round.c cell.c regress.c


If you are in Section 004 use:

          grade -1s4,ee160 algorithms round.c cell.c regress.c


You should verify that you turned in things successfully, which you can do with the grade command by simply leaving the file names off from the previous command:

//1s1 - stands for HW 1 section 1

1s1 - is the number 1, the letter 's', and the number '1'

          grade -1s1,ee160
         
          OR
         
          grade -1s2,ee160
         
          OR
         
          grade -1s3,ee160

          OR
         
          grade -1s4,ee160

Remember, Homeworks are due by 11:59 PM on the date specified. I will process Homeworks 1 min after at 12:00AM the next day. Late Homework is not accepted.