Gerry Wildenberg -- C module exercises

Mathematical and Computing Sciences


For all of the following exercises, the language to be used is C. No C++ or any other language is to be used. Any place that my name appears, use your name. Where I say "Write a program ...", I mean: "Write, test, and prepare a listing of the program and it's output."

  1. Write a program to print output which resembles the following.
                   ********************************
                   * My name is: Gerry Wildenberg *
                   ********************************
    
  2. Write a while loop to print the numbers 2, 4, ..., 20
  3. Write a program, which does NOT call a function, to print a table of meters and the corresponding number of feet. (3.25 feet equals 1 meter.)
  4. Write a program, which calls a function that takes an integer number of meters as an argument and returns the corresponding number of feet, to print a table of meters and the corresponding number of feet. (3.2808 feet equals 1 meter.)
  5. Write a program that prints out it's input double spaced (i.e. with a blank line after each line of input).
  6. We can imagine a language (we'll call it C--) in which the only syntax for comments was that "//" meant that everything else on the line was a comment. (Of course this rule would not apply if the "//" happenned to be inside a pair of double quotes.) Write a program to remove all such comments. If you don't have any programs around like that, here's one you can download. Sample C-- Program
  7. In the game of "Guess the Number", there are two players, one called Guesser, the other called Chooser. First Chooser picks a number between 1 and 100. Then Guesser makes guesses at the number receiving the answer of, higher, lower or right. When Guesser gets the number, the score is how many guesses it took. If guesser gives up the score is 100. (Needless to say, low scores are good.)
    A. Write a program which can play the part of Chooser. The following may help you.
    
    main ()
    {
            long k, n;
    	int i, m;
            k= time(0);
            srandom(k);
    	
    	for(i=0;i<20;i++){
    		n=random();
    		m=n%100 + 1;
    		printf("%d\n",m);
    	}
    }
    

    B. Write a program which can play the role of Guesser. Use an efficient strategy based on binary search.
  8. Rewrite the program on meter to feet conversion so that the conversion is done using a function called meter_to_foot. Have a second function called "table(int m, int n) " which calls meter to foot repeatedly using the values from m to n in the loop.
  9. Print a table of ascii values for the decimal integers 40 to 127. Have columns in the table for octal, hex, decimal and the character itself.
  10. Write a program which takes two numbers command line parameters and prints their product.
  11. Write two versions of a function that converts a string parameter to upper case. Version A: The string is a parameter and the upper-case string is returned. Version B: The parameter itself is changed.
  12. Generate a number k between 45 and 55. Then generate k random integers in the range 7 to 13 inclusive and use the ?: operator to print them 7 per line.
  13. Write a program which fills an array with 100 random numbers from 1 to 100.(expect some duplicates). Then find the largest number in the array and the smallest and print that out. Then find the average of the values in the array and how much each value in the array differs from the average and print out that information. This program should be broken up into functions so that little or no work is done in main.
  14. Write a function that takes a string parameter and converts it to all upper case. Write a main program that reads a string and then calls your function and prints out the results.
  15. Write a very general conversion function which takes as parameters a value to be converted and a conversion factor. Modify table() so that it can handle the conversion factor and call the new conversion function. Let main() consist of little more than a call to table.
  16. Add a heading parameter to table().