Question

In: Computer Science

Create a text file with 1000 random H's and T's, then write a program to count how many times each of the pairs HH, HT, TH, TT occur.

Programing in C

Create a text file with 1000 random H's and T's, then write a program to count how many times each of the pairs HH, HT, TH, TT occur. Each should occur equally often. (use getchar(), read until EOF.) Regard the file as a sequence of pairs (so pairs do not overlap.) If there are an odd number of characters, ignore the last one.

Example output:

HH occured 113 times

HT occured 128 times

TH occured 133 times

TT occured 126 times

Solutions

Expert Solution

#include
#include

int main(void)
{
   // initialise variables
FILE *fp;
int c1,c2;
int hhCount=0,ttCount=0,htCount=0,thCount=0;
fp = fopen("texgt.txt", "r");
if(fp==NULL)
   {
   // if error in opening a file
printf("Error in opening the file..!!");
   exit(1);
   }   
  

while((c1 = fgetc(fp)) != EOF)
{
   // read second character
   c2=fgetc(fp);
   if(c2==EOF)
   break;
  
if(c1=='H')
   {
   if(c2=='H') // this is HH
   hhCount++;
else // This is HT
   htCount++;
   }
   else
   {
   if(c2=='H') // this is TH
   thCount++;
else // this is TT
   ttCount++;
   }
  
}
// print output
printf("HH occured %d times\n",hhCount);
printf("HT occured %d times\n",htCount);
printf("TH occured %d times\n",thCount);
printf("TT occured %d times\n",ttCount);

fclose(fp);

return 0;
}

==================

SCREENSHOT:

OUTPUT:

RANDOM FILE:

HHTTTTHTHTTHHTTHTHHTHTTHTTTHTHTTTTTTTHHTTTHTTTHHHTHTHTTHHTHTTTTTTTTTTTTTHTHHTHHTHTTHTHHTTTHTHHHTHHHHTHTTHHTHTHTHHHHTHHTHTTTHTHTHHTHHHHHHTHHHTTHTHTTHHHHHHHHHHTHTTHHTHTHHHTTHTTHHTHHTHHTTHTTTTTTHTHTHTHHHHTHTHHHHHTHHTTTHTHTTTTTHTHTHHHHTHHTTHHHTHTHTHTTTHHHHTHHTTTHHTTTHTHTHTHTHTHHHHHHHTTHTHTHHHTTTHTHHHHTHHTHTTTHHTHTHHTTTTTHTHTTTHHHTTTTHHHHTTHTTTHHHTHTTHTTHTHHTTTHHTHHTHTTTTTHTHTTHTHTTHTTHTTTHTHHHHTHHTHTHTHHHTTTTTTHHTTHHHTTTHTHHHTHTTTHTHHTTHHHTTTHTHHTHHTTHTHHHHHTHHTHTTTHHHHTHTHTTHTTHHTHTTTTHHHTHHTTTTHHHHTHHTHTTHHTHHTHTTTTHHHTTHHHTHTTHHTTHTTTTTHTTHHTTHHTTTTTTTHHTTTHHTHHTHTHTHTTTTHHHTTHHHHHHTTTHHHHHTTHTHHHHHTHTTHHHTTTTTHTHHTHTHTHHHHTTTTHTTHHHTTTHHTTTHHHHHHHTTHHTHHTHHHTHHHTTHTHTTTHHHHTTTHTHTTTTTHTHTTHTTTTTHHTHHTTTTHHTHTHTHHHTTHHTTHTTHTTTHTHHHHTHTHTHHHHHHTHHTTHHTTHTTHTTTHHTHHHTHTTHTHHTHHHHHHTHTHHTTTTTHTTHTTHTHHTHHHHHTTHHTTHTHTHTHHHHTHTTTHTTTHTTTTTHTTTTHTHHHHHTTHHTTHTTHHTTTTTTHTHHTTTTTTHHTTTHTHTTHTTHTTTHTTTTHTHHHTTHTHHHTTTHHTHHHHHHHHHTHTHTHHHTTHHHHHHTHTTTTTTHHTTTTHHTHTTTTHTTHHTTTTHTTTHTHTTTHTTHHTHT

=============


Related Solutions

Write a C++ program to open and read a text file and count each unique token...
Write a C++ program to open and read a text file and count each unique token (word) by creating a new data type, struct, and by managing a vector of struct objects, passing the vector into and out of a function. Declare a struct TokenFreq that consists of two data members: (1) string value; and (2) int freq; Obviously, an object of this struct will be used to store a specific token and its frequency. For example, the following object...
Write a C++ program to create a text file. Your file should contain the following text:...
Write a C++ program to create a text file. Your file should contain the following text: Batch files are text files created by programmer. The file is written in notepad. Creating a text file and writing to it by using fstream: to write to a file, you need to open thew file as write mode. To do so, include a header filr to your program. Create an object of type fsrteam. Open the file as write mode. Reading from a...
How many words are in the Gettysburg Address? Write a program that reads any text file,...
How many words are in the Gettysburg Address? Write a program that reads any text file, counts the number of characters, num- ber of letters and number of words in the file and displays the three counts. To test your program, a text file containing Lincoln’s Gettysburg Address is included on the class moodle page. Sample Run Word, Letter, Character Count Program Enter file name: GettysburgAddress.txt Word Count = 268 Letter Count = 1149 Character Count = 1440 Do the...
WRITE A C++ PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX (Create index in text file)
WRITE A C++ PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX (Create index in text file)
Write a program to choose a random number X in the interval [2,10] 1000 times and...
Write a program to choose a random number X in the interval [2,10] 1000 times and record what fraction of the outcomes satisfy X > 5, what fraction satisfy 5 < X < 7, and what fraction satisfy x2 −12x+35 > 0. How do these results compare with Exercise 1?
WRITE A JAVA PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX (Create index in text file) full...
WRITE A JAVA PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX (Create index in text file) full code
Design and write a python program that reads a file of text and stores each unique...
Design and write a python program that reads a file of text and stores each unique word in some node of binary search tree while maintaining a count of the number appearance of that word. The word is stored only one time; if it appears more than once, the count is increased. The program then prints out 1) the number of distinct words stored un the tree, Function name: nword 2) the longest word in the input, function name: longest...
create a file with 1000 alphanumeric ones written one per line. Write a program in python...
create a file with 1000 alphanumeric ones written one per line. Write a program in python that randomly selects 100 of them, holds the index of each alphanumeric, adds the hash of the previous 100 and tries to find a random alphanumeric so that if it adds it to the list of indicators the SHA256 of the whole to have 10 zeros at the end. You start with the original Hash: 1111..111 my solution so far: import random import string...
How do I do this: Write a program that can read a text file of numbers...
How do I do this: Write a program that can read a text file of numbers and calculate the mean and standard deviation of those numbers. Print the result in another text file. Put the result on the computer screen. EACH LINE OF THE PROGRAM MUST BE COMMENTED!
PYTHON PROBLEM Given two numbers a and b, count how many times each of the digits...
PYTHON PROBLEM Given two numbers a and b, count how many times each of the digits 0 to 9 occur in all numbers between a and b, inclusive. In order to make your code simpler, implement first the function intToList(n) that takes as input one integer n, and returns a list with the digits of n in the order they appear. For example, intToList(1964) should return [1,9,6,4]. Using the function intToList, implement the function digitsCount(a, b) that returns a list...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT