Question: Write a C program to maintain n counters indexed by 0 .. n-1. n will be the first input value and all counters are initially
Write a C program to maintain n counters indexed by 0 .. n-1. n will be the first input value and all counters are initially valued as zero. The following operations will then appear, one per line, in the input: a. 0 - terminate execution.
b. 1 - print the counters in ascending index value order as (index, count) pairs. (O(n) time)
c. 2 - print the counters in ascending counter value order as (index, count) pairs. (O(n) time)
d. 3 i - add one to the counter indexed by i. (O(log n) time)
e. 4 i - subtract one from the counter indexed by i. (O(log n) time)
f. 5 i j - determine the number of counters whose values are no smaller than i and no larger than j. (O(log n) time) The input will be read from standard input (stdin) as either keyboard typing or as a shell redirect (<) from a file.
Prompts/menus are completely unnecessary! Your program should dynamically allocate three tables - map, index, and count. (If you wish, the last two tables may be implemented as an array of structs.) index[i] indicates which of the n counters has its value presently stored as count[i]. map[i] is used to find the counter with index i, i.e. it is always true that index[map[i]]==i. Operation 2 may be coded as: for (i=0;i using the file below where the first line is the input, the first number of the second line to the last line is the menu option, the next number in the second line to the last line is what needs to be done 5 3 4 3 4 3 3 3 4 3 3 3 4 3 3 3 2 3 4 3 3 3 2 3 1 3 4 3 3 3 2 3 1 3 0 1 2 5 3 4 3 4 3 4 3 3 3 4 3 3 3 4 3 3 3 2 3 4 3 3 3 2 3 1 3 4 3 3 3 2 3 1 3 0 1 2 5 4 6 4 4 4 4 4 4 4 4 4 4 4 4 4 3 4 3 4 3 4 3 4 4 4 4 1 2 5 4 6 0
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
