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()
{

}

1) I'm having issues with reading data into a 2D array....

It's probably easiest to use a nested loop to read in values. Since it is given there will be 50 ints in the file you can use for loops:
int temp;

outer loop to control row
  inner loop to control column
    read data into cell indicated by current row and column indexes

2) I have no idea on how to use recursive techniques or traversing arrays.......

Really? Were you sleeping or sick during those lectures? Did your dog eat your notes? How about your text----presumably you have some sort of printed/electronic reference material to use?

Both of us could use a little less hyperbole, eh?

(HINT: use a nested array to traverse the array given it's multidimensional)

3) a better explanation on recursive techniques or maybe some examples:

In general you need some way to stop the function from calling itself. That means there must be some predetermined endpoint that can be recognized and there must be change with each call of the function that can be passed from function call to function call.
Here's two examples:

#include <iostream>
using namespace std;

void countUpFive(int x, int counter)
{   
   if(counter < 5)
   {
     cout << x << ' ';
     countUpFive(++x, ++counter);
   }
}

void countBackward(int x, int counter)
{
   if(counter < 5)
     countBackward(x + 1, ++counter);
  
   cout << x << ' ';
}

int main()
{
    int x = 10;
    int counter = 0;

    countUpFive(x, counter);
    cout << '\n';

    counter = 0;
    x = 10;

    countBackward(x, counter);
    
    cin.get();
    return 0;
}

Expected output:
10 11 12 13 14 
14 13 12 11 10

WARNING: Above code is untested.

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.

As per this requirement I don't see any reason why you should be using a 2d array. You just have to read 50 integers in an array and the output a sum from 0-n. As far as displaying the array in 5 rows of 10 cols, that is just display and has nothing to do with a 2d array. So why are you making your life complicated? Use a 1d array and try recursion with that.

Thanks for the suggestions guys I really appreciate it. Yeah I ironed out the other issues got the three functions left.

I'll try that out Chandra and post back if I have any issues.

This article has been dead for over six months. Start a new discussion instead.