Question

Sometimes when I sit in restaurants waiting on food, I request the children’s menu to play...

Sometimes when I sit in restaurants waiting on food, I request the children’s menu to play the games. One of my favorites is the word puzzle in which you search for words hidden in a scramble of letters. I began to work out a solution to this game programmatically. That seemed a little simple, so I decided to have you solve the problem of a scramble of integers, finding the sub-row or sub-column which sums to another integer.
You will provide me with a library containing (at least) the function FindSum as demonstrated in the file sum finder.cc. You should read the header file as well as my test file to understand what is expected from the library. Do note that the preconditions keep you from any unpleasantness due to array bounds and the like.
I have provided you a basic test application which you can use to ensure that your code is, at least partially, correct. I would suggest a more rigorous testing scheme to ensure that your methods handle any edge cases.
Late assignments will lose 10% per day late, with no assignment begin accepted after 3 days (at 100% reduction in points).
You will receive 1.5 points for each if your FindSum function finds:
• horizontal summation left-to-right,
• horizontal summation right-to-left,
• vertical summations top-to-bottom,
• vertical summations bottom-to-top,
• that the summation does not exist (2 points)

Edit: given a 2d array of ints find two of them that sum up to another integer you entered. Look at the header file for more detailed info. The two files at the bottom are used to test the code.

Main Program Header file

#include
// using size_t


/* The function accepts an integer, an integer matrix, a pair of unsigned
* integrals in a size_t array representing matrix size, and an unsigned
* integral type output parameter storing the number of sums found. It returns
* an n x 2 matrix with the indicies of any sums found.
*
* The function searches the columns, diagonals, and rows of the matrix
* parameter for a series of integers which sum to the provided sum. It returns
* the number sums found in the output parameter and pairs of any indices as
* the function's return value.
*
* Preconditions:
* - All array parameters are allocated,
* - The matrix array's dimensions are correctly represented in the two
* elements of the array matrix_size.
*
* Parameters:
* - sum: the integer sum begin sought
* - matrix: a two-dimension integer array
* - matrix_size: a two-element size_t array with index 0 storing the number
* of rows, and index 1 storing the number of columns in the matrix.
* See kRow and kCol below.
* - sums_found: an output parameter to store the number of summations found
*
* Postconditions:
* - The output parameter sums_found corresponds to the number of summations
* found.
* - The output parameter sums_found holds zero if no summations were found.
*
* Returns:
* - Returns an unsigned integral type matrix of size n x 4, where n is the
* value in sums_found. If any summations are found, row the the matrix
* shall correspond to the following:
* {start row, start col, end row, end col}
* The returned values must be of the form:
* start row <= end row AND start column <= end column
* - Returns the nullptr if no summations were found.
*/
//val grind?
size_t** FindSum(
int sum, const int** matrix, const size_t matrix_size[],
size_t* sums_found);


const size_t kRow = 0; // maps to row of matrix_size
const size_t kCol = 1; // maps to col of matrix_size
const size_t kStartRow = 0; // maps to index of starting row in indices
const size_t kStartCol = 1; // maps to index of starting colum in indices
const size_t kEndRow = 2; // maps to index of ending row in indices
const size_t kEndCol = 3; // maps to index of ending colum in indices

#endif //NOLINT

Code below is the test files code

#ifndef HW3_INC_TEST_WORD_FINDER_H_  //NOLINT
#define HW3_INC_TEST_WORD_FINDER_H_  //NOLINT


#include 
// using size_t
#include 
using std::cin;
using std::cout;
using std::endl;
#include 
using std::stringstream;
#include 
using std::string;

#include   //NOLINT


const size_t kRows = 5;
const size_t kCols = 5;
const int kTest_matrix[kRows][kCols] = {
  {-1,  57,   17, -81,  47},
  {29, -61,   12,  10, 232},
  {47,  17, -101,  76,  -2},
  {-9,  69,  -99, 511,  94},
  {38, -89,  372, 267, -211}
};
const size_t kIndices_size = 4;


/* Used to generate a new dyamic array from the above fixed-size automatic
 * array.
 */
const int** AutoToDynamic(const int auto_array[kRows][kCols]);
void Delete(const int** dyna_array, size_t size);
void Delete(size_t** dyna_array, size_t size);


/* Compare two sets of indices to ensure they are equal.
 * Preconditions: two input parameters are allocated as arrays, each containing
 *   four elements
 */
bool IndicesEqual(const size_t expected[][kIndices_size], const size_t** actual,
  size_t size);

/* Converts a set of indices to a string for printing.
 * Preconditions: input parameter must be allocated as an array with four
 *   elements
 */
string ToString(const size_t*);

bool TestSummation(int, size_t, const size_t[][kIndices_size]);

#endif  //NOLINT

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include 


int main(int argc, char* argv[]) {
  std::cout << "TESTING ROW FindSum:" << std::endl;
  size_t expected_row_indices[][4] = {
    {0, 0, 0, 1},
    {4, 3, 4, 4}
  };
  if (TestSummation(56, 2, expected_row_indices)) {
    std::cout << "  PASSED" << std::endl;
  } else {
    std::cout << "  FAILED" << std::endl;
    return 1;
  }

  std::cout << "TESTING COLUMN FindSum:" << std::endl;
  size_t expected_col_indices[][4] = {
    {1, 3, 2, 3},
    {2, 1, 3, 1}
  };
  if (TestSummation(86, 2, expected_col_indices)) {
    std::cout << "  PASSED" << std::endl;
  } else {
    std::cout << "  FAILED" << std::endl;
    return 1;
  }

  return 0;
}


bool TestSummation(
    int sum, size_t size, const size_t expected_indices[][kIndices_size]) {
  // create matrix to pass to FindSum
  const int **matrix = AutoToDynamic(kTest_matrix);
  // create matrix size for FindSum
  const size_t matrix_size[] = {kRows, kCols};

  size_t sums_found = static_cast(-1);  // space for output param
  size_t **actual_indices = FindSum(sum, matrix, matrix_size, &sums_found);
  const size_t ** const_actual = const_cast(actual_indices);

  if (sums_found == 0 || !actual_indices) {
    std::cout << "  No summation found for sum: " << sum << std::endl;
    Delete(matrix, kRows);
    return false;
  }

  if (sums_found != size) {
    std::cout << "  Expected sums found: "
        << size
        << ", Actual sums found: " << sums_found << std::endl;

    Delete(matrix, kRows);
    Delete(actual_indices, sums_found);
    return false;
  }

  if (sums_found == 0 || !actual_indices) {
    std::cout << "  No summation found for sum: " << sum << std::endl;
    Delete(matrix, kRows);
    Delete(actual_indices, sums_found);
    return false;
  }

  if (sums_found != size) {
    std::cout << "  Expected sums found: "
        << size << ", Actual sums found: " << sums_found << std::endl;

    Delete(matrix, kRows);
    Delete(actual_indices, sums_found);
    return false;
  }

  if (!IndicesEqual(expected_indices, const_actual, sums_found)) {
    Delete(matrix, kRows);
    Delete(actual_indices, sums_found);
    return false;
  }

  Delete(matrix, kRows);
  Delete(actual_indices, sums_found);
  return true;
}


const int** AutoToDynamic(const int auto_array[kRows][kCols]) {
  int **dyna_array = new int*[kRows];
  for (size_t i = 0; i < kRows; ++i) {
    dyna_array[i] = new int[kCols];

    for (size_t j = 0; j < kCols; ++j)
      dyna_array[i][j] = auto_array[i][j];
  }

  return const_cast(dyna_array);
}


void Delete(const int** dyna_array, size_t size) {
  for (size_t i = 0; i < size; ++i)
    delete [] dyna_array[i];
  delete [] dyna_array;
}

void Delete(size_t** dyna_array, size_t size) {
  for (size_t i = 0; i < size; ++i)
    delete [] dyna_array[i];
  delete [] dyna_array;
}


bool IndicesEqual(
    const size_t expected[][kIndices_size], const size_t** actual,
    size_t size) {
  bool *found = new bool[size];
  for (size_t i = 0; i < size; ++i) {
    found[i] = false;
    for (size_t j = 0; j < size; ++j) {
      // NOTE: this method was suggested by Matthew Shelly
      if (actual[i][kStartRow] == expected[j][kStartRow]
          && actual[i][kStartCol] == expected[j][kStartCol]
          && actual[i][kEndRow] == expected[j][kEndRow]
          && actual[i][kEndCol] == expected[j][kEndCol]) {
        found[i] = true;
        break;
      }
    }
  }
  for (size_t i = 0; i < size; ++i)
    if (!found[i]) {
      std::cout << "  " << ToString(expected[i])
        << " NOT FOUND" << std::endl;
      delete [] found;
      return false;  // one of the index pairs was not found
    }
  delete [] found;
  return true;
}


string ToString(const size_t* indices) {
  stringstream sout;
  sout << "(" << indices[kStartRow] << ", " << indices[kStartCol] << ") - ("
    << indices[kEndRow] << ", " << indices[kEndCol] << ")";
  return sout.str();
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1


#include <iostream>
using namespace std;
using std::cin;
using std::cout;
using std::endl;
using std::string;


const SIZE_T Row = 5;
const SIZE_T Col = 5;
const int originalTest_matrix[Row][Col] = {
{-16, 57, 47, -71, 87},
{290, -61, 12, 10, 292},
{476, 17, -101, 76, -2},
{-9, 69, -99, 519, 98},
{38, -79, 372, 247, -216}
};
const SIZE_T originalIndices_size = 4;


/* generate dynamic array from fixed size array
*/
const int** FixedToDynamic(const int fixed_array[Row][Col]);
void Delete(const int** dynamic_array, SIZE_T size);
void Delete(SIZE_T** dynamic_array, SIZE_T size);


/* two sets of indices are compared.
* input parameters are defined with foru parameters
*/
bool IndicesEqual(const SIZE_T expected[][originalIndices_size], const SIZE_T** actual,
SIZE_T size);

/* Convert a set of indices to a string */
string ToString(const SIZE_T*);

bool Summation_value(int, SIZE_T, const SIZE_T[][originalIndices_size]);


int main(int argc, char* argv[]) {
std::cout << "TESTING ROW FindSum:" << std::endl;
SIZE_T expected_row_indices[][4] = {
{0, 0, 0, 1},
{4, 3, 4, 4}
};
if (Summation_value(56, 2, expected_row_indices)) {
std::cout << " PASSED" << std::endl;
} else {
std::cout << " FAILED" << std::endl;
return 1;
}

std::cout << "TESTING COLUMN FindSum:" << std::endl;
SIZE_T expected_col_indices[][4] = {
{1, 3, 2, 3},
{2, 1, 3, 1}
};
if (Summation_value(86, 2, expected_col_indices)) {
std::cout << " PASSED" << std::endl;
} else {
std::cout << " FAILED" << std::endl;
return 1;
}

return 0;
}


bool Summation_value(
int sum, SIZE_T size, const SIZE_T expected_indices[][originalIndices_size]) {
// create matrix to pass to FindSum
const int **matrix = FixedToDynamic(originalTest_matrix);
// create matrix size for FindSum
const SIZE_T matrix_size[] = {Row, Col};

SIZE_T sums_found = static_cast(-1); // space for output param
SIZE_T **actual_indices = FindSum(sum, matrix, matrix_size, &sums_found);
const SIZE_T ** const_actual = const_cast(actual_indices);

if (sums_found == 0 || !actual_indices) {
std::cout << " No summation found for sum: " << sum << std::endl;
Delete(matrix, Row);
return false;
}

if (sums_found != size) {
std::cout << " Expected sums found: "
<< size
<< ", Actual sums found: " << sums_found << std::endl;

Delete(matrix, Row);
Delete(actual_indices, sums_found);
return false;
}

if (sums_found == 0 || !actual_indices) {
std::cout << " No summation found for sum: " << sum << std::endl;
Delete(matrix, Row);
Delete(actual_indices, sums_found);
return false;
}

if (sums_found != size) {
std::cout << " Expected sums found: "
<< size << ", Actual sums found: " << sums_found << std::endl;

Delete(matrix, Row);
Delete(actual_indices, sums_found);
return false;
}

if (!IndicesEqual(expected_indices, const_actual, sums_found)) {
Delete(matrix, Row);
Delete(actual_indices, sums_found);
return false;
}

Delete(matrix, Row);
Delete(actual_indices, sums_found);
return true;
}


const int** FixedToDynamic(const int fixed_array[Row][Col]) {
int **dynamic_array = new int*[Row];
for (SIZE_T i = 0; i < Row; ++i) {
dynamic_array[i] = new int[Col];

for (SIZE_T j = 0; j < Col; ++j)
dynamic_array[i][j] = fixed_array[i][j];
}

return const_cast(dynamic_array);
}


void Delete(const int** dynamic_array, SIZE_T size) {
for (SIZE_T i = 0; i < size; ++i)
delete [] dynamic_array[i];
delete [] dynamic_array;
}

void Delete(SIZE_T** dynamic_array, SIZE_T size) {
for (SIZE_T i = 0; i < size; ++i)
delete [] dynamic_array[i];
delete [] dynamic_array;
}


bool IndicesEqual(
const SIZE_T expected[][originalIndices_size], const SIZE_T** actual,
SIZE_T size) {
bool *found = new bool[size];
for (SIZE_T i = 0; i < size; ++i) {
found[i] = false;
for (SIZE_T j = 0; j < size; ++j) {
// NOTE: this method was suggested by Matthew Shelly
if (actual[i][StartRow] == expected[j][StartRow]
&& actual[i][StartCol] == expected[j][StartCol]
&& actual[i][EndRow] == expected[j][EndRow]
&& actual[i][EndCol] == expected[j][EndCol]) {
found[i] = true;
break;
}
}
}
for (SIZE_T i = 0; i < size; ++i)
if (!found[i]) {
std::cout << " " << ToString(expected[i])
<< " NOT FOUND" << std::endl;
delete [] found;
return false; // one of the index pairs was not found
}
delete [] found;
return true;
}


string ToString(const SIZE_T* indices) {
stringstream sout;
sout << "(" << indices[StartRow] << ", " << indices[StartCol] << ") - ("
<< indices[EndRow] << ", " << indices[EndCol] << ")";
return sout.str();
}

Add a comment
Know the answer?
Add Answer to:
Sometimes when I sit in restaurants waiting on food, I request the children’s menu to play...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • When running the program at the destructor  an exception is being thrown. Can someone help me out?...

    When running the program at the destructor  an exception is being thrown. Can someone help me out? vararray.h: #ifndef VARARRAY_H_ #define VARARRAY_H_ class varArray { public:    varArray(); // void constructor    int arraySize() const { return size; } // returns the size of the array    int check(double number); // returns index of element containg "number" or -1 if none    void addNumber(double); // adds number to the array    void removeNumber(double); // deletes the number from the array   ...

  • Who could write the array.cpp file ?   //main.cpp #include "array.hpp" #include <iostream> int main() { int...

    Who could write the array.cpp file ?   //main.cpp #include "array.hpp" #include <iostream> int main() { int n; std::cin >> n; array a(n); for (int i = 0; i < n; i++) { std::cin >> a.data()[i]; } std::cout << "array size:" << a.max_size() << std::endl; std::cout << "array front:" << a.front() << std::endl; std::cout << "array back:" << a.back() << std::endl; int* data = a.data(); std::cout << "array elements using data:" << std::endl; for (int i = 0; i < n;...

  • Hi guys! I need help for the Data Structure class i need to provide implementation of...

    Hi guys! I need help for the Data Structure class i need to provide implementation of the following methods: Destructor Add Subtract Multiply Derive (extra credit ) Evaluate (extra credit ) ------------------------------------------------------- This is Main file cpp file #include "polynomial.h" #include <iostream> #include <sstream> using std::cout; using std::cin; using std::endl; using std::stringstream; int main(int argc, char* argv[]){    stringstream buffer1;    buffer1.str(        "3 -1 2 0 -2.5"    );    Polynomial p(3);    p.Read(buffer1);    cout << p.ToString()...

  • How do I do this? -> string print() const; Function to output the data, return the...

    How do I do this? -> string print() const; Function to output the data, return the output in the form of string, with all elements in the hash table included and each seperated by a space this is my code: template <class elemType> string hashT<elemType>::print() const { for (int i = 0; i < HTSize; i++){        if (indexStatusList[i] == 1){        cout <<HTable[i]<< " " << endl;        }   }    } **********Rest of the code...

  • #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool...

    #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool openFile(ifstream &); void readData(ifstream &, int [], int &); void printData(const int [], int); void sum(const int[], int); void removeItem(int[], int &, int); int main() { ifstream inFile; int list[CAP], size = 0; if (!openFile(inFile)) { cout << "Program terminating!! File not found!" << endl; return -1; } //read the data from the file readData(inFile, list, size); inFile.close(); cout << "Data in file:" <<...

  • Hello, I am trying to write this program and have received a "Segmentation Fault" error but...

    Hello, I am trying to write this program and have received a "Segmentation Fault" error but cannot seem to figure out where. I haven't been able to find it and have been looking for quite a while. The goal is to basically create a version of Conway's Game of Life. I am able to generate a table if the input values for rows and columns are equal, but if they are not I receive a segmentation fault and cannot continue...

  • I need a program in c++ the same as below code but I want it to...

    I need a program in c++ the same as below code but I want it to prompt the user to enter the number of elements after that I want it to ask the user to enter the array elements #include<algorithm> #include<stdio.h> #include<string.h> #include<iostream> using namespace std; int a[50]={2,5,4,3}; bool x[100]; int N=4;//number of elements    int k=10;//target sum int sum;//current target sum int cmp(const void *a,const void *b) { return *(int *)b-*(int *)a; } void backtrace(int n) { if(sum>k) return...

  • This is c++ programming and here is my code. I am getting an exception thrown on...

    This is c++ programming and here is my code. I am getting an exception thrown on the destructor for permanentworker. Can you tell me why I am getting this exception? #include <iostream> #pragma warning(disable:4996) class PermanentWorker { private:    char *name;    int salary; public:    PermanentWorker(const char* nam, int money) : salary(money)    {        name = new char[strlen(nam) + 1];        strcpy(name, nam);    }    int getPay() const    {        return salary;   ...

  • I need help with the code below. It is a C program, NOT C++. It can...

    I need help with the code below. It is a C program, NOT C++. It can only include '.h' libraries. I believe the program is in C++, but it must be a C program. Please help. // // main.c // float_stack_class_c_9_29 // // /* Given the API for a (fixed size), floating point stack class, write the code to create a stack class (in C). */ #include #include #include #include header file to read and print the output on console...

  • I've posted 3 classes after the instruction that were given at start You will implement and...

    I've posted 3 classes after the instruction that were given at start You will implement and test a PriorityQueue class, where the items of the priority queue are stored on a linked list. The material from Ch1 ~ 8 of the textbook can help you tremendously. You can get a lot of good information about implementing this assignment from chapter 8. There are couple notes about this assignment. 1. Using structure Node with a pointer point to Node structure to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT