I have a two part assignment. Part one is to create a menu driven program using variables to do certain tasks. Part two is to convert the variables being used into 2 arrays. The code for part one is post below and works perfectly for everything that is required. My problem is I do not have the slightest clue about how to change all my variables being used into arrays and getting the program to even compile let alone run successfully. Thanks in advance for all your help.

HEADER.H

#include <iostream>

using namespace std;

#include <cmath>

const int SUCCESS = 0;

const int FAILURE = 1;

void mainMenu();

int getSum(int x, int y);

int getDifference(int a, int b);

int getProduct(int c, int d);

double computePower(double &e, double &f);

int getFact(int &g);

void binary(int h);

int sum = 0;

int difference = 0;

int product = 0;

double answer = 0;

int factorial = 1;

**************************************
MAIN.CPP

#include "header.h"

int main()
{
	mainMenu();

	return SUCCESS;

}   // end of main

*****************************************
FUNCTIONS.CPP

#include "header.h"

void mainMenu(){

	int option = 0;
	int x = 0;
	int y = 0;
	int a = 0;
	int b = 0;
	int c = 0;
	int d = 0;
	double e = 0;
	double f = 0;
	int g = 0;
	int h = 0;

	cout << endl;	
	cout << "Choose an option you wish to execute." << endl;
	cout << "[1] Add" << endl;
	cout << "[2] Subtract" << endl;
	cout << "[3] Multiply" << endl;
	cout << "[4] Factorial" << endl;
	cout << "[5] Binary" << endl;
	cout << "[6] Power" << endl;
	cout << "[7] Quit" << endl << endl;

	cout << "Enter your choice now:  ";

	cin >> option;

	if ( option <= 0 || option > 7 ){

		cout << endl;
		cout << "Option number not valid!" << endl << endl;

		return;

	}   // end of if

	if ( option == 1 ){

		getSum(x,y);

		cout << endl;
		cout << "The sum is = " << sum << endl << endl;

		mainMenu();

	}   // end of if

	if ( option == 2 ){

		getDifference(a,b);

		cout << endl;
		cout << "The difference is = " << difference << endl << endl;

		mainMenu();

	}   // end of if

	if ( option == 3 ){

		getProduct(c,d);

		cout << endl;
		cout << "The product is = " << product << endl << endl;

		mainMenu();

	}   // end of if

	if ( option == 6 ){

		computePower(e,f);

		cout << endl;
		cout << e << " to the power of " << f << " is " << answer << endl << endl;

		mainMenu();

	}   // end of if

	if ( option == 4 ){

		getFact(g);

		cout << endl;
		cout << g << "! is " << factorial << endl << endl;

		mainMenu();

	}   // end of if

	if ( option == 5 ){

		cout << endl;
		cout << "Enter number:  ";

		cin >> h;

		if ( h < 0 ){

		cout << "Only positive numbers can be converted into binary, try again!" << endl << endl;

		return;

		}   // end of if

		cout << endl;
		cout << "Your number converted to binary is:  ";	
		binary(h);

		cout << endl;
		mainMenu();

	}   // end of if

	if ( option == 7 ){

		cout << endl;
		cout << "Thank you for using our program, have a nice day =] " << endl << endl;

		return;		

	}   // end of if


}   // end of function

//======================================

int getSum(int x, int y){

	cout << endl;
	cout << "Enter number:  ";

	cin >> x;

	cout << endl;
	cout << "Enter number:  ";

	cin >> y;

	sum = x + y;

	return sum;

}   // end of function

//================================

int getDifference(int a, int b){

	cout << endl;
	cout << "Enter number:   ";

	cin >> a;

	cout << endl;
	cout << "Enter number:  ";

	cin >> b;

	difference = a - b;

	return difference;

}   // end of fucntion

//===================================

int getProduct(int c, int d){

	cout << endl;
	cout << "Enter number:  ";

	cin >> c;

	cout << endl;
	cout << "Enter number:  ";

	cin >> d;

	product = c * d;

	return product;

}   // end of function

//=======================================

double computePower(double &e, double &f){

	cout << endl;
	cout << "Enter number:  ";

	cin >> e;

	cout << endl;
	cout << "Enter power:  ";

	cin >> f;

	answer = pow(e,f);

	return answer;

}   // end of function

//========================================

int getFact(int &g){

	int n = 1;

	cout << endl;
	cout << "Enter number[1,12]:  ";

	cin >> g;

	if ( g < 1 || g > 12 ){

		cout << endl;
		cout << "Factorial of " << g << " will not compute correctly, try again!" << endl << endl;

		return FAILURE;

	}   // end of if

	while ( n <= g ){

		factorial = factorial * n;

		n++;

		
	}   // end of while

	return factorial;	

}   // end of function

//=========================================

void binary(int h){

	int remainder;

	if ( h <= 1 ){

		cout << h;

		return;

	}   // end of if

	remainder = h % 2;
	binary(h >> 1);

	cout << remainder;
	

}   // end of function

//===============================================

Recommended Answers

All 8 Replies

I have a two part assignment. Part one is to create a menu driven program using variables to do certain tasks. Part two is to convert the variables being used into 2 arrays. The code for part one is post below and works perfectly for everything that is required. My problem is I do not have the slightest clue about how to change all my variables being used into arrays and getting the program to even compile let alone run successfully. Thanks in advance for all your help.

Which variables are you supposed to turn into arrays? What do the two arrays represent?

I believe all the variables are suppose to be turned into just two arrays. So the two arrays are suppose to hold the users inputs for each function.

I believe all the variables are suppose to be turned into just two arrays. So the two arrays are suppose to hold the users inputs for each function.

So each function gets passed two arrays? That doesn't sound right for a function like getDifference, which is subtracting one integer from another. Are you sure you aren't supposed to have ONE array and have the SIZE of the array be 2 elements, as in this?

int getDifference (int x[])
{
     int difference = x[0] - x[1];
     return difference;
}

No it is suppose to be two separate arrays so and example of getDifference would be something like this for user inputs such as: 5700 - 600 = 5100

int Array1[] = { 5 7 0 0 }
int Array2[] = { 0 6 0  0 }

int difference = { 5 1 0 0 }

No it is suppose to be two separate arrays so and example of getDifference would be something like this for user inputs such as: 5700 - 600 = 5100

int Array1[] = { 5 7 0 0 }
int Array2[] = { 0 6 0  0 }

int difference = { 5 1 0 0 }

Well I suppose how you proceed will depend on what you are familiar with already. Are you familiar with pointers and how to dynamically allocate arrays? If you don't want to dynamically allocate the arrays, you can decide on a maximum number of digits ahead of time and allocate your arrays to that constant size in the beginning. Do you need 2 arrays or 3? For example, where are you going to store the answer in getDifference? In a third array or are you supposed to overwrite one of the two you have? And do you want your arrays to return a value or are you planning on having the answer be one of the parameters passed to the function, which the function will then change?

The teacher just added that we are suppose to use const int MAXARRAY = 1000 for the arrays. To answer your other questions. I will be using 3 arrays with 2 of the arrays being passed into the function and then the third array being created inside the function and being returned back to the calling program.

The teacher just added that we are suppose to use const int MAXARRAY = 1000 for the arrays. To answer your other questions. I will be using 3 arrays with 2 of the arrays being passed into the function and then the third array being created inside the function and being returned back to the calling program.

You can't return an array. You can return a pointer to an array.

http://www.daniweb.com/forums/thread81371.html

You say that you are creating the array inside the function, then returning that array. Again, what you'll be returning is a pointer to the array that is created. It seems to me that you'll need to create that new array in the function dynamically with the "new" command. Otherwise you may run into "out of scope" problems.

This sample program may give some ideas on how to create and pass the arrays. It doesn't do any calculations and there are other things you'll need to deal with to make it work for your application. Is this what you had in mind?

#include <iostream>
using namespace std;

int*  funcD (int a[], int b[]);

int main ()
{
    int a[1000];
    int b[1000];
    int* c;
    
    c = funcD (a, b);
    for (int i = 0; i < 10; i++)
         cout << c[i];
         
    cout << endl;
    return 0;
}
    
    
    
int* funcD (int a[], int b[])
{
       int* c  = new int[1000];
       for (int i = 0; i < 1000; i++)
            c[i] = i;
            
       return c;
}

Yes, that is what i had in mind about what I would like to do with the arrays that I will be creating.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.