Lab 12: More Recursion
/*
LAB 12: Recursion Again
In this lab you will expand upon the code listed below to create a program that uses recursion to count how many characters are between each pair of (),
not counting characters that are within other ()s. For example:
(abc(ab(abc)cd)(()ab))
Would result in the following output (only not bold):
Level 3: 3
Level 2: 4
Level 3: 0
Level 2: 2
Level 1: 3
Another example would be this:
(abc(ab(b)))
Level 3: 1
Level 2: 2
Level 1: 3
"Level" Refers to the level of () nesting - 3 means the characters are within a pair within a pair within a pair.
You may assume all input will only have a single level 1 pair, and that all other pairs will be within that pair (I.E, all input will start with '(' and end with
the ')' that forms a pair with the start of the input).
SPECIAL NOTICE!: For this lab, you must have the 'TA' grade your program as a '2' before you can attempt for a '3' - don't try to get a '3' before already receiving
a '2' from your TA. Of course, your TA will then change your grade to a '3' if you complete it in time.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_INPUT_LENGTH 100
int recursiveParaCheck(char input[], int doesNotMatterWhatVarNameIsButItIsStandardToMatchFunction, int level);
void main()
{
char input[MAX_INPUT_LENGTH];
char notDone = 'Y';
do
{
//Read in input
printf("Please enter input: ");
scanf(" %s", input);
//Call Recursive Function to print out desired information
recursiveParaCheck(input, 1, 1); // I assume that position 0 contains a '(', as is noted in the program definition.
printf("\n Would you like to try again? Y/N: ");
scanf(" %c", ¬Done);
notDone = toupper(notDone);
}while(notDone == 'Y');
}
int recursiveParaCheck(char input[], int startPos, int level)
{
int pos = startPos;
int total = 0;
do
{
if(input[pos] != '(' && input[pos] != ')')
{
++total;
}
//What is the base case? (The point at which you stop making recursive calls and start resolving them.((x)))
/*if(BASE CASE)
{
//Do stuff!
}
//When do you need to make a recursive call?
if(SITUATION WHERE WE MAKE RECURSIVE CALL)
{
//Do stuff!
}
*/
//HINT: the above two IF statements should be the ONLY THINGS you need to add to this program to make it work (for 2 points, anyway)
++pos;
}while(input[pos] != '\0'); // until the end of the input string
}
/*
POINTS:
1 Point: Have the correct conditions for the two IF statements (both base case and situation where you need to make recursive call)
2 Points: Have a working program that prints out the needed information as noted above.
3 Points(Extra Credit): Alter the program so that it prints out the levels in top-down order.
So instead of
Level 3: 3
Level 2: 4
Level 3: 0
Level 2: 2
Level 1: 3
The program would print:
Level 1: 3
Level 2: 4
Level 2: 2
Level 3: 3
Level 3: 0
You may assume there will never be more than 3 levels of ()s.
Hint: You will somehow need to store each [level: Value] pair/string, and then sort them after all of them have been stored (or store them in such a way that later sorting is unneeded).
*/ //You can copy-paste everything before this into your editor of choice.
Notes:
Keep in mind that this is a team effort so you should agree with your partner on what you are going to do before you start typing. The partner who is typing is the "driver" and the partner watching is the "navigator." Be sure to switch roles every 10 to 15 minutes, to foster a deep understanding of the code for both partners. The navigator should be watching for syntax errors and verifying the correctness of the code you're writing.
It will speed things up for you if you keep a window open for editing and have a separate window open for compiling and running your program. Remember that windows are resizeable!
Submission:
You should work with a partner for a grade. Only one submission per group is necessary, but be sure to include your name if you work alone, or both people's names if you worked with a partner.
You should submit the lab to the Blackboard at the end of the lab session, by 50 minutes after the hour.