CSE 1310 - Practice Problems - Practice Set 3


Practice Problem 1

File problem1.java contains an incomplete program. The goal of the program is to go through an array of integers, and print out all integers that are greater than or equal to 50.

Complete that program, by defining a print_large function, that satisfies the following specs:

If you implement your function correctly, the program should print:
105
82
60


Practice Problem 2

File problem2.java contains an incomplete program. The goal of the program is similar to that of Problem 1: we want to go through an array of integers, and print out all integers that are greater than or equal to 50. However, here we do it a little bit differently: we do not hardcode the value 50 in the print_large function, but we pass it in as an argument.

Complete that program, by defining a print_large function, that satisfies the following specs:

If you implement your function correctly, the program should print:
105
82
60


Practice Problem 3

File problem3.java contains an incomplete program. The goal of the program is similar to that of Problems 1 and 2: we want to go through an array of integers, and print out all integers that are greater than or equal to 50. However, here we do it in a different way, by writing two functions: keep_large and print_array_list

Define a keep_large function, that satisfies the following specs:

Define a print_array_list function, that satisfies the following specs: If you implement your two functions correctly, the program should print:
105
82
60


Practice Problem 4

File problem4.java contains an incomplete program. The goal of the program is a little different from that of Problems 1, 2, 3. Here the data is stored in an array list, instead of an array. We still want to go through the data in that array list, and print out all integers that are greater than or equal to 50.

You should reuse the print_array_list function from the previous problem.

You should define a new keep_large function, different from your solution in the previous problems, that satisfies the following specs:

If you implement your function correctly, the program should print:
105
82
60


Practice Problem 5

File problem5.java contains an incomplete program. The goal of the program is to read integers stored in a file, and print out all integers that are greater than or equal to 50.

You should reuse the print_array_list and keep_large functions from previous problems.

You should define a new read_numbers function, that satisfies the following specs:

If you implement your function correctly, using file numbers1.txt, the program should print:
105
82
60


Practice Problem 6

Write a function reverse_lines(filename) that:

For example, suppose that, right before calling the function, file file1.txt has these contents:

hello
today is
Tuesday
Then, after calling reverse_lines("file1.txt"), file file1.txt should have these contents:
Tuesday
today is
hello


Practice Problem 7

Write a function foo(int A, int B) that satisfies these specs: For example:


Practice Problem 8

Write a function print_special(String S) that satisfies these specs: For example, print_special("Texas") prints this:
T
ee
xxx
aaaa
sssss 


Practice Problem 9

Write a function print_special_to_file(String S, String output_file) that creates a new file, whose name is specified by argument output_file, such that the contents of the file are the same as the output shown on the screen in the previous problem.

More specifically, the contents of the output file should satisfy these specs:

For example, print_special_to_file("Texas", "target.txt") creates a file called target.txt, whose contents are these:
T
ee
xxx
aaaa
sssss 


Practice Problem 10

Write a function check_letters(String A, String B), that satisfies these specs: For example:


Practice Problem 11

Write a function check_ab(String S), that satisfies these specs: In other words, the function counts how many times letter 'a' appears in S, (counting both upper and lower cases), and how many times letter 'b' appears in S (counting again both upper and lower cases), and checks whether 'a' appears as many times as 'b'. For example:


Practice Problem 12

Write a function check_ab_in_file(String filename), that satisfies these specs: In other words, the function counts how many times letter 'a' appears in the text file, (counting both upper and lower cases), and how many times letter 'b' appears in the text file (counting again both upper and lower cases), and checks whether 'a' appears as many times as 'b'.


Practice Problem 13

Write a function find_N(int number) that satisfies these specs: For example:


Practice Problem 14

Write a function subarray_sums(int[][] items) that satisfies these specs: For example:


Practice Problem 15

Write a function pick_elements(int[] items, int[] positions) that satisfies these specs: For example:


Practice Problem 16

Write a function insert(int[] items, int position, int value) that satisfies these specs: For example:


Practice Problem 17

Write a function a_after_star(String s) that counts and returns the number of times that character 'a' occurs AFTER the first occurrence of character '*'.

The function should return 0 if the string does not contain character '*' at all.

For example:


Practice Problem 18

Write a function a_between_stars(String s) that counts and returns the number of times that character 'a' occurs AFTER the first occurrence of character '*' and BEFORE the second occurrence of character '*'.

The function should return 0 if the string does not contain character '*' at all. If the string contains character '*' only once, the function should return the number of times that character 'a' occurs betweeh character '*' and the end of the string.

For example:


Practice Problem 19

We have files like towns.txt, whose contents look like this:
cat1chicago9234
dog3dallas01
sheep2boston4392897
donkey6denver4921109
Each line contains: Write a function print_towns(String filename) that satisfies these specs: For example, applied to file towns.txt, print_towns("towns.txt") prints this:
chicago
dallas
boston
denver


Practice Problem 20

Write a function check_sorted(int[] array) that: For example:


Practice Problem 21

Write a function check_squares(int[] array), that satisfies these specs: For example:


Practice Problem 22

Write a function count_equal(String filename, int col1, int col2), that satisfies these specs: For example, using file file1.txt:


Practice Problem 23

Write a function largest_average(String filename), that satisfies these specs: For example, using file file2.txt:


Practice Problem 24

Write a function max_length_position(String filename) that satisfies these specs: For example, using file file2.txt:


Practice Problem 25

Write a function print_combinations(String[] array1, String[] array2) that prints all pairs combining a value of array1 with a value of array2. For example, if a1 = {"red", "white", "green", "blue"} and a2 = {"shirt", "pants", "shoes"}, then print_combinations(a1, a2) should print:
red shirt
red pants
red shoes
white shirt
white pants
white shoes
green shirt
green pants
green shoes
blue shirt
blue pants
blue shoes


Practice Problem 26

Write a function multiplication_table(int k) that satisfies these specs: For example:


Practice Problem 27

Write a function subarray_sums(int[] array) that returns a 2-dimensional array of integers. The result should have a number of rows and a number of columns that are equal to the length of the input array. At position [i][j], the result should contain the sum of values array[i] + array[i+1] + ... + array[j]. If j < i, result[i][j] should be 0.

For example, if a1 = {7, 3, 8, 1, 2}, then subarray_sums(a1)should return array

{{7, 10, 18, 19, 21},
 {0,  3, 11, 12, 14},
 {0,  0,  8,  9, 11},
 {0,  0,  0,  1,  3},
 {0,  0,  0,  0,  2}}



CSE 1310 - Practice Problems - Practice Set 3