Hey guys I need help with a program for school. It involves a menu to access sub programs and use of recursive techniques as well as 2D arrays and traversing.
Here's the requirements:
In this program, you are to create a menu-driven program that allows the user to execute one of three recursive routines as many times as desired. The menu must list the three routines, accept the user’s selection, and then perform the chosen recursive routine. The menu must also list an option to quit the program. Use a character to accept the menu choice and error trap for bad menu selections. Also print welcoming and goodbye messages at the start/end of the program.
1. Array Totaler: Given an integer array of size 50 (Filled with numbers read in from a file called “numbers.txt”, which is assumed to have 50 integer values), do the following: A) display the array to the screen (use 5 rows of 10 numbers each), B) ask the user how many numbers they would like totaled (error trap accepting only numbers between 1 and 50), C) Traverse the array recursively starting at the first element in the array and display each element in the array up to the number the user entered, and D) display the total of all the elements traversed.
2. Digit Summation: Ask the user to enter a non-negative integer (error trap for bad input). Then display the number with the digits summed. Repeat until the number has been reduced to a single digit. For example, if the user entered 3456, the next number would be 18 and the final answer would be 9.
3. Loop Faker: Ask the user for a start number, a target number, and an increment number. Make sure that the target is larger than the start number. Print a series of numbers starting with the start number and going up to the target number in increment number increments. The last number in the sequence is allowed to be larger than the target number.
First off, I'm having issues with reading data into a 2D array and I have no idea on how to use recursive techniques or traversing arrays.
Also I'm a bit rusty so my code is probably pretty bad.
This is what I've gotten so far... I really need a better explanation on recursive techniques or maybe some examples?
TIA
#include <iostream>
#include <cctype>
#include <stdlib.h>
#include <fstream>
#include <cstring>
using namespace std;
void welcome(); // welcome message
void showMenu(); // displays menu
void processChoice(); // reads user choice
void arrayTotals(); // array totaler
void digitSums(); // digit summation
void fakeLoops(); // loop faker
int main()
{
welcome();
showMenu();
arrayTotals();
}
void welcome()
{
cout << "\n\t Welcome to the Recursion Exercises! \n\n\n";
cout << "Please choose a recursive routine to sample by pressing the letter for the \noption you want, then press 'enter'.\n\n";
}
void showMenu()
{
char choice = ' ';
cout << "A) Array Totaller: Calculates the total of a portion of an array." << endl;
cout << "B) Digit Summation: Calculates the single digit 'sum' of a number's digits." << endl;
cout << "C) Loop Faker: Looks like a loop, but runs recursively." << endl;
cout << "Q) Quit the program." << endl;
cin >> choice;
}
void processChoice(char userChoice)
{
switch (userChoice)
{
case 'A': case 'a':
arrayTotals();
break;
case 'B': case 'b':
digitSums();
break;
case 'C': case 'c':
fakeLoops();
break;
case 'Q': case 'q':
cout << "\n\n Thank you for your time. \n\n";
exit (1);
break;
default:
cout << "That choice is not listed, please choose from the listed options only! \nPlease choose a recursive routine to sample by pressing the letter for the\noption you want, then press 'enter'.";
}
}
void arrayTotals()
{
int i = 0;
int num[5][10];
int max = 50;
int totalRead = 0;
ifstream inFile;
inFile.open("numbers.txt", ios::in);
while (inFile >> num[totalRead][totalRead] && totalRead < max)
{
totalRead++;
}
}
void digitSums()
{
int posNum;
cout << "Enter a positive number: ";
cin >> posNum;
if (posNum <= 0);
{
cout << "That is not a positive number." << endl;
cout << "Enter a positive number: ";
cin >> posNum;
}
else
{
}
}
void fakeLoops()
{
}