Note:
This exercise is worth 2 marks, but you do not have submit any code for it.
You can use dry run as usual:
prompt$ ~cs1921/bin/dryrun lab10 |
prompt$ ~cs1921/bin/dryrun lab10 2 |
(Quacks) Bracket matching is an important problem, e.g. for parsing computer programs. A bracket-matching program checks whether all opening brackets such as '(', '[' and '{' have corresponding closing brackets ')', ']' and '}'. A stack is an excellent data structure to implement a bracket-matching program.
Your tasks:
Download the Quack ADT
Write a program called bracketsQ.c that uses the quack ADT to check whether all the brackets on the stdin text stream are balanced or not
(a+b)*c
(a[i]+b[j])*c[k]
void f(char a[], int n) {int i; for(i=0;i<n;i++) a[i] = (a[i]*a[i]*a[i])*(i+1); return;}
((())){{[]}{()()()}}(()){{}}
|
balanced |
a(a+b*c
a(a+b]*c
a[i]+b[j]*c[k])
((]())
((())){{[]}{(()()}}(()){{}}
|
unbalanced |
(Recursion) In Lab 3, you wrote a program called countplus.c that prints a sequence of numbers. Modify the program to use recursion instead of iteration. Call the modified program recountplus.c. The usage is the same as before.
(Recursion) Now modify recountplus.c to count backwards as well as forwards. Call the modified program recount.c. The direction of counting is determined by the sign of the command-line argument. You should use only one recursive function in the program. An example of counting backwards is:
prompt$ ./recount -12 0,-1,-2,-3,-4,-5,-6,-7,-8,-9,-10,-11,-12 |
You might find this exercise challenging.
Examples of program execution are:
prompt$ ./sumTree 2 1 3 1 2 3 Sum of nodes = 6 |
prompt$ ./sumTree 8 4 6 2 7 5 3 1 12 10 9 14 13 11 15 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Sum of nodes = 120 |
prompt$ ./sumTree 6 5 4 3 2 1 1 2 3 4 5 6 Sum of nodes = 21 |
prompt$ ./sumTree 1 2 3 4 5 6 1 2 3 4 5 6 Sum of nodes = 21 |
prompt$ ./sumTree 5 6 7 1 2 3 4 1 2 3 4 5 6 7 Sum of nodes = 28 |
prompt$ ./sumTree 7 7 Sum of nodes = 7 |
prompt$ ./sumTree Usage: ./sumTree integers ... |
prompt$ ./sumTree 3 10 a Usage: ./sumTree integers ... |
You are not required to submit any code for this exercise. However, you MUST demonstrate this during the lab to obtain the 2 marks for this exercise.
give cs1921 lab10 bracketsQ.c recountplus.c recount.c sumTree.c