1. Write a program that allows you to read in an English-French dictionary into
ID: 3550716 • Letter: 1
Question
1. Write a program that allows you to read in an English-French dictionary into two arrays - one for English words and one for French words. The dictionary data can be stored in a file, call it dict.dat. Move the English words until they are sorted alphabetically and also move the associated French words to corresponding locations in the parallel array. Once sorted, your program should allow for a user to look up an English word and then print its French equivalent. Your dictionary should be able to store a maximum of 50 English and French words.
You should have 4 functions: read_in, sort_words, search_words, and write_out.
The prototypes are:
int read_in(char[][WORD_LENGTH], char[][WORD_LENGTH]);
void sort_words(char[][WORD_LENGTH], char[][WORD_LENGTH], int);
void search_words(char[][WORD_LENGTH], char[][WORD_LENGTH], int);
void write_out(char[][WORD_LENGTH], char[][WORD_LENGTH], int);
WORD_LENGTH could be 30.
An example of dict.dat could be:
house maison
mother mere
father pere
sister soeur
brother frere
library bibliotheque
door porte
hello bonjour
good-bye au revoir
cat chat
dog chien
book livre
Parallel arrays are used to maintain a relationship between data items stored in multiple arrays. This relationship is established by using the same index value when accessing each array. C++ dose not create or maintain the relationship, it is the programmer
Explanation / Answer
i have done this assignment in my college days file save please and run
please rate dear for the efforts
/* example for contents of data.txt
easy
facile
dog
chien
horse
coeval
bet
parier
lie
mensonge
sea
mer
best
le meillur
ghost
esprit
high
de haut
fair
juste
move
mettre
*/
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
static const int CAPACITY = 50;
int read_in(string e[], string f[]); //Initial read to the arrays and returns number of elements in use
void sort_words(string e[], string f[], const int used); //Sorts the elements (strings) in e lexicographically and performs the same operations on f
void search_words(const string e[], const string f[], const int used); //searches e for a string and outputs the corresponding element of f if found
void write_out(const string e[], const string f[], const int used); //prints both arrays to screen
void newLine(); //Used to remove the negative aspect of using the extraction operator on the cin stream
//i.e. it removes leftover characters in the input buffer up to the next ' '
int main()
{
string englishWords[CAPACITY]; //initialize an array for English words
string frenchWords[CAPACITY]; //initialize an array for French words
int numUsed = read_in(englishWords, frenchWords); //max numUsed is 50
sort_words(englishWords, frenchWords, numUsed); //sorts elements 0 - numUsed lexicographically
bool again = true; //do-while ends when false
do
{
int choice; //choice for Main Menu
cout << setw(36) << right << "+---------------------------------+" << endl
<< setw(36) << right << "| Main Menu |" << endl
<< setw(36) << right << "+---------------------------------+" << endl << endl
<< "0. Quit" << endl
<< "1. Search for an English word" << endl
<< "2. Display the entire collection of words" << endl;
//<< "3. Print collection to file" << endl; //if you need to print to file instead of cout, add another function with the appropriate definition
cin >> choice; //take users choice
newLine(); //remove all chars up to the next ' ' (only needed if using cin)
switch(choice)
{
case 0:
cout << "Bye" << endl;
again = false;
break;
case 1:
search_words(englishWords, frenchWords, numUsed);
break;
case 2:
write_out(englishWords, frenchWords, numUsed);
break;
default:
cout << "Enter 0, 1, or 2" << endl;
break;
}
} while(again);
return(0);
}
int read_in(string e[], string f[])
{
int count = 0; //Zero elements in use for englishWords[] and frenchWords[]
ifstream infile("data.txt");
if(!infile)
{
cout << "Error opening input file" << endl;
return -1;
}
while(!infile.eof())
{
if(count == CAPACITY)
{
infile.close(); //does not read more than 50 entries for each array
return count; //you need this because you limited the arrays to 50 words each
}
getline(infile, e[count]); //better than using the extraction operator because some (French)
getline(infile, f[count]); //words contain spaces e.g. "de haut"
count++;
}
infile.close();
return count; //returns elements in use so the other functions can ignore empty elements
}
void sort_words(string e[], string f[], const int used)
{
for(int i = 0; i < used; i++)
{
for(int j = (i + 1); j < used; j++)
{
if(e[j] < e[i])
{
string temp = e[j];
e[j] = e[i];
e[i] = temp;
temp = f[j];
f[j] = f[i];
f[i] = temp;
}
}
}
}
void search_words(const string e[], const string f[], const int used)
{
bool found = false;
string target;
cout << "Enter an English word and the French equivalent will be displayed: ";
getline(cin, target);
for(int i = 0; i < used; i++)
if(e[i] == target)
{
cout << " The French word for " << target << " is " << f[i] << endl << endl;
found = true;
}
if(found == false)
cout << endl << target << " not found" << endl << endl;
}
void write_out(const string e[], const string f[], const int used)
{
cout << setw(17) << right << "English" << left << " | " << "French" << endl
<< setw(17) << right << "--------------" << left << "-+-" << "--------------" << endl;
for(int i = 0; i < used; i++)
cout << setw(17) << right << e[i] << left << " | " << f[i] << endl;
}
void newLine()
{
char symbol;
do
{
cin.get(symbol);
}while(symbol != ' ');
}
');
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.