espace std; // The puzzle will always have exactly 20 columns const int numCols = 20; // Searches the entire puzzle, but may use helper functions to implement logic void searchPuzzle(const char puzzle[][numCols], const string wordBank[], vector &discovered, int numRows, int numWords); // Printer function that outputs a vector void printVector(const vector &v); // Example of one potential helper function. // bool searchPuzzleToTheRight(

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Trying to fix the errors.

C++ given code 

#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

// The puzzle will always have exactly 20 columns
const int numCols = 20;

// Searches the entire puzzle, but may use helper functions to implement logic
void searchPuzzle(const char puzzle[][numCols], const string wordBank[],
vector <string> &discovered, int numRows, int numWords);

// Printer function that outputs a vector
void printVector(const vector <string> &v);

// Example of one potential helper function.
// bool searchPuzzleToTheRight(const char puzzle[][numCols], const string &word,
// int rowStart, int colStart)


int main()
{
int numRows, numWords;

// grab the array row dimension and amount of words
cin >> numRows >> numWords;

// declare a 2D array
char puzzle[numRows][numCols];

// TODO: fill the 2D array via input
// read the puzzle in from the input file using cin

// create a 1D array for wods
string wordBank[numWords];

// TODO: fill the wordBank through input using cin


// set up discovery vector
vector <string> discovered;

// Search for Words
searchPuzzle(puzzle, wordBank, discovered, numRows, numWords);

// Sort the results
sort(discovered.begin(), discovered.end());

// Print vector of discovered words
printVector(discovered);

return 0;
}

// TODO: implement searchPuzzle and any helper functions you want
void searchPuzzle(const char puzzle[][numCols], const string wordBank[],
vector <string> &discovered, int numRows, int numWords)
{

}

// TODO: implement printVector
void printVector(const vector <string> &v)
{

}

 

My code

#include <bits/stdc++.h>
using namespace std;

// vector stores 1 corresponding to index
// number which is already assigned
// to any char, otherwise stores 0
vector<int> use(10);

// structure to store char and its corresponding integer
struct node
{
char c;
int v;
};

// function check for correct solution
int check(node* nodeArr, const int count, string s1,
string s2, string s3)
{
int val1 = 0, val2 = 0, val3 = 0, m = 1, j, i;

// calculate number corresponding to first string
for (i = s1.length() - 1; i >= 0; i--)
{
char ch = s1[i];
for (j = 0; j < count; j++)
if (nodeArr[j].c == ch)
break;

val1 += m * nodeArr[j].v;
m *= 10;
}
m = 1;

// calculate number corresponding to second string
for (i = s2.length() - 1; i >= 0; i--)
{
char ch = s2[i];
for (j = 0; j < count; j++)
if (nodeArr[j].c == ch)
break;

val2 += m * nodeArr[j].v;
m *= 10;
}
m = 1;

// calculate number corresponding to third string
for (i = s3.length() - 1; i >= 0; i--)
{
char ch = s3[i];
for (j = 0; j < count; j++)
if (nodeArr[j].c == ch)
break;

val3 += m * nodeArr[j].v;
m *= 10;
}

// sum of first two number equal to third return true
if (val3 == (val1 + val2))
return 1;

// else return false
return 0;
}

// Recursive function to check solution for all permutations
bool permutation(const int count, node* nodeArr, int n,
string s1, string s2, string s3)
{
// Base case
if (n == count - 1)
{

// check for all numbers not used yet
for (int i = 0; i < 10; i++)
{

// if not used
if (use[i] == 0)
{

// assign char at index n integer i
nodeArr[n].v = i;

// if solution found
if (check(nodeArr, count, s1, s2, s3) == 1)
{
cout << "\nSolution found: ";
for (int j = 0; j < count; j++)
cout << " " << nodeArr[j].c << " = "
<< nodeArr[j].v;
return true;
}
}
}
return false;
}

for (int i = 0; i < 10; i++)
{

// if ith integer not used yet
if (use[i] == 0)
{

// assign char at index n integer i
nodeArr[n].v = i;

// mark it as not available for other char
use[i] = 1;

// call recursive function
if (permutation(count, nodeArr, n + 1, s1, s2, s3))
return true;

// backtrack for all other possible solutions
use[i] = 0;
}
}
return false;
}

bool solveCryptographic(string s1, string s2,
string s3)
{
// count to store number of unique char
int count = 0;

// Length of all three strings
int l1 = s1.length();
int l2 = s2.length();
int l3 = s3.length();

// vector to store frequency of each char
vector<int> freq(26);

for (int i = 0; i < l1; i++)
++freq[s1[i] - 'A'];

for (int i = 0; i < l2; i++)
++freq[s2[i] - 'A'];

for (int i = 0; i < l3; i++)
++freq[s3[i] - 'A'];

// count number of unique char
for (int i = 0; i < 26; i++)
if (freq[i] > 0)
count++;

// solution not possible for count greater than 10
if (count > 10)
{
cout << "Invalid strings";
return 0;
}

// array of nodes
node nodeArr[count];

// store all unique char in nodeArr
for (int i = 0, j = 0; i < 26; i++)
{
if (freq[i] > 0)
{
nodeArr[j].c = char(i + 'A');
j++;
}
}
return permutation(count, nodeArr, 0, s1, s2, s3);
}

// Driver function
int main()
{
string s1 = "SEND";
string s2 = "MORE";
string s3 = "MONEY";

if (solveCryptographic(s1, s2, s3) == false)
cout << "No solution";
return 0;
}

 

 

Output differs. See highlights below.
3
HELLOJKLIYQSRPZIMKOP
WRLDQJKL IYQSRP ZI M KOP
BY Z ANTINE QWERTYUIOPZ
Input
НELLO
WORLD
Your output
Solution Eound:
Expected output
HELLO
2: Compare output a
Output differs. See highlights below.
25
RKTATO O INE DO X L
LPOA
JGGIJUV REBASIHGILJYN
FPAXBEL LDNYUE ZBIESNH
LEIAHK S USSAMCVBH NIWS
UIZULFFKDOTE JOZFMD I Z
BM GLG ZEE HRN INCNX X OTR
ISNIXT KSZTOERE HURDI I
M V P N B W AHLAKIWK X MR CF J
P ZHKLZ JIGTPNDPTPVX Y H
UN W WJY JRLWUIORRYOX P P
GAAILIB ADVSYOIUQHODF
MODW ZZQDKN Q ONA N F PTHR
DVZGYW ZYQP PCHGV MTJPP
LQH VEZ VIGEEMOOF SLGY L
CZLUPSAMRS G VX V MILTFR
U AKBOZRTSICZIPDTAX KA
U AWAVV X IEIDLCYOOBCNN
SYIWX C N D V N
GJEDIIEQNSSNG HO IEWIK
GFEGHRJEGLLPQO YQODEI
A V VNN PD TWRDVCMAGSJAN
Input
H H C DR JAYA
ADNIQY Y GOWYEDY ZAOOPV
RJOKNJ M Z LVIUVW HFOF D V
vCT ZHUOBINANZ H IPYBZ M
LM X YNITG CQELVUQB PLDL
АMIDALA
ANAKIN
JEDI
LEIA
LUKE
ОBINAN
ТАТОOINE
TWILEK
Your output
Solution found:
ANAKIN
JEDI
LEIA
Expected output
LUKE
ОBINAN
TATOOINE
O
Transcribed Image Text:Output differs. See highlights below. 3 HELLOJKLIYQSRPZIMKOP WRLDQJKL IYQSRP ZI M KOP BY Z ANTINE QWERTYUIOPZ Input НELLO WORLD Your output Solution Eound: Expected output HELLO 2: Compare output a Output differs. See highlights below. 25 RKTATO O INE DO X L LPOA JGGIJUV REBASIHGILJYN FPAXBEL LDNYUE ZBIESNH LEIAHK S USSAMCVBH NIWS UIZULFFKDOTE JOZFMD I Z BM GLG ZEE HRN INCNX X OTR ISNIXT KSZTOERE HURDI I M V P N B W AHLAKIWK X MR CF J P ZHKLZ JIGTPNDPTPVX Y H UN W WJY JRLWUIORRYOX P P GAAILIB ADVSYOIUQHODF MODW ZZQDKN Q ONA N F PTHR DVZGYW ZYQP PCHGV MTJPP LQH VEZ VIGEEMOOF SLGY L CZLUPSAMRS G VX V MILTFR U AKBOZRTSICZIPDTAX KA U AWAVV X IEIDLCYOOBCNN SYIWX C N D V N GJEDIIEQNSSNG HO IEWIK GFEGHRJEGLLPQO YQODEI A V VNN PD TWRDVCMAGSJAN Input H H C DR JAYA ADNIQY Y GOWYEDY ZAOOPV RJOKNJ M Z LVIUVW HFOF D V vCT ZHUOBINANZ H IPYBZ M LM X YNITG CQELVUQB PLDL АMIDALA ANAKIN JEDI LEIA LUKE ОBINAN ТАТОOINE TWILEK Your output Solution found: ANAKIN JEDI LEIA Expected output LUKE ОBINAN TATOOINE O
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Declaring and Defining the Function
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education