Starting from:

$25

CH-230-A-Assignment 8 Solved


Assignment 8 - Queues, Stacks, Files

•    The problems of this assignment must be solved in C or C++ (instruction in each problem).

•    Your programs should have the input and output formatting according to the testcaseslisted after the problems.


Take a look at the three files and understand the source code. Extend the code of queue.c by implementing the enqueue() function. Follow the hints given in the slides (see Lecture 8, slide

12).

You can assume that the input will be valid. To pass the testcases your output has to be identical with the provided ones.

 Testcase 8.1: inputTestcase 8.1: output
aadd int: Putting 3 into queue

31 items in queue

aType a to add, d to delete, q to quit:

5add int: Putting 5 into queue a2 items in queue

7Type a to add, d to delete, q to quit: qadd int: Putting 7 into queue

3 items in queue

Type a to add, d to delete, q to quit:

Bye.

Problem 8.2 Removing from the queue                                                                  

Extend the source code of queue.c from Problem 8.1 by implementing the dequeue() function. Follow the hints given in the slides (see Lecture 8, slide 13) and consider the case of a queue underflow.

You can assume that the input will be valid except the semantical possibility of reaching queue underflow. To pass the testcases your output has to be identical with the provided ones.

Testcase 8.2: input
 Testcase 8.2: output

add int: Putting 3 into queue

1     items in queue

Type a to add, d to delete, q to quit:

add int: Putting 5 into queue

2     items in queue

Type a to add, d to delete, q to quit:

add int: Putting 7 into queue

3     items in queue

Type a to add, d to delete, q to quit:

Removing 3 from queue

2 items in queue

Type a to add, d to delete, q to quit:

Removing 5 from queue

1 items in queue

Type a to add, d to delete, q to quit:

Bye.
a 3 a 5 a 7 d d q
 
Problem 8.3
Printing the queue

Language: C
Extend the source code of queue.h, queue.c and testqueue.c from Problem 8.2 by adding and implementing the additional function printq() for printing the elements of the queue separated by spaces. If you enter ’p’, then the program should print the elements of the queue. Make sure that you can print more than once.

You can assume that the input will be correct. To pass the testcases your output has to be identical with the provided ones.

Testcase 8.3: input
Testcase 8.3: output

add int: Putting 3 into queue

1    items in queue

Type a to add, d to delete, p to print, q to quit:

add int: Putting 5 into queue

2    items in queue

Type a to add, d to delete, p to print, q to quit:

add int: Putting 7 into queue

3    items in queue

Type a to add, d to delete, p to print, q to quit: content of the queue: 3 5 7

3 items in queue

Type a to add, d to delete, p to print, q to quit:

Bye.
a 3 a 5 a 7 p q
 
Problem 8.4
A stack for converting numbers                                                    
 
Language: C
Modify the stack implemented for Problem 7.7 such that you can use it for converting a positive decimal number stored in an unsigned int into the binary representation of the number using division by 2 and storing the remainder of the division by 2 in the stack.

Upload again all files related to this problem (i.e., stack.h, stack.c and convertingstack.c). You can assume that the input will be valid. To pass the testcases your output has to be identical with the provided ones.

 Testcase 8.4: inputTestcase 8.4: output
75The binary representation of 75 is 1001011.

Problem 8.5 Read chars and write an int                                                               
Write a program which reads the first two characters from the file “chars.txt” and writes the sum of their ASCII code values as a number into “codesum.txt”. Use an editor to create the input file “chars.txt”. Your program is responsible to create the output file “codesum.txt”. You can safely assume that the content of the input file will be valid.

Problem 8.6 Read and write doubles                                                                      
Write a program which reads from the keyboard the names of two files containing two double numbers. Your program should read these two values from the two files, compute their sum, difference, product and division, and write the results on separate lines into the file “results.txt”. You can safely assume that the input is valid, the two input files exist and each contains one valid double value.

Problem 8.7 Merge two files

Language: C
Write a program which reads the content of two files “text1.txt” and “text2.txt” line by line and merges them into another file called “merge12.txt”.

You can safely assume that the input is valid.

Problem 8.8 Counting words in a file

Language: C
Write a program which reads the content of a file given as input and counts the number of the words in the file. It is assumed the words are separated by one or multiple of the following characters: ’ ’ ’,’ ’?’ ’!’ ’.’ ’\t’ ’\r’ ’\n’. For testing your solution to this problem, please use:

https://grader.eecs.jacobs-university.de/courses/320112/c/words.txt https://grader.eecs.jacobs-university.de/courses/320112/c/words2.txt You can assume that the content of the input file will be valid if existing.

 Testcase 8.8: inputTestcase 8.8: output

words.txtThe file contains 17 words.

Problem 8.9 Concat n files                                                                                     
Write a program which reads from the standard input the value of an integer n and then the names of n files. The program should concatenate the content of the n files separated by ’\n’ and write the result on the standard output and also into output.txt.

Read the input files and write the output file using the binary mode. Use a char buffer of size 64 bytes and chunks of size 1 byte when reading and the same buffer with chunks of size 64 bytes (or less if the last write and file size is not a multiply of 64) when writing. For testing your solution to this problem, please use:

 Testcase 8.9: input

3 file1.txt file2.txt file3.txt
Testcase 8.9: output

Concating the content of 3 files ...

The result is: The first file’s content.
The second file’s content. The third files’s content.

The result was written into output.txt

More products