jhonc66
asked on
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;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>>>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
what I meant to say was the code will demonstrate both pass by reference and pass by value
http://www.java2s.com/Tutorial/Cpp/0140__Function/0060__function-parameters.htm
I'm sure you will find what you need in these examples.