Question

Scala: Write a Program as elegantly as possible Given a piece of text, create a histogram...

Scala: Write a Program as elegantly as possible

Given a piece of text, create a histogram of letter pairs (order from high to low). For instance, for the text, “this is a good thing”,

the letter pairs are: th, hi, is, is, go, oo, od, th, hi, in, and ng. (ignore a) The histogram will be:
th: 2, is: 2, hi: 2 go: 1, oo: 1, od: 1, in: 1, ng: 1

Sample Input/Output:

Enter text: this is a good thing
Histogram: th: 2, is: 2, hi: 2 go: 1, oo: 1, od: 1, in: 1, ng: 1
Enter text: coooooool
Histogram: oo: 6, co: 1, ol: 1
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
   char sentence[10],letter_pair[9][2];
   //Enter the string
       printf("Enter a string\n");
       scanf("%s", sentence);
       printf("The entered string is: %s\n", sentence);
   //
   //generate histrogram
   int freq=0;
   int hist[strlen(sentence)-1];
   for(int i=0;i<strlen(sentence);i++)
   {
       for(int j=0;j<strlen(sentence)-1;j++)
       {
           for(int k=0;k<2;k++)
           {
               if (sentence[i] != letter_pair[j][k])
               {
                   letter_pair[j][k]=sentence[i++];
               }
               else
               {      
                   hist[j]=++freq;
               }
           }
           hist[j]=++freq;
           i=i-2;
       }
      
   }
   printf("The histogram of entered string is:\n");
   for (int x=0;x<strlen(sentence)-1;x++)
   {
       for(int y=0;y<2;y++)
       {
           printf("%s :", letter_pair[x][y]);
       }
       printf(" : %d\n", hist[x]);
   }
}

Add a comment
Know the answer?
Add Answer to:
Scala: Write a Program as elegantly as possible Given a piece of text, create a histogram...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Scala: Write and test a Program as elegantly as possible Given a piece of text, create...

    Scala: Write and test a Program as elegantly as possible Given a piece of text, create a histogram of letter pairs (order from high to low). For instance, for the text, “this is a good thing”, the letter pairs are: th, hi, is, is, go, oo, od, th, hi, in, and ng. (ignore a) The histogram will be: th: 2, is: 2, hi: 2 go: 1, oo: 1, od: 1, in: 1, ng: 1 Sample Input/Output: Enter text: this is...

  • Write a python program that will create a histogram of the number of times each character occurs ...

    Write a python program that will create a histogram of the number of times each character occurs in a file. Check https://en.wikipedia.org/wiki/Histogram and http://interactivepython.org/runestone/static/thinkcspy/Functions/ATurtleBarChart.html for help. Your program will read any input text file and print the histogram. If you have a huge file you should have a graph similar to: Note the y-axis shows the percentage of each letter. The height of each bar is percentage of that letter. Program MUST show the graph with percentage marks on y-axis...

  • Write a program that displays a histogram to show the number occurrences of each number in...

    Write a program that displays a histogram to show the number occurrences of each number in an input sequence. There should be a text filed to accept users input numbers and button to show the histogram. Enter your student number to generate your unique histogram. A student with 100013456 will give a histogram like the following one (note the text filed and button are not shown in the following histogram, BUT is required for the application) java code is needed...

  • JavaFX Write a program that displays a histogram to show the number occurrences of each number...

    JavaFX Write a program that displays a histogram to show the number occurrences of each number in an input sequence. There should be a text filed to accept users input numbers and button to show the histogram. Enter your student number (9 digit number) to generate your unique histogram. A student with ID 100013456 will give a histogram like the following one (note the text filed and button are not shown in the following histogram, BUT is required for the...

  • Create a program in C++ that has only an integer type input called in. This program...

    Create a program in C++ that has only an integer type input called in. This program interchanges two characters’ positions based on user’s input value. The specifications are: If user enters 0, first and second characters are swapped. If user enters 1, first and last characters are swapped. If users enter 2, second and last characters are swapped. For example: The original text is “ABC”. First letter is A, second letter is B and last letter is C. If 0...

  • 6.15 Program 6: Using Arrays to Count Letters in Text 1. Introduction In this program, you...

    6.15 Program 6: Using Arrays to Count Letters in Text 1. Introduction In this program, you will practice working with arrays. Your program will read lines of text from the keyboard and use an array to track the number of times each letter occurs in the input text. You will use the contents of this array to generate a histogram (bar graph) indicating the relative frequencies of each letter entered. Test cases are available in this document. Remember, in addition...

  • Must be done in python and using linux mint Write a program to create a text...

    Must be done in python and using linux mint Write a program to create a text file which contains a sequence of test scores. Each score will be between 0 and 100 inclusive. There will be one value per line, with no additional text in the file. Stop adding information to the file when the user enters -1 The program will first ask for a file name and then the scores. Use a sentinel of -1 to indicate the user...

  • Write a C++ program that reads in a text file and writes the histogram of character counts sorted in alphabetical order to an output file. Eg, if the input file is “to be or not to be”, then the output should be: b **2 e **2 n *1 o ****4

    Write a C++ program that reads in a text file andwrites the histogram of character counts sorted inalphabetical order to an output file. Eg, if theinput file is “to be or not to be”, then the outputshould be:b   **2e   **2n   *1o   ****4r    *1t    ***3

  • Using basic c++ write 2 separate codes for this assignment. Program #1 Write a program that...

    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. •...

  • 14.3: More Sentences Write a program that allows a user to enter a sentence and then...

    14.3: More Sentences Write a program that allows a user to enter a sentence and then the position of two characters in the sentence. The program should then report whether the two characters are identical or different. When the two characters are identical, the program should display the message: <char> and <char> are identical! Note that <char> should be replaced with the characters from the String. See example output below for more information. When the two characters are different, the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT