CSE 1310 - Practice Problems - Practice Set 3


Practice Problem 1, variation a

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 1, variation b

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 2

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 3

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


Practice Problem 4

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


Practice Problem 5

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


Practice Problem 6

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


Practice Problem 7

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


Practice Problem 8

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 9

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


Practice Problem 10

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}}