Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ program that, given a seven-digit number, writes to a file every pos

ID: 3665791 • Letter: W

Question

Write a C++ program that, given a seven-digit number, writes to a file every possible seven-letter word corresponding to that number. There are 2187 (3 to the seventh power) such words.

Include the numbers 0 and 1; just embed them in the words as a 0 and a 1. Example: one possibility for 5701679 would be: LR01OPW. Assume the user correctly enters only 7 digits (no -). Read the user input into a char array. Use a recursive function to build the combinations. Leave the output in a file and inform the user of the file name before exiting.

You will need some constants:

const int ROWSIZE = 10;

const int COLSIZE = 5;

const int NUMSIZE = 8;

You will need an array to read in the users 7-digit number, a 2-dimensional character array, and an array in which to build the number combinations:

char array[NUMSIZE]; // user input do not go beyond 7 chars and a null byte!

// 2-d array of letters

char letters[ROWSIZE][COLSIZE] = { { '0', '', '', '', '' },

{ '1', '', '', '', '' },

{ 'A', 'B', 'C', '', '' },

{ 'D', 'E', 'F', '', '' },

{ 'G', 'H', 'I', '', '' },

{ 'J', 'K', 'L', '', '' },

{ 'M', 'N', 'O', '', '' },

{ 'P', 'Q', 'R', 'S', '' },

{ 'T', 'U', 'V', '', '' },

{ 'W', 'X', 'Y', 'Z', '' } };

char buildArr[ROWSIZE]; // output

Your recursive routine should have five parameters:

void combine (char *, // user input array

                             char [ROWSIZE][COLSIZE], // 2-d array of letters

                             char *, // array in which to build the output

                             int, // index into output array

                             ofstream&); // reference to the open output file

Please add comments to each line of code!

edit: If it helps, the question is based off of "C++ How to program (Early Objects Version) 9th Edition) Question 14.12.  

Explanation / Answer

#inlcude <iostream>

#include <cctype>

using namespace std;

int main() {
  
   fstream file;
   const int ROWSIZE = 10;
const int COLSIZE = 5;
const int NUMSIZE = 8;

   char inputNumber[7];
   cout << "Enter a 7 digit number: " ;
  
   // open a file in write mode.
   ofstream outfile;
outfile.open("afile.dat");

cout << "Writing to the file" << endl;
cin.getline(inputNumber, 100);
// write inputted data into the file.
outfile << inputNumber << endl;
// close the opened file.
outfile.close();
// open a file in read mode.
ifstream infile;
infile.open("afile.dat");

cout << "Reading from the file" << endl;
infile >> inputNumber;
// close the opened file.
infile.close();


   char array[NUMSIZE]; // user input
//I will create a two-dimensional array with the dimensions of 10 * 5 and waste space
char letters[ROWSIZE][COLSIZE] = {{'0', '', '','',''},
                               {'1', '', '','',''},
                               {'A', 'B', 'C','',''},
                               {'D', 'E', 'F','',''},
                               {'G', 'H', 'I','',''},
                               {'J', 'K', 'L','',''},
                               {'M', 'N', 'O','',''},
                               {'P', 'Q', 'R','S',''},
                               {'T', 'U', 'V','',''},
                               {'W', 'X', 'Y','Z',''}};

char buildArr[ROWSIZE]; // output
  
   //void combine (char*,char[ROWSIZE][COLSIZE],char*,int, outfile);
   void print(int number[],char output[],int start,int end)
   {  
       static int next;
       int len,i;

       if(start==end)
       {
           printf("%s ",output);
           return ;
       }
       len = strlen(digits[number[start]]);

       if(number[start]==0 || number[start]==1)
       {

           print(number,output,start+1,end);
           return ;
       }
      
       for(i=0;i<len;i++)
           { output[next]=digits[number[start]][i];
           next++;
       print(number,output,start+1,end);
           next--;
           return;
           }
   }

   void printallwords(int number[],int n, int i, char output[n+1])
           {
               for(i=0;i&lt;n;i++)
               {
               if(number[i]="=0" ||="" number[i]="=1";
               n01 = output[n-n01]="" ;
               print(number,output,0,n);
           }
   }
  

   number[]="{0,1,3,9};
   int n=sizeof(number)/sizeof(number[0]);
   printallwords(number,n);
   return 0;
}

Sharing pseudo code too for ref:

Some psuedocode:

list recursiveFind(list input, node curNode)
if curNode has no children
// we found a word!
return list containing ("") (so the caller knows we found a result instead of failing)
else
results = empty list
foreach letter in list
if find a node where value == letter
recursive_results = recursiveFind(input except for that letter, that node you just found)
foreach result in recursive_results
push (letter + result) into results
return results

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote