CSC231 Optional Homework 9 2017

From dftwiki3
Revision as of 10:46, 22 April 2017 by Thiebaut (talk | contribs) (Testing Your Program)
Jump to: navigation, search

--D. Thiebaut (talk) 11:10, 22 April 2017 (EDT)


Page under construction!

UnderConstruction.jpg

Problem 1: C Functions


Write a C program that contains only C functions. The functions will be called by an external main program. This is very similar to the the last homework assignment, except then it was done in assembly.

Functions


You have to write 3 functions: getMin(), zap() and merge().

getMin()
Receives 3 ints as parameters and returns the smallest.
Example
  printf( "Min of -10, 1, 10 = %d\n", getMin( -10, 1, 10 ) );
  printf( "Min of -10, 1, 10, -20, 2 = %d\n", getMin( getMin( -10, 1, 10 ), -20, 2 ) );
  // output:
  //  Min of -10, 1, 10 = -10
  //  Min of -10, 1, 10, -20, 2 = -20


zap()
Receives two strings as parameters and modifies the first one by finding the first
string in it, and replacing it with dashes.
Example
  char s1[] = "Mississippi Burning";
  char s2[] = "Mississippi Burning";

  //--- test zap ---
  printf( "s1 = %s\n", s1 );
  zap( s1, "ss" );
  printf( "zap(s1) = %s\n", s1 );

  printf( "s2 = %s\n", s2 );
  zap( s2, "tt" );
  printf( "zap(s2) = %s\n", s2 );
 
  // output:
  // s1 = Mississippi Burning
  // zap(s1) = Mi--issippi Burning 
  // s2 = Mississippi Burning
  // zap(s2) = Mississippi Burning


merge()
receives 3 arrays of ints. The first two have dimension 5, each, and are sorted. The third one is of dimension 10, and contains random information, i.e. it is not initialized. Merge() takes the ints from both arrays of dimension 5 and merges them into the third array, keeping the ints sorted. You may assume that the first two arrays will always have dimension 5, and the third one will always have dimension 10.
Example
  int A[] = { 1, 2, 3, 10, 11 };
  int B[] = { 4, 5, 12, 13, 15 };
  int C[10];

  merge( A, B, C );
  for ( i=0; i<10 && A[i]!=-1; i++ )
    printf( "%d, ", C[i] );
  printf( "\n" );
  // output:
  // 1, 2, 3, 4, 5, 10, 11, 12, 13, 15,


Merging in Python


For reference, here is how merge would work in Python:

from __future__ import print_function

def merge( A, B, C ):
    """
    merges 2 arrays into a 3rd one.  The dimensions of the arrays can be 
    any valid integer.  C must be a mutable list.
    """
    i = 0
    j = 0
    k = 0
    while i<len(A) and j<len(B):
        if A[i] < B[j]:
            C.append( A[i] )
            i += 1
        else:
            C.append( B[j] )
            j += 1
        if i >= len( A) or j >= len( B ):
            break
    while i < len( A ):
        C.append( A[i] )
        i += 1
    while j < len( B ):
        C.append( B[j] )
        j += 1
 
def main():   
    A = [1, 3, 10, 20, 30 ]
    B = [2, 3, 4, 5, 100 ]
    C = []
    merge( A, B, C )
    print( "A = ", A )
    print( "B = ", B )
    print( "C = ", C )

main()

# output
# A =  [1, 3, 10, 20, 30, 31]
# B =  [2, 3, 4, 5, 100]
# C =  [1, 2, 3, 3, 4, 5, 10, 20, 30, 31, 100]


Testing Your Program


Here is