CMPS 200 Homework Assignment 9, Fall 2011
American University of Beirut · Introduction to Programming · 2 pages
More CMPS 200 files
- Homework 11 · Fall 2018HomeworkView
- Homework Last Assignment · Fall 2018HomeworkView
- Homework Asst10 · Fall 2017HomeworkView
- Homework 9 · Spring 2017HomeworkView
- Homework Assignment 2 · Fall 2016HomeworkView
- Homework Assignment 5 · Fall 2016HomeworkView
- Homework Assignment 6 · Fall 2016HomeworkView
- Homework 1 · Spring 2016HomeworkView
Show the text of this homework
Faculty of Arts & Sciences
Department of Computer Science
CMPS 200—Introduction to Programming
Assignment 9 – Due Fri Dec 2, 2011
Notes and Announcements
A reminder that the next exam will take place on Dec 3rd in the Bliss labs at 9:00 am. Exam will be closed book.
Problem 1. WordLengths.
Write a program that takes the name of input file as command line argument. The program should count
the number of letters in each word of the file (a “word” is any sequence of printable characters; this is
also formally called a “token”) and print a histogram of how many word lengths (how many contain each
number of letters). Your program should print the histogram in two ways: on the console (as shown on
page 522 of your text) or in a drawing window using vertical colored bars.
Problem 2. LongestSortedSequence
Write a program that reads a sequence of integers from the command line and prints the length of the
longest sorted (nondecreasing) sequence of integers in the array. For example, in the array [3, 8, 10, 1, 9,
-14, -3, 0, 14, 207, 56, 98, 12] the longest sorted sequence had four values -3, 0, 14, 207, so your
program should print 4 if passed this array. Sorted means nondecreasing so the sequence could contain
duplicates. The program should print 0 if the command line argument is empty. Make sure you
decompose the program into appropriate methods.
Problem 3. RemoveDuplicates.
Write a program RemoveDuplicates.java that takes three file names as command line arguments. The
first two files contain a list of integers (the first line of each file contain the number of integers in that
file). Your program should write to the third file the union of the numbers in the first two files (without
any duplicates). Hint: Store the contents of the files in arrays first.
Problem 4. Format
The unix fmt program reads lines of text, combining and breaking lines so as to create an output file with
lines as close to without exceeding 72 characters long as possible. The rules for combining and breaking
lines are as follows.
A new line may be started anywhere there is a space in the input. If a new line is started, there
will be no trailing blanks at the end of the previous line or at the beginning of the new line.
A line break in the input may be eliminated in the output, provided it is not at the end of a blank
or empty line and is not followed by a space or another line break. If a line break is eliminated, it
is replaced by a space.
Spaces never appear at the end of a line.
If a sequence of characters longer than 72 characters appears without a space or line break, it
appears by itself on a line.
Sample Input
The unix fmt program reads lines of text, combining
and breaking lines so as to create an
output file with lines as close to without exceeding
72 characters long as possible. The rules for combining and breaking
lines are as follows.
1. A new line may be started anywhere there is a space in the input.
If a new line is started, there will be no trailing blanks at the
end of the previous line or at the beginning of the new line.
Introduction to Programming (CMPS 200)
Fall 2011
2 of 2
2. A line break in the input may be eliminated in the output, provided
it is not followed by a space or another line break. If a line
break is eliminated, it is replaced by a space.
Sample Output
The unix fmt program reads lines of text, combining and breaking lines
so as to create an output file with lines as close to without exceeding
72 characters long as possible. The rules for combining and breaking
lines are as follows.
1. A new line may be started anywhere there is a space in the input.
If a new line is started, there will be no trailing blanks at the end of
the previous line or at the beginning of the new line.
2. A line break in the input may be eliminated in the output,
provided it is not followed by a space or another line break. If a line
break is eliminated, it is replaced by a space.
Problem 5. Transpose
Write a program Transpose.java that that reads a 2D array from a file and prints its transpose (rows
and columns interchanged) to standard output.
Your program should consist of the following methods:
// Create and return a 2D array read from standard input
public static int[][] readArray(String filename)
// Print a 2D array to standard output
public static void printArray(int[][] a)
// Transposes a 2D array in place
public static void transpose(int[][] a)
The input file that your program should read consist of two numbers representing the number of rows
(n) and number of columns (m) in the array, followed by nxm integers that represent the entries. For
example, consider the file matrix1.txt containing the following:
4 3
12 0 18 -1 2 3 7 8 0 -12 8 1
An invocation of the program using this input file might produce:
> java Transpose matrix1.txt
12 -1 7 -12
0 2 8 8
18 3 0 1