Write a program in C++ that, given a seven-digit number, writes to a file every possible seven-letter 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 user’s 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', '\0', '\0', '\0', '\0' },
{ '1', '\0', '\0', '\0', '\0' },
{ 'A', 'B', 'C', '\0', '\0' },
{ 'D', 'E', 'F', '\0', '\0' },
{ 'G', 'H', 'I', '\0', '\0' },
{ 'J', 'K', 'L', '\0', '\0' },
{ 'M', 'N', 'O', '\0', '\0' },
{ 'P', 'Q', 'R', 'S', '\0' },
{ 'T', 'U', 'V', '\0', '\0' },
{ 'W', 'X', 'Y', 'Z', '\0' } };
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 include comments
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
const int ROWSIZE = 10;
const int COLSIZE = 5;
const int NUMSIZE = 8;
void combine(char *array, char letters[ROWSIZE][COLSIZE], char *build_array, int n, ofstream& output)
{
/********************************************************************
char* is for user input
char is for 2D array of letters
char* is for an array in which to build the output
int is for index into output array
*********************************************************************/
if (n == NUMSIZE - 1) {
output << build_array << endl;
return;
}
else {
int c = 0; // counter to move through columns in 2-D array
int b = array[n] - '0'; // counter to move through rows in 2-D array
while (letters[b][c] != '\0') {
build_array[n] = letters[b][c++];
combine(array, letters, build_array, n + 1, output);
}
}
}
int main()
{
char array[NUMSIZE]; // array
char buildArr[ROWSIZE] = { 0 }; // array
int z = 0; // counter to function
ofstream os; // output file
cout << "Enter a seven digit number: ";
cin >> array;
cout << endl;
os.open("combination.txt");
if (!os.is_open())
cout << "ERROR 0504: Unable to execute file. ";
char letter[ROWSIZE][COLSIZE] = {{ '0', '\0', '\0', '\0', '\0' },
{ '1', '\0', '\0', '\0', '\0' },
{ 'A', 'B', 'C', '\0', '\0' },
{ 'D', 'E', 'F', '\0', '\0' },
{ 'G', 'H', 'I', '\0', '\0' },
{ 'J', 'K', 'L', '\0', '\0' },
{ 'M', 'N', 'O', '\0', '\0' },
{ 'P', 'Q', 'R', 'S', '\0' },
{ 'T', 'U', 'V', '\0', '\0' },
{ 'W', 'X', 'Y', 'Z', '\0' }};
combine(array, letter, buildArr, z, os);
cout << "\nFilename: combination.txt" << endl;
os.close();
return 0;
}
Write a program in C++ that, given a seven-digit number, writes to a file every possible...
MUST BE PROCEDURAL CODE, DO NOT USE GLOBAL VARIABLES. Write a program in C++that generates random words from a training set as explained in the lectures on graphs. Do not hard-code the training set! Read it from a file. A suggested format for the input file: 6 a e m r s t 10 ate eat mate meet rate seat stream tame team tear Here are some suggestions for constants, array declarations, and helper functions #include <iostream> #include <fstream> #include...
Write a program that writes a series of random numbers to a file. Each random number should be in the range of 1 through 500 inclusive. 1.Use a file called randoms.txt as a input file and output file a. If you go to open the file and its not there then ask the user to create the file and then quit 2. Ask the user to specify how many random numbers the file will hold. a.Make sure that the...
IN C++ Write a program in c++ that will read from a file the following sentence: The quick brown fox jumps over the lazy dog Each word must be read into one location in an array beginning with the first element. You must declare an array as follows: char *words [9] ; // this is an array of c- strings. HINT words[0] will contain "the" words[1] will contain "quick" write a function int length (const char *a) to determine the...
Write a program **(IN C)** that displays all the phone numbers in a file that match the area code that the user is searching for. The program prompts the user to enter the phone number and the name of a file. The program writes the matching phone numbers to the output file. For example, Enter the file name: phone_numbers.txt Enter the area code: 813 Output: encoded words are written to file: 813_phone_numbers.txt The program reads the content of the file...
In c programming
.
Part A: Writing into a Sequential File Write a C program called "Lab5A.c" to prompt the user and store 5 student records into a file called "stdInfo.txt". This "stdInfo.txt" file will also be used in the second part of this laboratory exercise The format of the file would look like this sample (excluding the first line) ID FIRSTNAME LASTNAME GPA YEAR 10 jack drell 64.5 2018 20 mina alam 92.3 2016 40 abed alie 54.0 2017...
Write a C program convert.c that converts each number in an array by the sum of that number plus 6 modulus 10. A sample input/output: Enter the length of the array: 5 Enter the elements of the array: 3 928 4 14 77 Output: 9 4 0 0 3 The program should include the following function: void convert(int *a1, int n, int *a2) The function converts every element in array a1 of length n to an output array a2. The...
Using basic c++ write 2 separate codes for this assignment. Program #1 Write a program that calculates the average of a group of test scores, where the lowest score in the group is dropped. It should use the following functions. • void getScore() should ask the user for a test score, store it in the reference parameter variable, and validate it. This function should be called by the main once for each of the five scores to be entered. •...
Write a program in C that takes a file name as the only argument on the command line, and prints out the number of 0-bits and 1-bits in the file int main ( int argc , char * argv [] ) { // Check if the user gave an argument , otherwise print " ERROR : no argument " // Check if the file can be read , otherwise print " ERROR : can ’t read " // Otherwise ,...
You are to write three functions for this lab: mean, remove, display. Download the main.cpp file provided to get started. Read the comments in the main function to determine exactly what the main function should do. //include any standard libraries needed // - Passes in an array along with the size of the array. // - Returns the mean of all values stored in the array. double mean(const double array[], int arraySize); // - Passes in an array, the size...
Using the diagram below and the starter code, write Document Processor that will read a file and let the user determine how many words of a certain length there are and get a count of all of the word lengths in the file. Your program should do the following: The constructor should accept the filename to read (word.txt) and then fill the words ArrayList up with the words from the file. The countWords method should accept an integer that represents...