Commit a7400ae9 authored by Ari Trachtenberg's avatar Ari Trachtenberg
Browse files

Tweaked labs 0 and 1, added lab 2

parent 72fd46e7
Loading
Loading
Loading
Loading
+6 −6
Original line number Diff line number Diff line
@@ -38,14 +38,14 @@ string labOne_pFive(string str);
 * @return true if and only if [str] is a palindrome
 * @see https://en.wikipedia.org/wiki/Palindrome
 *
 * @example 1
 * @example 0
 * labOne_pSix("deed") == true because "deed" is the same forward as backward.
 *
 * @example 2
 * @example 1
 * labOne_pSix("deeds") == false because "deeds" backwards is "sdeed", and the
 *     two strings are different.
 *
 * @example 3
 * @example 2
 * labOne_pSix("aibophpobia") == true because the string is the same forward as backward.
 *
 */
@@ -57,15 +57,15 @@ bool labOne_pSix(string str);
 * @see https://en.wikipedia.org/wiki/Factorial
 * @see https://en.wikipedia.org/wiki/Factorial_number_system
 *
 * @example 1
 * @example 0
 * labOne_pSeven(3) returns 11 because:
 *    3 = 1*2! + 1*1!
 *
 * @example 2
 * @example 1
 * labOne_pSeven(10) returns 120 because:
 *    10 = 1*3! + 2*2! + 0*1!
 *
 * @example 3
 * @example 2
 * labOne_pSeven(463) returns 34101 because:
 *    463 = 3*5! + 4*4! + 1*3! + 0*2! + 1*1!
 */
+5 −5
Original line number Diff line number Diff line
@@ -10,14 +10,14 @@
 * @note You may use only *one* loop in your code - no
 *       nested loops.
 *
 * @example 1
 * @example 0
 * height: 3
 * width: 4
 * ****
 * ****
 * ****
 *
 * @example 2
 * @example 1
 * height: 1
 * width: 10
 * **********
@@ -33,7 +33,7 @@ void labOne_pZero();
 * @note: You must use *loops* to compute the component sums.
 * @see https://en.wikipedia.org/wiki/Square_number
 *
 * @example 1
 * @example 0
 * Max: 2
 * 5
 *
@@ -42,9 +42,9 @@ void labOne_pZero();
 *    b.  The sum of all squares between 1 and 2 (inclusive) (i.e., 1)
 *    c.  The sum of all positive powers of two less than or equal to 2 (i.e., 2)
 *
 * @example 2
 * @example 1
 * Max: 9
 * 47
 * 48
 *
 * This is because 20+14+13 = 48:
 *    a.  2+4+6+8 = 20
+63 −0
Original line number Diff line number Diff line
//
// Created by Ari Trachtenberg on 9/24/24.
//

// CHAPTER 7

/**
 * @param arr an array of at least three double values
 * @param size The size of [arr], which must be >=3
 * @return A pointer to the third largest value of [arr]
 *
 * @example 0
 * int A[] = {1,5,3,2}
 * labTwo_pThree(A,4) returns a pointer to 2 (i.e., A+3) because it is
 * the third largest element in the array.
 *
 * @example 1
 * int B[] = {3,1,4,1,5,9,2,6,5,3,5,6}
 * labTwo_pThree(A,12) returns a pointer to 6 (i.e., either A+7 or A+11).
 */
double* labTwo_pThree(double* arr, int size);


/**
 * @param arr1 An array of characters, terminated with a null character '\0'.
 * @param arr2 Another null-terminated array of characters.
 * @param result An unallocated array of characters.
 *
 * This method concatenates [arr1] and [arr2] into [result]. The array
 * [result] must be sufficiently allocated with "new" to contain the non-null
 * contents of [arr1], [arr2], and a terminating null character.
 */
void labTwo_pFour(const char arr1[], const char arr2[], char result[]);

/**
 * @param src A null-terminated array of characters, representing the source string.
 * @param seek A null-terminated array of characters, representing the pattern to seek.
 * @param replace A null-terminated array of characters, representing the replacement pattern.
 * @return A new null-terminated array of characters that is a copy of [src], but with
 *    each contiguous substring [seek] replaced by [replace].
 *
 * @example 0
 * const char *src0 = "Hatty";
 * const char *seek0 = "t";
 * const char *replace0 = "p";
 * labTwo_pFive(src0, seek0, replace0) returns "Happy", because the "t"'s have been
 * replaced by "p"'s in "Hatty".
 *
 * @example 1
 * const char *src1 = "Hatty";
 * const char *seek1 = "tt";
 * const char *replace1 = "ppil";
 * labTwo_pFive(src1, seek1, replace1) returns "Happily", because the
 * "tt" is replaced with "pp".
 *
 * @example 2
 * const char *src2 = "Happy";
 * const char *seek2 = "p";
 * const char *replace2 = "";
 * labTwo_pFive(src2, seek2, replace2) returns "Hay", because the "p" are each replaced
 * with the empty string "".
 */
char *labTwo_pFive(const char src[], const char seek[], const char replace[]);
 No newline at end of file
+64 −4
Original line number Diff line number Diff line
@@ -5,7 +5,7 @@
// CHAPTER 6

/**
 * @param arr An array of integers
 * @param arr An array of [size] integers
 * @param size The number of integers stored in array [arr]
 * @return The sum of the following for [arr]:
 *    * Every element at even index.
@@ -14,16 +14,76 @@
 *
 * @example 1
 * int test[] = {1,2,3};
 * labSix_pZero(test,3) returns 9 as the sum of:
 * labTwo_pZero(test,3) returns 9 as the sum of:
 *    * 1+3 (elements at even index)
 *    * 2 (even elements)
 *    * 3 (first*last element)
 *
 * @example 2
 * int test2[] = {1,2,3,4};
 * labSix_pZero(test,5) returns 14 as the sum of:
 * labTwo_pZero(test,4) returns 14 as the sum of:
 *    * 1+3 (elements at even index)
 *    * 2+4 (even elements)
 *    * 4 (first*last element)
 */
int labSix_pZero(int arr[], int size);
 No newline at end of file
int labTwo_pZero(int arr[], int size);


/**
 * @param arr1 An array of [size] integers
 * @param arr2 An array of [size] integers
 * @param size The size of arrays [arr1] and [arr2]
 * @return true iff arr1 and arr2 have the same elements (in any order),
 *    with the same multiplicities (in the case of duplicate elements).
 *
 * @example 1
 * int A1[] = {1,4,9,16,9,7,4,9,11}
 * int A2[] = {11,1,4,9,16,9,7,4,9}
 * labTwo_pOne(A1,A2,9) returns true because both arrays contain the
 *    elements 1, 4 (appears twice), 7, 9 (appears 3 times), 11
 *
 * @example 2
 * int B1[] = {1,4,9,16,9,7,4,9,11}
 * int B2[] = {11,11,7,9,16,4,1,4,9}
 * labTwo_pOne(B1,B2,9) returns false because:
 *    * B1 contains only one 11, and B2 contains 2
 *    * B2 contains 16, which B1 does not
 */
bool labTwo_pOne(int arr1[], int arr2[], int size);


/**
 * @param arr An array of the integers 1 ... size, in some order
 * @param size The size of array [arr], which must also be a perfect square.
 * @return true iff arr, arranged row-by-row into a sqrt(size) x sqrt(size) matrix,
 *    produces a magic square.
 * @see https://en.wikipedia.org/wiki/Magic_square - A magice square is a matrix
 *    each of whose rows, columns, and both diagonals sum to the same value.
 *
 * @example 0
 * int A[] = {1,2,3,5}
 * labTwo_pTwo(A,4) returns false, because A is arranged into the following matrix:
 *    1 2
 *    3 5
 * The sum of the first row is 3, but the sum of the second row is 8.
 * Similarly, the sum of the diagonal from top-left to bottom right is 6, whereas the
 * sum of the contrary diagonal is 5.
 *
 * @example 1
 * int A[] = {2,7,6,9,5,1,4,3,8}
 * labTwo_pTwo(A,9) returns true, because A is arranged into the following matrix:
 *    2 7 6
 *    9 5 1
 *    4 3 8
 *  The sum of any row, column, or diagonal of the matrix equals 15.
 *
 *  @example 2
 *  int B[] = {16,3,2,13,5,10,11,8,9,6,7,12,4,15,14,1}
 *  labTwo_pTwo(A,16) returns true, because A is arranged into the following matrix:
 *     16  3  2 13
 *      5 10 11  8
 *      9  6  7 12
 *      4 15 14  1
 *   The sum of any row, column, or diagonal of the matrix equals 15.
 */
bool labTwo_pTwo(int arr[], int size);
 No newline at end of file
+2 −2
Original line number Diff line number Diff line
@@ -22,14 +22,14 @@ void labZero_pThree();
 *    four points among the following choices:
 *    square, rectangle, trapezoid, rhombus, none of these
 *
 * @example 1
 * @example 0
 * 0 0
 * 0 1
 * 1 1
 * 1 0
 * square
 *
 * @example 2
 * @example 1
 * 0 0
 * 0 1
 * 1 1
Loading