Array in function

Hi,

I don't know why my value doesn't pass to the next function, can u please help me.

Thank you,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <cstdarg>
using namespace std;
const float PI = 3.14;
void getUserInput(float diameters[], float prices[]);
void getBestDeal(float diameters[], float prices[], float perSquareInch[]);

int main()

{
	float diameters[3]; //for pizza's diametersure
	float prices[3]; //for pizza's price
	float perSquareInch[3];//for pizza per square inch
	

	getUserInput(diameters, prices);
	getBestDeal(diameters, prices, perSquareInch);
	

	system("pause");
	return 0;
}
//Fuction definition 
//First function
void getUserInput(float diameters[], float prices[])
{
	cout << " Small Pizza varies between 5 to 7 inches. Small pizza costs $12.90 " << endl;
	cout << " Medium pizza varies between 8 to 10 inches. Medium pizza costs $13.40 " << endl;
	cout << " Large pizza varies between 11 to 13 inches. Large pizza costs $27.90 " << endl;

	do{
		cout << "Enter diameter of a small pizza (in inches): " <<endl;
		cin >> diameters[0];
	} while (diameters[0] < 5 || diameters[0] > 7);

	cout << "Enter the price of a small pizza: " <<endl;
	cin >> prices[0];
	do{
		cout << "Enter diameter of a medium pizza (in inches): " <<endl;
		cin >> diameters[1];
	} while (diameters[1] < 8 || diameters[1] > 10);

	cout << "Enter the price of a medium pizza: " <<endl;
	cin >> prices[1];
	do{
		cout << "Enter diameter of a large pizza (in inches): " <<endl;
		cin >> diameters[2];
	} while (diameters[2] < 11 || diameters[2] > 13);

	cout << "Enter the price of a large pizza: " <<endl;
	cin >> prices[2];
}
//Second function 
void getBestDeal(float diameters[], float prices[], float perSquareInch[]){

	for (int i = 0; i < 3; ++i){

		double area;
		area = PI * (diameters[i] / 2) * (diameters[i] / 2);
		perSquareInch[i] = prices[i] / area;
	}
}
I figured it out, I just had to add cout statement to my second function
For anyone who might be interested in my program final result:
Here's the requirements:

1. Create a first function that gets an input from the user for 3 different pizza sizes(in inches) and their prices accordingly. Validate user input therefore only the proper price and pizza size should be accepted.
The valid ranges for pizza sizes : for small 5 to 7, for medium 8 to 10, and for large 11 to 13. The price ranges on pizza are between 12.50 to 32.00.
2. Create a second function that calculates the area of a pizza and price per inch.
3. Create the last function that displays results.


Here's my final code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101

#include <iostream>
#include <cstdarg>
#include <iomanip>
using namespace std;
const float PI = 3.14;
//Function declarations:
void getUserInput(float diameters[], float prices[]);
//getUserInput asks the user to enter the diameter and price of the pizza.
void getBestDeal(float diameters[], float prices[], float perSquareInch[]);
//Calculates the area of a pizza and price per inch.
void printResult(float diameters[], float prices[], float perSquareInch[], int bestDeal);
//Prints the diameter, price, price per square inch and best pizza deal.
int main()

{
	float pizzaSizes[3]; //for pizza's .
	float pizzaPrices[3]; //for pizza's price.
	float perSquareInch[3];//for pizza per square inch.
	int bestPizzaDeal = 0; //for best pizza deal.

	getUserInput(pizzaSizes, pizzaPrices);
	//First function call
	getBestDeal(pizzaSizes, pizzaPrices, perSquareInch);
	//Second function call
	printResult(pizzaSizes, pizzaPrices, perSquareInch, bestPizzaDeal);
	//Third function call

	system("pause");
	return 0;
}
//Fuction definition 
//First function
void getUserInput(float diameters[], float prices[])
{
	cout << " Small Pizza varies between 5 to 7 inches. Small pizza costs $12.90 " << endl;
	cout << " Medium pizza varies between 8 to 10 inches. Medium pizza costs $13.40 " << endl;
	cout << " Large pizza varies between 11 to 13 inches. Large pizza costs $27.90 " << endl;
	cout << endl;
	cout << endl;

	do{
		cout << "Enter diameter of a small pizza (in inches): ";
		cin >> diameters[0];
	} while (diameters[0] < 5 || diameters[0] > 7);

	cout << "Enter the price of a small pizza: ";
	cin >> prices[0];
	do{
		cout << "Enter diameter of a medium pizza (in inches): ";
		cin >> diameters[1];
	} while (diameters[1] < 8 || diameters[1] > 10);

	cout << "Enter the price of a medium pizza: ";
	cin >> prices[1];
	do{
		cout << "Enter diameter of a large pizza (in inches): ";
		cin >> diameters[2];
	} while (diameters[2] < 11 || diameters[2] > 13);

	cout << "Enter the price of a large pizza: ";
	cin >> prices[2];
}
//Second function 
void getBestDeal(float diameters[], float prices[], float perSquareInch[]){

	for (int i = 0; i < 3; ++i){

		double area;
		area = PI * (diameters[i] / 2) * (diameters[i] / 2);
		perSquareInch[i] = prices[i] / area;
	}

	/*int min = 0;
	float minSquareInch = perSquareInch[0];
	for (int i = 0; i < 3; ++i){
		if (perSquareInch[i] < minSquareInch){
			minSquareInch = perSquareInch[i];
			min = i;
		}
	}*/
}

//Third function
void printResult(float diameters[], float prices[], float perSquareInch[], int bestDeal){

	cout << fixed << setprecision(2);
	cout << "Small pizza Diameter = " << diameters[0] << " inches, Price = $" << prices[0] << " .Per square inch = $" << perSquareInch[0]<<endl;
	cout << "Medium pizza Diameter = " << diameters[1] << " inches, Price = $" << prices[1] << " .Per square inch = $" << perSquareInch[1]<<endl;
	cout << "Large pizza Diameter = " << diameters[2] << " inches, Price = $" << prices[2] << " .Per square inch = $" << perSquareInch[2]<<endl;

	if (bestDeal == 0)
		cout << "Dear customer the small pizza is the better buy."<< endl;

	else if (bestDeal == 1)
		cout << "Dear customer the medium pizza is the better buy."<<endl;

	else
		cout << "Dear customer the large pizza is the better buy."<<endl;

}

Let me know if you have any questions, comment or clarification.
Topic archived. No new replies allowed.