CSCE 150A (Fall 2009) Homework 5

Assigned Friday, November 13
Due Monday, November 23 Wednesday, November 25 at 11:59pm
Total points: 150


When you hand in your results from this homework, you should submit the following:

  1. The source code of your programs, in text files, named as specified below in Problems 3–5.
  2. An optional text file called README that contains any notes about your programs that you want the graders to know.
  3. A single .pdf file with your writeup of the results for all homework problems. Only pdf will be accepted, and you should only submit one pdf file, with the name username.pdf, where username is your username on cse. We will not accept any hard copy submissions of your homework!
  4. Every problem requres a writeup in this homework. For Problems 3–5, you need to decompose each problem just like you did in the previous homework. For an example of how to decompose a problem, see Question 5 on Homework 2, where we give you the problem decomposition. This is what is expected for the decompositions for this homework.
  5. New requirement: For each Problem 3–5 for which you are defining the program file name.c, you need to create a file name.txt, which is a text file describing at least ten test inputs for your program, along with the outputs your program should generate from those inputs, and the outputs that your program actually generated (hopefully those will match what should have been generated). Your inputs should vary significantly and test the boundary conditions. E.g. for the election problem, you should vary the number of candidates and precincts, including one precinct with multiple candidates, multiple precincts with one candidate, one precinct with one candidate, and the maximum number of precincts with the maximum number of candidates. Your test inputs for election should also vary the outcome: some results should have an outright winner and others should require a runoff. You must do this for all the programming problems.

Put your name in all your files, and submit everything by the due date and time using the web-based handin program.

On this homework, you must work on your own and submit your own results written in your own words.


  1. (20 pts) Define the following function:
    mergeSortedArrays(int *mergedarray, const int *a, const int *b,
        int sizeOfA, int sizeOfB);
    
    that takes as input two arrays a and b, which are assumed to be sorted in ascending order. It merges a and b into a single array mergedarray, which you may assume is of size sizeOfA + sizeOfB. When the function is done, array mergedarray must contain all the elements of arrays a and b, in sorted order. Your function should not do any sorting on its own.

  2. (25 pts) Define the following function:
    int median(const int *array, int n);
    
    which, given an array of size n, returns the value of the median element. The median is defined as follows: Note that array is passed as a constant, thus you may not make changes to array in the function. You may assume that n < 100. You may not assume that array is sorted.

  3. (30 pts) Write a program called rainfall.c that reads in rainfall data from the current year and the previous year, and prints these values as well as summary statistics for both years. The summary statistics should include total annual rainfall and average monthly rainfall. Your program will read the numbers from standard input, one month per line (line 1 is January, line 2 is February, and so on), with the current year being in the first column and the previous year in the second column. Do not prompt the user for input; instead assume that the input is in a file that will be redirected to standard input:
    ./rainfall < input.txt
    If the input file looks like this:
    3.2  4
    2.2  1.6
       .
       .
       .
    
    and so on, then your program's output should resemble:
                           Table of Monthly Rainfall
                 Jan   Feb   ...
    This year    3.2   2.2
    Last year    4.0   1.6
    
    Total rainfall this year:  34.8
    Total rainfall last year:  42.8
    Average monthly rainfall for this year:  2.9
    Average monthly rainfall for last year:  3.6
    
    Your program should have a function that reads the input into the arrays, a function that computes the total rainfall for a single year (which you'll use for each of the two years separately), and a function that prints the table.

  4. (35 pts) Write a program called election.c that reads from standard input election results, where each row represents a precinct and each column represents a candidate. As with the rainfall program, your program will read its input from a file redirected to standard input. This input will start with a line with two integers: the first will be the number of precincts (p, which you may assume is < 10) and the second will be the number of candidates (c, which you may assume is < 5). The rest of the file will have p rows, each with c columns, where column i in row j represents the number of votes that candidate i got in precinct j. For example, your input file might look like this:
    5 3
    63 19 21
    45 126 54
    29 218 124
    4 135 152
    52 97 128
    
    After reading the input, your program should do the following:
    1. Display the vote counts in a table, with row and column labels indicating the precinct and candidate.
    2. Compute and display the total number of votes received by each candidate, as well as each candidate's percentage of total votes cast.
    3. If one candidate received at least 50 percent of the votes, your program should display a message declaring that candidate the winner.
    4. If no candidate received at least 50 percent of the votes, your program should display a message declaring a runoff between the two candidates receiving the largest number of votes, and identifying the two candidates by index (e.g. "Candidate 1" and "Candidate 2").
    Here is the output for the input file given above:
    Precinct   Candidate 0   Candidate 1   Candidate 2
        1           63            19            21
        2           45           126            54
        3           29           218           124
        4            4           135           152
        5           52            97           128
    Total          193           595           479
    Percentage     15%           47%           38%
    
    Result: Runoff required between Candidate 1 and Candidate 2.
    
    Your program should have a function to read in the data and a function to print the results.

  5. (40 pts) Write a program called hangman.c that plays an interactive game of Hangman. Your program should have a character array called word that contains the word that the player will attempt to guess, as well as character array called guessed, initialized with '*' characters, that contains the letters of word correctly guessed, in their proper position (e.g. if word is stewie and the user has correctly guessed 's' and 'e', then guessed should be s*e**e). Your program will repeatedly prompt the user for a new letter, read it in, update guessed, and then print guessed. The program should end when either (a) the user has entered too many letters not in word, or (b) the user correctly enters all the letters of word. Like word, the maximum number of incorrect letters should be specified in a variable and set at compile time (i.e. not input by the user). Your output might look something like this:
    Welcome to Hangman
    Enter a character: e
    Current guess: **e**e
    Enter a character: s
    Current guess: s*e**e
    Enter a character: x
    Current guess: s*e**e
    Enter a character: t
    Current guess: ste**e
    Enter a character: w
    Current guess: stew*e
    Enter a character: y
    Current guess: stew*e
    Enter a character: i
    Current guess: stewie
    Congratulations!  You guessed the word!  Goodbye!
    

    To avoid issues with entering characters one at a time, your program may use as a subroutine the following function:

    char getinput(void)
    {
    char c;
    
    printf("Enter a character: ");
    do {
    c = getchar();
    } while (c == '\n');
    
    return c;
    }
    
    Your program should have a function to scan word for the latest letter entered by the user and update guessed.

Back
Last modified 16 August 2011; please report problems to sscott.