Question: Question [35 Marks] You are given a C++ program ( Debug.cpp ) with errors (syntax errors and/ or logical errors, if any). The program has

Question [35 Marks]

You are given a C++ program (Debug.cpp) with errors (syntax errors and/ or logical errors, if any). The program has three (3) user-defined functions as listed below:

Function Name

Description

getMenu

The function asks the users to enter a menu name and then return the menu name as a string pointer-type variable. The returned menu name is later assigned as an item of string pointer type array variable named menus.

getPricePcs

The function accepts two arguments. The first argument is the caption text to guide users to enter the price or the pieces number of chicken on the menu. The second argument is either the price or the piece of chickens which were represented by an array of integer pointer type variables: price and pcs. The getPricePcs function assigns data entered by the users to the corresponding item of these array variables.

cheapestMenu

The function accepts three arguments that are the menus, price, and pcs parallel array pointer-type variables. It calculates the price for one piece of chicken on each menu (price / pcs) and then returns the index number of array items which representing the cheapest menu (menu with lowest price for one piece of chicken).

The main function of the program has a series of calls to getMenu and getPricePcs functions inside a loop control structure. The menus, price, and pcs are parallel pointer type arrays with references to their items index was made based on variable used to control the loop (loop which used to make a series of call to getMenu and getPricePcs functions). Some of the output was produced by a call made to cheapestMenu function. The last part of the output produced after the index number of parallel array items representing the cheapest menu was returned by the cheapestMenu function.

You are required to debug the errors, compile, and run the program. You are NOT ALLOWED to remove any statements in the program. You are only allowed to update the statements provided in the program and add a new statement(s) if absolutely necessary.

The program should produce the outputs as in Figure 1. Note: The values in bold are input by the user.

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

//Debug.cpp

#include

#include

#define SIZE 3

using namespace std;

string getMenu() { string *p_data = new string; cout << "Menu name: "; getline(cin, *p_data); return p_data;

} void getPricePcs(string caption, int *p_data[]) { cout << caption; cin >> p_data;

} int cheapestMenu(string *m[], int *pr, int *pc) { float pcs_price, pcs_price_lowest = 0; int idx; cout << "Check chicken price per-pcs: ";

for (int i = 0; i < SIZE; i++) { pcs_price = (float) *pr[i] / *pc[i]; cout << *m[i] << " - " << pcs_price << " ";

if (pcs_price_lowest < pcs_price) { pcs_price_lowest = pcs_price; idx = i;

} } cout << " ";

return idx;

}

// Start main function int main() { string *menus [SIZE]; int *price; int *pcs [SIZE]; // pieces of chicken

// Examples of menus, price and pcs of chicken

// Chicken Deluxe - RM 30 - 5 pcs // Happy Combo - RM 51 - 8 pcs // Family Bucket - RM 75 - 15 pcs for (int i = 0; i < SIZE; i++) { menus[i] = getMenu();

price[i] = new string; getPricePcs("Price (RM): ", price[i]);

getPricePcs("Chicken (pcs): ", pcs[i]);

cin.ignore(); // need this as we mix the use of getline and cin

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

cout << " ";

} int idx_cheapest = cheapestMenu(menus, price, pcs);

cout << "Cheapest menu is " << *menus[idx_cheapest]

<< " priced at RM " << *price[idx_cheapest] << " for "

<< pcs[idx_cheapest] << " pieces of chicken ";

// delete array data from memory for (int i = 0; i < SIZE; i++) { delete menus[i]; delete price[i]; delete pcs[i];

} return 0;

}

Menu name: Chicken Deluxe

Price (RM): 30

Chicken (pcs): 5

Menu name: Happy Combo

Price (RM): 51

Chicken (pcs): 8

Menu name: Family Bucket

Price (RM): 75

Chicken (pcs): 15

Check chicken price per-pcs:

Chicken Deluxe - 6

Happy Combo - 6.375

Family Bucket - 5

Cheapest menu is Family Bucket priced at RM 75 for 15 pieces of chicken

Figure 1: The example of output

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!