Question: Question 1 and Question 2. Feel free to write both Q1 and Q2 in one exam_main.c or two of them. Use in1.txt, in2.txt, in3.txt for
Question 1 and Question 2. Feel free to write both Q1 and Q2 in one exam_main.c or two of them. Use in1.txt, in2.txt, in3.txt for your testing. The output files of using in1.txt are: out1.txt, out2.txt, and out3.txt They are included. Your output should look the same if correct. #pragma warning (disable: 4996) //for those using windows only #include#include #include #include #include enum { MaxLen = 255, space = 32, tilda = 126}; void print_histogram(FILE *f, int h[]) { fprintf(f, "ch \th[ch] "); //header fprintf(f, "== \t===== "); //header Write a for loop from space to <= tilda and use fprint to print ch and h[ch] } You can write the two questions (loops) in one main program or two programs. int main(void) { printf("Welcome to exam1 program "); Q1: Write an infinite while loop (while true) that reads an input text file and generates 3 output text files: out1.txt, out2.txt, out3.txt as follows: (Do not ask the user to enter the names of the 3 output files) char fin_name[MaxLen]; FILE* fin; FILE* f1, *f2, *f3; First infinite loop (while true) asks the user to enter a text file name fin_name. if (strlen(fin_name) < 1) break the while loop. Then open four text files: fin input file with the name fin_name f1, f2, f3 output text files Inside the loop: declare: int ch = ' '; //use int not char because eof = -1 read one character ch at a time and: In f1, write all alphanumerical chars and spaces (space, end of lines, ..) Alphanumerical chars mean letters and digits. In f2, write all the punctuation chars and spaces (space, end of lines, ..) Use the is functions like isspace etc to check. In f3, write a histogram of all characters and their frequency in fin. Complete the print_histogram function and use it. To find the histogram, declare an array of int and initialize it to 0: int hist[tilda + 1] = { 0 }; //initialize all entries to 0 while reading characters from fin to store in f1, f2, or both (for spaces), increment the entry in hist for that character: hist[ch]++; When you finish reading fin and writing to f1 and f2, call print_histogram(f3, hist) to write it to hist. Then close all 4 files: fin, f1, f2, and f3. Q2: Write an infinite while loop (while true) and inside it: declare two integers: int n1 = 0, n2 = 0; Ask the user to enter two integers and read them into n1 and n2 Use scanf to read integers n1 and n2. Find the result q (quotient) of n1/n2 in integer format not double. Find the remainder r (remainder) of the integer division of n1 by n2. if n2 is 0, do not calculate q and r but ask for new values. if n1 is negative or n2 is negative: break the while loop print to screen q and r as integers. return 0; }
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
