[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 325
  • Last Modified:

sending values as parameters

Hi , i would like to send values through functions as parameters,, the idea is to be able to take either send a value or the complete array to be able to calculate an average of weight or height and calling another function for this.... I'm just not sure how to do so... Thanks.
#include <iostream>
using namespace std;

// program to calculate the BMI 
//Author Jhon Quintero
//ub30089043
//Programming 2


// list of prototypes
void bmicalculation ();
void averageweight ();
void averageheight ();
void averagebmi ();
void displayreports();
void menu();

//this function calculates the values for the BMI
void bmicalculation () {

		//initializing the multi-dimensional array 2 values, 5 persons
	float BMIaray[5][2];
	float aveweight=0;
	float aveheight=0;
	


	//nested for loop for input values
	for (int i=0 ;i<5;i++)
	{
		cout<<"please enter the person #"<<i+1<<" data"<<endl;

			for (int j=0 ;j<2;j++)
			{

				if (j==0){
				cout<<"weight (kg)? "<<endl;
				cin>>BMIaray[i][j];
				aveweight=aveweight+BMIaray[i][j];
				
				}
				else{
					cout<<"height (m)? "<<endl;
					cin>>BMIaray[i][j];
					aveheight=aveheight+BMIaray[i][j];
				}

			}
			
			cout<<" "<<endl;

	}

	cout<<"average weight "<<endl;
	cout<<aveweight;
	cout<<" "<<endl;
	cout<<"average Height"<<endl;
	cout<<aveheight;
	cout<<" "<<endl;


	//nested for loop for output values
	float h=0;
	float w=0;


	for (int i=0 ;i<5;i++)
	{
		if (i==0){
		cout<<"\t\t"<< "Weight (kg)"<<"\t";
		cout<< "Height (m)"<<"\t";
		cout<< "Bmi"<<"\t"<<endl;
		}
		cout<< "person "<<i+1<<": ";


			for (int j=0 ;j<2;j++)
			{
				cout<<"\t"<< BMIaray[i][j]<<"\t";

				if (j==0){
					
					w=BMIaray[i][j];
				}
				else{
				
					h=BMIaray[i][j];
				}


				

			}

			cout<<"\t"<<w/(h*h);
			cout<<endl;

	}

	//going back to the menu
	menu();


}
//this function calculates the average weight
void singlebmi(){

	float weight=0;
	float height=0;
	float bmi=0;


	cout<<"please input the Weight (kg)"<<endl;
	cin>>weight;
	cout<<"please input the Height (m)"<<endl;
	cin>>height;
	bmi=weight/(height*height);
	cout<<" "<<endl;
	cout<<"The BMI is: "<<bmi<<endl;



	//going back to the menu
	menu();

	
}
void averageweight (){
}
//this function calculates the average height
void averageheight (){
}
//this function calculates the average bmi
void averagebmi (){
}
void displayreports(){

}
//main menu function
void menu(){

	int option=0;

	cout<<"1.Calculate a single BMI"<<endl;
	cout<<"2.Enter data"<<endl;
	cout<<"3.Display Reports"<<endl;
	cout<<"4.Exit"<<endl;
	cout<<" "<<endl;

	cin>>option;

	switch (option)
	{
	case 1:
		singlebmi();
		break;
	case 2:
		bmicalculation ();
		break;
	case 3:
		displayreports();
		break;
	case 4:
		exit(0);
		break;

	default:
		cout<<"not a Valid option , please try again"<<endl;
		cout<<" "<<endl;
		break; 

	}



}




int main () {


	menu();

	//calling the function to input data and calculate bmi
	bmicalculation();

	system("pause");
	return 0;
}

Open in new window

0
jhonc66
Asked:
jhonc66
  • 2
1 Solution
 
phoffricCommented:
There is much to say on this topic. This C++ Function tutorial link provides each concept with a code example.
     http://www.java2s.com/Tutorial/Cpp/0140__Function/0060__function-parameters.htm

I'm sure you will find what you need in these examples.
0
 
trinitrotolueneCommented:
I've modified your code a bit to demonstrate passing by reference. Take a look at option 5. This should give you an idea of passing by value.

Have also added a display function within the bmicalculation() function. This function accepts an array as an argument. Passing an array is however achieved by passing the array's actual address.
// testvalParam.cpp : Defines the entry point for the console application.
//
#include <iostream>
using namespace std;

// program to calculate the BMI 
//Author Jhon Quintero
//ub30089043
//Programming 2


// list of prototypes
void bmicalculation ();
void averageweight ();
void averageheight ();
void averagebmi ();
void displayreports();
void display(float arr[][2]);
void ValPass(int a, float b);
void menu();

//this function calculates the values for the BMI
void bmicalculation () {

		//initializing the multi-dimensional array 2 values, 5 persons
	float BMIaray[5][2];
	float aveweight=0;
	float aveheight=0;
	


	//nested for loop for input values
	for (int i=0 ;i<5;i++)
	{
		cout<<"please enter the person #"<<i+1<<" data"<<endl;

			for (int j=0 ;j<2;j++)
			{

				if (j==0){
				cout<<"weight (kg)? "<<endl;
				cin>>BMIaray[i][j];
				aveweight=aveweight+BMIaray[i][j];
				
				}
				else{
					cout<<"height (m)? "<<endl;
					cin>>BMIaray[i][j];
					aveheight=aveheight+BMIaray[i][j];
				}

			}
			
			cout<<" "<<endl;

	}

	cout<<"average weight "<<endl;
	cout<<aveweight;
	cout<<" "<<endl;
	cout<<"average Height"<<endl;
	cout<<aveheight;
	cout<<" "<<endl;
	
	display(BMIaray); //passing array address here. 
	
	//going back to the menu
	menu();


}
//this function calculates the average weight
void singlebmi(){

	float weight=0;
	float height=0;
	float bmi=0;


	cout<<"please input the Weight (kg)"<<endl;
	cin>>weight;
	cout<<"please input the Height (m)"<<endl;
	cin>>height;
	bmi=weight/(height*height);
	cout<<" "<<endl;
	cout<<"The BMI is: "<<bmi<<endl;

	

	//going back to the menu
	menu();

	
}

//receives an array as a Parameter
void display(float myarray[][2])
{

	float w=0,h=0;
	for (int i=0 ;i<5;i++)
	{
		if (i==0){
		cout<<"\t\t"<< "Weight (kg)"<<"\t";
		cout<< "Height (m)"<<"\t";
		cout<< "Bmi"<<"\t"<<endl;
		}
		cout<< "person "<<i+1<<": ";


			for (int j=0 ;j<2;j++)
			{
				cout<<"\t"<< myarray[i][j]<<"\t";

				if (j==0){
					
					w=myarray[i][j];
				}
				else{
				
					h=myarray[i][j];
				}			

			}

			cout<<"\t"<<w/(h*h);
			cout<<endl;

	}
}

void averageweight (){
}
//this function calculates the average height
void averageheight (){
}
//this function calculates the average bmi
void averagebmi (){
}
void displayreports(){

}

void ValPass(int a, float b)
{
	cout<<"The values passed are :"<<a<<" and "<<b<<endl;
}
//main menu function
void menu(){

	int option=0;

	cout<<"1.Calculate a single BMI"<<endl;
	cout<<"2.Enter data"<<endl;
	cout<<"3.Display Reports"<<endl;
	cout<<"4.Exit"<<endl;
	cout<<"5.Test Value passing"<<endl;
	cout<<" "<<endl;

	cin>>option;

	switch (option)
	{
	case 1:
		singlebmi();
		break;
	case 2:
		bmicalculation ();
		break;
	case 3:
		displayreports();
		break;
	case 4:
		exit(0);
		break;

	case 5:
		ValPass(23, 34.5);
		break;

	default:
		cout<<"not a Valid option , please try again"<<endl;
		cout<<" "<<endl;
		break; 

	}



}




int main () {


	menu();

	//calling the function to input data and calculate bmi
	//bmicalculation();

	system("pause");
	return 0;
}

Open in new window

0
 
trinitrotolueneCommented:
>>>>I've modified your code a bit to demonstrate passing by reference
what I meant to say was the code will demonstrate both pass by reference and pass by value
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now