Problem Set: C Functions and Arrays

1.  Convert the following Java program to C:

   import java.util.Scanner;
   public class ArrayEx {
      public static void main(String [] args) {
         int [] a,b;
         int  j, m, suma = 0, sumb = 0, sumdiff = 0;
         Scanner keyboard = new Scanner(System.in);
         a=new int[100];
         b=new int[100];

         System.out.print("enter the number of pairs to read: ");
         m = keyboard.nextInt();
         System.out.println("enter " + m + " pairs:");
         for (j = 0 ; j < m ; j++) {
            a[j]=keyboard.nextInt();
            b[j]=keyboard.nextInt();
            suma = suma + a[j];
            sumb += b[j];
            sumdiff = sumdiff + (a[j] - b[j]);
         }
         for (j = m - 1; j >= 0; j--)
            System.out.println(a[j] + "   " + b[j] + "   " + (a[j]-b[j]));
         System.out.println(suma + "   " + sumb + "   " + sumdiff);
      }
   }


2.  Write a function match which has 3 parms: two integer arrays
    (named one and two) and the size of the arrays (an int). The
    function returns the number of times "matches" occur in parallel
    positions in the two arrays. That is, count the number of times
    one[i] == two[i]. The lengths of both arrays are the same.


3.  Write a function readArray which has two parms: an array of ints and
    the size of the array. Prompt the user and read values into the array
    from the keyboard.


4.  Write the include statements, prototypes and main function to declare
    two int arrays of size 25, call your readArray function to read values
    into each of your arrays, call your match function, and print out the
    number of matches in the two arrays.


5.  Write a function reverse which has 3 parms: two integer arrays
    (named one and two) and the size of the arrays (an int). The
    function copies the values from two into one in such that one
    contains the reverse of two. That is, one[0] = two[size-1],
    one[1] = two[size-2], etc. The lengths of both arrays are the same.


6.  Write the include statements, prototypes and main function to declare
    and initialize an int array of 12 elements and declare a second int
    array of size 12. Call your reverse function to reverse the first array
    into the second, and print out the second array.


7.  Write a function called lenstr with one parm, a string. The function
    will return the length of the string. Do not use strlen.


8.  Write the statements to declare a string that can hold at most 25
    characters. Prompt the user to enter a string, read the string, and
    call the lenstr function and print the length of the string.


9.  Write a function called count that will count and return the occurrences
    of a given character in a string named letters.  The parameters will
    be the string letters and the character to count. For example, if the
    string contains the values
             x8RAaa0sSaA
    and the character to count is 'a', then the function returns 3.


10.  Write the statements to declare a string that can hold at most 25
    characters. Prompt the user to enter a string, read the string, and
    call the count function and print the number of times the character
    p is contained in the string.


11. Write a function called catstr with two parms, both strings. The
    function will add the characters in the second string to the end
    of the first string. Assume that the first string is large enough
    to hold the characters of the second string. Do not use strcat.


12. Write the statements to declare and initialize two strings. Make sure
    that the first string is large enough to hold the contents of both
    strings. Call your catstr function to add the second string to the
    first, then print both strings.


13. Write a function called ncatstr with three parms, two strings and
    and int. The function will add the characters in the second string
    to the end of the first string. It will add at most n chars: if
    the second string is less than n chars, it will add all chars followed
    by a null byte. If the second string is n chars or more, it will add
    n-1 chars and a null byte. Note that this is not the same as the way
    strncat works. Do not use strncat.


14. Write the statements to declare and initialize four strings as
    follows:
        string 1: 20 chars, contains "cloudy"
        string 2: 15 chars, contains "blueblue"
        string 3: 25 chars, contains "sunny"
        string 4: 15 chars, contains "bright bright"
    Call your ncatstr function to add at most 10 characters of string 2
    to string 1. Call it again to add at most 10 characters of string 4
    to string 3. Print string 1 and string 3. What will be printed?


Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2016