CodeCheck® C++ Exercises

Assignments

With any of these assignments, click on the Clone button to use it in your class.

Programming Problems

Branches

Branches Without Functions

  1. Given an input string, print the string with the first and last letter removed if they were equal, or the original string if they were not. CodeCheck
  2. On a chessboard, positions are marked with a letter between a and h for the column and a number between 1 and 8 for the row. Given an input string with a letter and number, print whether it is in a corner, at the border, or in the inside of the chess board. CodeCheck
  3. On a chessboard, fields are marked with a letter between a and h for the column and a number between 1 and 8 for the row. Given a position string, print the color of the field (white or black). CodeCheck
  4. Given the x- and y-coordinates of a point, print whether the point lies in the first, second, third, or fourth quadrant plane, or on the x-axis, or on the y-axis, or the origin. CodeCheck
  5. Given three numbers x, y, z, determine if one of them is the sum of the two others, and then print out the equation. CodeCheck
  6. Given three numbers x, y, z, print out how whether they are all distinct, two are the same, or all three are the same. CodeCheck
  7. Given two integers x and y, print their sum, but if it is more than 100 or less than 0, print "out of range". CodeCheck
  8. Given three integers x, y, z, print the sum of the odd integers. CodeCheck
  9. Given three input strings, print all but the shortest one. CodeCheck
  10. Given an actual time and an alarm clock time, both in “military” format (such as 0730 for 7:30am), print how many more minutes before the alarm rings. But if the time is after the alarm, print "Alarm already went off". CodeCheck
  11. Repeat the preceding exercise for U.S. time (e.g. 7:30am). CodeCheck
  12. Given a point with integer coordinates (x, y) and a compass direction N, E, S, W, print the neigboring lattice point. CodeCheck
  13. Repeat with compass directions NW, NE, SW, SE. CodeCheck

Branches with Functions

  1. Given two numbers, return true if they both have the same sign. CodeCheck
  2. Write a function that checks whether two points with coordinates x1, y1 and x2, y2 are in the same quadrant. CodeCheck
  3. Given two numbers, return their distance if it less than 10, or 10 otherwise. CodeCheck
  4. On a chessboard, positions are marked with a letter between a and h for the column and a number between 1 and 8 for the row. Given two position strings, return true if they have the same color. CodeCheck
  5. Given three numbers x, y, z and a Boolean variable strict, return true if strict is false and x ≤ y ≤ z, or strict is true and x < y < z. CodeCheck
  6. Given four numbers a, b, c, d, return true if exactly two of them are the same. CodeCheck
  7. Given three numbers, return true if they are evenly spaced; that is, sorted in ascending or descending order, with the same distance between them. CodeCheck
  8. Repeat for four numbers. Hint: Use the previous problem. CodeCheck
  9. Given numbers x, y, and target, return whichever of x and y is closer to the target. If they have the same distance, return the smaller of the two. CodeCheck

Strings

No Loops

  1. Given a string, return the string with the first and last characters swapped. CodeCheck
  2. Given a string, return the string with the middle two characters removed if it has even length, and the middle character removed if it has odd length. CodeCheck
  3. Given a string, return the string with the middle two characters swapped if it has even length, and the middle three characters swapped if it has odd length. CodeCheck
  4. Given a string, return the string with the first and second half swapped. If the string has odd length, leave the middle character in place. CodeCheck
  5. Given a string, return the string with the first half and the second half doubled. For example, Java becomes JaJavava and Hello becomes HelHellolo. CodeCheck

Comparing Strings

  1. Given two strings s and t, return the first position where their characters differ, or -1 if they are identical. CodeCheck
  2. Given two strings s and t, return the first position where their characters are the same, or -1 if there is no such position. CodeCheck
  3. Given a string s, count how many times the ith character from the front equals the ith character from the back. CodeCheck

Finding Substrings

  1. Given strings s and t, return a list of all positions in which t occurs in s. CodeCheck
  2. Given strings s and t, return a string in which all occurrences of t are removed from s. CodeCheck
  3. Given strings s and t, return a string in which all characters in t are removed from s. CodeCheck
  4. Given a string s, return the longest prefix that is repeated somewhere else in the string. CodeCheck
  5. Given a string s, find the longest prefix that also occurs at the end (so that s = p + t + p), and return t, the string without the beginning and the end. CodeCheck

Words

  1. Given a string in which words are separated by spaces, return the longest word. CodeCheck
  2. Given a string in which words are separated by spaces, return the first word that is duplicated (such as an accidental “this this”). CodeCheck
  3. Given a string in which words are separated by spaces, return the first word that is repeated somewhere in the string. CodeCheck
  4. Given a string in which words are separated by spaces, and two words a and b, interchange all occurrences of a and b. CodeCheck

Numbers in Strings

  1. Given a string, find the first integer inside and return its value as an integer. Return 0 if there is no integer inside. CodeCheck
  2. Given a string, find the sum of all integers inside. Return 0 if there is no integer inside. CodeCheck
  3. Given a string, find all integers inside and return a string that separates them by commas. CodeCheck

Other String Operations

  1. Given a string s and an integer n, return a string in which each of the characters in s is repeated n times. CodeCheck
  2. Given a string s, return the first vowel that is doubled in the string. CodeCheck
  3. Given a string s, return the string with adjacent duplicates removed. For example, Mississippi yields Misisipi. CodeCheck
  4. Given a string s and a string t, return a string in which all the characters of s that occur in t have been replaced by a _ sign. CodeCheck
  5. Given a string, return a string composed first of all characters at even positions and then of all characters at odd positions. CodeCheck
  6. Write a method that undoes the effect of the preceding exercise. CodeCheck
  7. Given a string and two characters x and y, interchange all occurrences of x and y. CodeCheck
  8. Given a string s and a character c, return a string with the characters surrounding the first of c reversed. For example, if s is Hello-World and c is -, return HellW-oorld. CodeCheck
  9. Given a string s and a character c, return a string with the characters surrounding any occurrence of c reversed. For example, if s is Hello beautiful world and c is a space, return Hellb oeautifuw lorld. CodeCheck

Arrays - Simple Exercises

No loops

  1. Given an array of integers of length ≥ 4, return the average of the first two and last two elements. CodeCheck
  2. Given an array of integers of length ≥ 3, return the average of the first, last, and middle element (or the two middle elements if the length is even). CodeCheck
  3. Given an array of integers, swap the first and last elements. CodeCheck
  4. Given an array of integers, swap the first two and the last two elements. CodeCheck

Filling

  1. Given an integer n, fill an array of length n with 1 -1 1 -1 ... CodeCheck
  2. Given integers n and k, fill an array of length n with 0 1 2 ... k-1 0 1 2 ... k-1 ... CodeCheck
  3. Given integers a and b, fill an array with a, a + 1, a + 2, ..., b. CodeCheck
  4. Given integers a and b, fill an array containing all even numbers that are at least a and at most b. CodeCheck
  5. Given an integer n, fill an array with 1 2 2 3 3 3 4 4 4 4 ... and finally n repeated n times. CodeCheck

Maximum and Minimum

  1. Given an array of integers, return the position of the first occurrence of the largest element. CodeCheck
  2. Given an array of integers, return the position of the last occurrence of the largest element. CodeCheck
  3. Given an array of integers, return the difference of the positions of the first and the last occurrence of the largest element. CodeCheck
  4. Given an array of integers, return the difference between the maximum and minimum. CodeCheck
  5. Given an array of integers, swap the the maximum and minimum. CodeCheck
  6. Given an array of integers, return the second-largest element.
  7. Given an array of element, find the shortest distance between two consecutive elements. CodeCheck

Finding Elements

  1. Given an array of integers and a value, return the position of the last occurrence of the value in the array, or -1 if the value does not exist. CodeCheck
  2. Given an array of integers and a value, return the difference between the last and first position of the value in the array, or -1 if the value does not exist. CodeCheck
  3. Given an array of integers and a value, fill an array with all positions of the value in the array. CodeCheck
  4. Given an array of integers and a value, return the position of the element that is closest to the value. If there is more than one, return the position of the first one. CodeCheck
  5. Given two arrays of integers of the same length, return the first position where their elements are the same. CodeCheck
  6. Given two arrays of integers of the same length, return the first position where their elements differ. CodeCheck

Counting Elements

  1. Given an array of integers, count how many elements are negative. CodeCheck
  2. Given an array of integers and two values a and b, count how many elements are at least a and at most b. CodeCheck
  3. Given an array of integers, return the most frequent element. If there is more than one element with maximum frequency, return the one that occurs first in the array. CodeCheck
  4. Given an array of integers, return an array of all elements that occur with maximum frequency. CodeCheck
  5. Given an array of integers, return the position of the first element that occurs more than once, or -1 if all elements occur exactly once. CodeCheck
  6. Given an array of integers, return an array of all elements that occur exactly once. CodeCheck
  7. Given an array of integers, remove all elements that occur more than once, leaving the first occurrence. CodeCheck
  8. Given two arrays of integers a and b of the same length, count the number of positions where the arrays have the same elements. CodeCheck
  9. Given two arrays of integers a and b of the same length, count the number of positions where the array elements differ by at least 2. CodeCheck
  10. Given an array a, count how many times the ith element from the front equals the ith element from the back. CodeCheck

Sums, Averages, Products

  1. Given an array of integers, return the sum of all positive elements. CodeCheck
  2. Given an array of integers, return the sum of all odd elements. CodeCheck
  3. Given an array of integers and two values a and b, return the sum of all elements that are at least a and at most b. CodeCheck
  4. Given an array of integers, return the average of all positive elements. CodeCheck
  5. Given an array of strings and a string s, return the average length of all strings containing s. CodeCheck
  6. Given an array of integers, return the product of all non-zero elements. CodeCheck
  7. Given an array of integers, give the smallest n so that the sum of the first n elements is at least as large as the sum of the remaining elements. CodeCheck
  8. Given an array of integers, give the largest n ≤ length / 2 so that the sum of the first n elements equals the sum of the last n elements. CodeCheck
  9. Given an array of integers, give the largest sum of n consecutive elements. CodeCheck
  10. Given an array of integers, find the largest sum of two different elements in the array. CodeCheck
  11. Given an array of floating-point numbers, return an array of the averages of consecutive element pairs of the original array. CodeCheck
  12. Given an array of integers and an integer n, return an array of the averages of n consecutive elements of the original array. CodeCheck
  13. Given an array of integers, return the average of all elements that are not the maximum or minimum. CodeCheck
  14. Given an array of integers, replace each element with the average of its neighbors. CodeCheck

Moving or Removing Elements

  1. Given an array of integers and an integer n, remove all elementts > n or < -n. Shift all remaining elements to the front, and return the number of removed elements. CodeCheck
  2. Given an array of integers, move all zeroes to the back. CodeCheck
  3. Given an array of integers, move all zeroes to the front. CodeCheck
  4. Given an array of integers, move all negative numbers to the front and all positive numbers to the back, keeping their relative order. CodeCheck
  5. Given an array of integers, remove all even elements that occur exactly twice. CodeCheck
  6. Given an array of integers, remove all adjacent duplicates. CodeCheck

Two Answers

  1. Given an array of integers, return an array of length 2 containing the first and last element, in sorted order. CodeCheck
  2. Given an array of integers, return an array of length 2 containing the minimum and the maximum. CodeCheck
  3. Given an array of integers, return an array of length 2, with the number of positive and the number of negative elements in the array. CodeCheck
  4. Given an array of integers, return an array of length 2, each being an array. The first one holds the positive elements and the second one holds the negative elements of the original array. CodeCheck
  5. Given an array of integers, return an array of length 2, each being an array. The first one holds the elements with even positions, and the second one holds the elements with odd positions of the original array. CodeCheck

Double Loops

  1. Given two arrays a and b, remove all elements of b from a. CodeCheck
  2. Given two arrays a and b, return an array that contains all elements occurring in a but not in b. CodeCheck
  3. Given an array a, return the longest n so that the first n elements equal the last n elements. CodeCheck
  4. Given an array of integers, return the largest sum of consecutive elements. CodeCheck
  5. Given an array of integers, return the largest sequence whose reverse also occurs somewhere in the array. CodeCheck
  6. Given an array a of integers, return an array of all elements of a that occur exactly once. CodeCheck
  7. Given an array of integers, return an array of arrays of length 2, containing each unique element together with its frequency. CodeCheck
  8. Given an array of integers, return the position of the longest subsequence of consecutive integers a, a + 1, a + 2, ... CodeCheck

Two-Dimensional Arrays

No Loops

  1. Given a two-dimensional array of integers, return the average of the four corner elements. CodeCheck
  2. Given a two-dimensional array of integers, return the middle element if both the row and column lenghts are odd, or the average of the two or four middle elements of one or both is even. CodeCheck
  3. Given a two-dimensional array of integers and a string N, E, S, W, give the middle element along the border in the given compass direction, or the average of the two middle ones if the border length is even. CodeCheck
  4. Given a two-dimensional array of integers and a string NW, NW, SW, SE, return the sum of the four elements in the given compass direction. CodeCheck
  5. Given a two-dimensional array of integers, row/column indexes r, c, and a string N E S W, return the neighbor in that direction, or 0 if it doesn't exist. CodeCheck
  6. Repeat with NW NE SW SE. CodeCheck
  7. Given a two-dimensional array of integers and row/column indexes r, c, return the number of neighbors (not counting the element itself). A neighbor is any neighbor in the compass direction N E S W NE NW SE SW. CodeCheck
  8. On a chessboard, positions are marked with a letter between a and h for the column and a number between 1 and 8 for the row. Given an 8x8 array of integers and a string with such a position, return the element at that position. CodeCheck

Loops Along a Row or Column

  1. Given a two-dimensional array of integers and a row index, return the largest element in that row. CodeCheck
  2. Given a two-dimensional array of integers and a column index, return the smallest element in that column. CodeCheck
  3. Given a two-dimensional array of integers, an index, and a string R or C, return the sum of the elements in that row or column. CodeCheck
  4. Given a two-dimensional array of integers, a row index r, and a column index c, return the sum of all elements in the given row and column. The element where the row and column meet should only be included once. CodeCheck
  5. Given a two-dimensional array of integers and a string N, E, S, W, return the sum of all elements of the border in the given compass direction. CodeCheck
  6. Given a two-dimensional array of integers, return the sum of all elements along the north and south border. CodeCheck
  7. Given a two-dimensional array of integers, return the sum of all elements along all borders but not the corners. CodeCheck
  8. Given a two-dimensional array of integers, return the sum of all elements along all borders including the corners. CodeCheck
  9. Given a two-dimensional square array of integers, return the sum of the elements along both diagonals. Make sure not to count the center twice. CodeCheck
  10. Given a two-dimensional array of integers and a row index r, reverse the given row. CodeCheck
  11. Given a two-dimensional square array of integers, reverse both diagonals. CodeCheck

Looping Over the Entire Array

  1. Given a two-dimensional array of integers, compute the sum of all positive elements. CodeCheck
  2. Given a two-dimensional array of integers, compute the sum of all elements that are not one of the borders. CodeCheck
  3. Given a two-dimensional square array of integers, compute the sum of all elements that are not on one of the diagonals. CodeCheck
  4. Given a two-dimensional array of integers, replace all negative elements with zero. CodeCheck
  5. Given a two-dimensional array of integers, reverse all rows. CodeCheck
  6. Given a two-dimensional array of integers, shift each row by one to the right and put a 0 at the leftmost column. The rightmost column is lost. CodeCheck
  7. Given an r x c array of integers, return an (r + 2) x (c + 2) array whose borders are zero and whose interior is the original array. CodeCheck
  8. Given a two-dimensional array of integers, return an array of the largest element in each row. CodeCheck
  9. Given a two-dimensional array of integers, return an array of the number of negative elements in each column. CodeCheck
  10. Given a two-dimensional array of integers, return the largest element in the array. CodeCheck
  11. Given a two-dimensional array of integers, return the position of the smallest element in the array as an array of length 2. CodeCheck
  12. Given a two-dimensional array of integers and a value x, return the position of the first match as an array of length 2. Return [-1, -1] if the value does not occur in the array. CodeCheck
  13. Given a two-dimensional array of integers and a value x, return the positions of all matching elements, as an array of arrays of length 2. CodeCheck

Looping over Neighbors

  1. Given a two-dimensional array of integers and row/column positions r, c, return the largest neighbor in the compass directions N E S W. CodeCheck
  2. Given a two-dimensional array of integers and row/column positions r, c, return the largest neighbor in the compass directions N E S W NE NW SE SW. CodeCheck
  3. Given a two-dimensional array of integers and row/column positions r, c, return the compass direction of the largest neighbor as a string N E S W NE NW SE SW. CodeCheck
  4. Given a two-dimensional array of integers, row/column positions r, c, and a value x, fill the given element and its neighbors in the compass directions N E S W NE NW SE SW with x. CodeCheck
  5. Given a two-dimensional array a of integers and row/column positions r, c, return a 3x3 array whose middle element is a[r][c], and whose neighbors are copied from the original. Set non-existent neighbors to zero. CodeCheck

Producing 2D Arrays

  1. Given an integer n, produce the array
    0 1 2 3 ... n
    1 2 3 4 ... 0
    ...
    n 0 1 2 ... n-1
    
    CodeCheck
  2. Given two two-dimensional arrays a and b of integers with the same number of rows and columns, return an array of the same size where each element is the greater of the corresponding elements in the two arrays. CodeCheck
  3. Repeat the preceding exercise where the array dimensions need not match, filling positions that exist in neither array with 0. CodeCheck
  4. Given two two-dimensional arrays of integers with the same number of rows and columns, return the first position where they do not match, as an array of length 2. CodeCheck
  5. Given two two-dimensional arrays with the same number of rows, return an array that places them side by side. CodeCheck
  6. Repeat where the row lengths don't have to match. Fill the shorter array with zeroes. CodeCheck
  7. Given a two-dimensional array a, return an array whose [i][j] element is the average of the neighbors of a[i][j] in the N, E, S, W direction. CodeCheck

Complex Loops

  1. Given a two-dimensional array of integers, return the number of distinct elements. CodeCheck
  2. Given a two-dimensional array of integers, remove any adjacent duplicate rows by filling the duplicates with zeroes. CodeCheck
  3. Given a two-dimensional array of integers, return the index of the first row that is entirely filled with CodeCheckzeroes.
  4. Given a two-dimensional array of integers, return the top left corner of the first subarray of the form
    0 0
    0 0
    CodeCheck
  5. Given a two-dimensional array of integers, return the position of the largest square subarray filled with zeroes, as an array of length 2. Return [-1, -1] if there is no such subarray. CodeCheck
  6. Repeat the previous exercise with subarrays that are not necessarily square. CodeCheck

Bug Report Form

Your name:

Your email address:

Problem description:

To protect against spam robots, please answer this simple math problem:
* =