Question

In: Computer Science

C Code! I need all of the \*TODO*\ sections of this code completed. For this dictionary.c...

C Code! I need all of the \*TODO*\ sections of this code completed.

For this dictionary.c program, you should end up with a simple English-French and French-English dictionary with a couple of about 350 words(I've provided 5 of each don't worry about this part). I just need a way to look up a word in a sorted array. You simply need to find a way to identify at which index i a certain word appears in an array of words and print out the corresponding French word, at the same index i.

#include <stdio.h>
#include <string.h>

const int NUMBER_ENTRIES = 352;

const char* english_NUMBER_ENTRIES[5]= {"war","size", "her","song","time"};

const char* french_NUMBER_ENTRIES[5] = {"guerre","taille","sa", "chanson","temps"};


int main(int argc, char **argv) 
{
  const int BUFFER_LENGTH = 128;
  char c, t;
  char user_word[BUFFER_LENGTH];
  enum { ENGLISH_FRENCH, FRENCH_ENGLISH } direction;
  int i;
  /* TODO: Add additional declarations here */
           printf("Dictionary\n\n");
  printf("English-French Dictionary\n\n");
  do {
    printf("Enter e for English-French\n");
    printf("Enter f for French-English\n");
    scanf("%c", &c);
    scanf("%c", &t);
  } while (!((c == 'e') || (c == 'f')));

  switch (c) {
  case 'e':
    direction = ENGLISH_FRENCH;
    break;
  case 'f':
    direction = FRENCH_ENGLISH;
    break;
  }

  printf("Please enter a word in ");
  switch (direction) {
  case ENGLISH_FRENCH:
    printf("English");
    break;
  case FRENCH_ENGLISH:
    printf("French");
    break;
  }
  printf(": ");
  i = 0;
  scanf("%c", &c);
  while (c != '\n') {
    user_word[i] = c;
    i++;
    if (i >= BUFFER_LENGTH) {
      i = BUFFER_LENGTH-1;
    }
    scanf("%c", &c);
  }
  user_word[i] = '\0';

  switch (direction) {
  case ENGLISH_FRENCH:
   printf(" The corresponding word is: ");
    /* TODO */
    break;
  case FRENCH_ENGLISH:
  printf(" The corresponding word is: ");
    /* TODO */
    break;
  }

  return 0;
}

Solutions

Expert Solution

Code :

#include <stdio.h>

#include <string.h>

const int NUMBER_ENTRIES = 352;

const char* english_NUMBER_ENTRIES[5]= {"war","size", "her","song","time"};

const char* french_NUMBER_ENTRIES[5] = {"guerre","taille","sa", "chanson","temps"};


int main(int argc, char **argv)

{

  const int BUFFER_LENGTH = 128;

  char c, t;

  char user_word[BUFFER_LENGTH];

  enum { ENGLISH_FRENCH, FRENCH_ENGLISH } direction;

  int i;

  /* TODO: Add additional declarations here */

           printf("Dictionary\n\n");

  printf("English-French Dictionary\n\n");

  do {

    printf("Enter e for English-French\n");

    printf("Enter f for French-English\n");

    scanf("%c", &c);

    scanf("%c", &t);

  } while (!((c == 'e') || (c == 'f')));

  switch (c) {

  case 'e':

    direction = ENGLISH_FRENCH;

    break;

  case 'f':

    direction = FRENCH_ENGLISH;

    break;

  }

  printf("Please enter a word in ");

  switch (direction) {

  case ENGLISH_FRENCH:

    printf("English");

    break;

  case FRENCH_ENGLISH:

    printf("French");

    break;

  }

  printf(": ");

  i = 0;

  scanf("%c", &c);

  while (c != '\n') {

    user_word[i] = c;

    i++;

    if (i >= BUFFER_LENGTH) {

      i = BUFFER_LENGTH-1;

    }

    scanf("%c", &c);

  }

  user_word[i] = '\0';

  int size = sizeof(english_NUMBER_ENTRIES)/sizeof(english_NUMBER_ENTRIES[0]);

  int index;

  switch (direction) {

  case ENGLISH_FRENCH:

   for(int i = 0; i<size; i++)

   {

       if(strcmp(english_NUMBER_ENTRIES[i], user_word) == 0) // strcmp is 0 if values are same

       {

           index = i; // get the index

       }

   }

    printf(" The corresponding word is: ");

    printf("%s\n", french_NUMBER_ENTRIES[index]);

    

    break;

  case FRENCH_ENGLISH:

    for(int i = 0; i<size; i++)

    {

        if(strcmp(french_NUMBER_ENTRIES[i], user_word) == 0) // strcmp is 0 if values are same

        {

            index = i; // get the index

        }

    }

    printf(" The corresponding word is: ");

    printf("%s\n",english_NUMBER_ENTRIES[index]);

    break;

  }

  return 0;

}

Variation in your code that i have done is :

OUTPUT :


Related Solutions

this is a python code that i need to covert to C++ code...is this possible? if...
this is a python code that i need to covert to C++ code...is this possible? if so, can you please convert this pythin code to C++? def main(): endProgram = 'no' print while endProgram == 'no': print # declare variables notGreenCost = [0] * 12 goneGreenCost = [0] * 12 savings = [0] * 12 months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] getNotGreen(notGreenCost, months) getGoneGreen(goneGreenCost, months) energySaved(notGreenCost, goneGreenCost, savings) displayInfo(notGreenCost, goneGreenCost, savings, months)...
Having Trouble with this C++ assignment THe places that are marked // TODO is where code...
Having Trouble with this C++ assignment THe places that are marked // TODO is where code should be filled in at Header (myQueue.h) #ifndef _MYQUEUE_H_ #define _MYQUEUE_H_ using namespace std; template class myQueue { public:    myQueue(int maxSz);    ~myQueue();    void enqueue(T item);    T dequeue(); int currentSize(); bool isEmpty(); bool isFull(); private:    T *contents; /*Dynamic initiate (C++ keyword new) the holder array*/    int front,rear; /*Index in the array of the front and rear element*/    int...
c# code working but output not right, I need to output all numbers like : Prime...
c# code working but output not right, I need to output all numbers like : Prime factors of 4 are: 2 x 2 here is just 2 Prime factors of 7 are: 7 Prime factors of 30 are: 2 x 3 x 5 Prime factors of 40 are: 2 x 2 x 2 x 5 here is just 2,5 Prime factors of 50 are: 2 x 5 x 5 here is just 2,5 1) How I can fix it 2)I...
Complete the // TODO sections in the EasyRental class. Create a method that implements an iterative...
Complete the // TODO sections in the EasyRental class. Create a method that implements an iterative sort that sorts the vehicle rentals by color in ascending order (smallest to largest) Create a method to binary search for a vehicle based on a color, that should return the index where the vehicle was found or -1 You are comparing Strings in an object not integers. Ex. If the input is: brown red white blue black -1 the output is: Enter the...
This problem needs to be solved with source code. I need a C++ program that will...
This problem needs to be solved with source code. I need a C++ program that will help me solve this question. I need it in C++, please. Writing with comments so it maybe cleared. 1.2. We received the following ciphertext which was encoded with a shift cipher: xultpaajcxitltlxaarpjhtiwtgxktghidhipxciwtvgtpilpit ghlxiwiwtxgqadds. 1. Perform an attack against the cipher based on a letter frequency count: How many letters do you have to identify through a frequency count to recover the key? What is...
I need to complete this C++ program. The instructions are in the comments inside the code...
I need to complete this C++ program. The instructions are in the comments inside the code below: ------------------------------------------------------------------------- Original string is: this is a secret! Encypted string is: uijt!jt!b!tfdsfu" Decrypted string is: this is a secret! //Encoding program //Pre-_____? //other necessary stuff here int main() { //create a string to encrypt using a char array cout<< "Original string is: "<<string<<endl; encrypt(string); cout<< "Encrypted string is: "<<string<<endl; decrypt(string); cout<<"Decrypted string is: "<<string<<endl; return 0; } void encrypt(char e[]) { //Write implementation...
c++ I need a code that will fill an array size of 1000, an array of...
c++ I need a code that will fill an array size of 1000, an array of size 2000, and an array size of 10000, with random int values. Basically like this: array1[1000] = filled all with random numbers array2[2000] = filled all with random numbers array3[10000] = filled all with random numbers C++ no need for print
C++ Hello .I need to convert this code into template and then test the template with...
C++ Hello .I need to convert this code into template and then test the template with dynamic array of strings also if you can help me move the function out of the class that would be great.also There is a bug where the memory was being freed without using new operator. I cant seem to find it thanks in advance #include using namespace std; class DynamicStringArray {    private:        string *dynamicArray;        int size;    public:   ...
Need the correct line of code for c program to solve  this equation . i is a...
Need the correct line of code for c program to solve  this equation . i is a value that the program all ready calculates from user imput. t i = (x*1000)+(y*500)+(z*250
Note: I need a code and other requirement Note: programming language is c++ If you need...
Note: I need a code and other requirement Note: programming language is c++ If you need more information, please clarify what information you want. consider solving the equation sin(x) - e^(-x) = 0 write a computer program to solve the given equation using: 1- bisection method 2- fixed-point method 3- newton's intervals: {0,1},{1,2},{2,3},{3,4},{4,5},{5,6},{6,7},{7,8},{8,9},{9,10} choose accuracy E = 10^(-5) Make sure you document your program Requirement : 1- Mathematical justification 2- algorithem description 3- code (program) with documentation 4-output: roots ,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT