User Defined Functions and Arrays

Good Morning,
I realized on my last question I phrased it wrong, I would like to have a user defined function that I pass an array to for editing.
Is this possible, when I do the pass to the function and add the & it errors out on me.

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
102
103
104
105
106
107
108
109
110
#include <iostream> //allows use of cout and endl commands
#include <iomanip>  // allows use of manipulation commands (setprecision, ect)


using namespace std; //This line allows you to use cin, cout and endl without the std:: prefix
// function declaration so function can be put after main
void menu(); 
void openSeats(int seats[13][6]);
//int firstClassSeat(int &seats[13][6]);
//int businessClassSeats(int &seats[13][6]);
//int economySeats(int &seats[13][6]);
//static variables to avoid global variables
static int choice;
static int seats[13][6] = { {1, 1, 2, 1, 2, 2},//Row 1
							{1, 2, 1, 2, 1, 2},//Row 2
							{1, 1, 2, 2, 1, 2},//Row 3
							{2, 1, 2, 1, 2, 2},//Row 4
							{1, 2, 1, 2, 1, 1},//Row 5
							{1, 2, 1, 1, 1, 2},//Row 6
							{2, 1, 1, 1, 2, 2},//Row 7
							{1, 2, 1, 2, 2, 1},//Row 8
							{2, 1, 2, 2, 1, 2},//Row 9
							{1, 2, 1, 2, 2, 2},//Row 10
							{1, 1, 2, 1, 2, 1},//Row 11
							{1, 1, 2, 2, 1, 2},//Row 12
							{1, 1, 1, 1, 2, 1} };//Row 13

//menu choices
static const int DISPLAYSEATS = 1, FIRSTCLASS = 2, BUSINESSCLASS = 3, ECONOMY = 4, QUIT = 9;
int main()//heading of the function main, every program must have a main function
{
	//info line
	cout << "This program allows you to pick a seat for your upcoming flight. " << endl;
	cout << endl;

	//start of loop
	do
	{
		menu(); //show menu function
		switch (choice) //switch start
		{
		//cases to execute functions
		case DISPLAYSEATS: 
			openSeats(seats);
			break;

		case FIRSTCLASS:
			//firstClassSeat(seats);
			break;

		case BUSINESSCLASS:
			//businessClassSeats(seats);
			break; 

		case ECONOMY:
			//economySeats(seats);
			break;
		}
	} while (choice != QUIT); //if 8 is entered quits the program
	system("pause"); //pauses the command window to see program running
	return 0; //ends the program
}

void menu()// sets function type as one that is not returning data to the program, only displays the menu choices, all text
{
	cout << endl;
	cout << "Menu Choices: " << endl;
	cout << endl;
	cout << "Press 1 to display all open seats: " << endl;
	cout << "Press 2 to reserve a first class seat: " << endl;
	cout << "Press 3 to reserve a business class seat: " << endl;
	cout << "Press 4 to reserve an economy class seat: " << endl;
	cout << "Press 9 to exit the program: " << endl;
	cout << "Please enter a selection: ";
	cin >> choice; //variable for selection from menu function
}
void openSeats(int seats[13] [6])//shows seat menu
{
	cout << endl << "The current status of the seats on this flight are: . " << endl; 
	
	cout << setw(10) << "A" << setw(5) << "B" << setw(5) << "C" << setw(5) << "D" << setw(5) << "E" << setw(5) << "F" << endl;
	for (size_t row = 0; row < 13; ++row) 
	{
		cout << "Row " << row + 1;
		for (size_t col = 0; col < 6; ++col) 
		{
		if (row < 9)
			cout << setw(5) << (seats[row][col] == 2 ? 'X' : '*');
		else 
			if (col == 0)
				cout << setw(4) << (seats[row][col] == 2 ? 'X' : '*');
			else if(col > 0)
				cout << setw(5) << (seats[row][col] == 2 ? 'X' : '*');
		}
		cout << '\n';
	}
	cout << endl << "Seats available for occupancy are indicated by a *. " << endl;
}
//int firstClassSeat(int &seats[13][6])
//{
//	return seats[13][6];
//}
//int businessClassSeats(int &seats[13][6])
//{
//	return seats[13][6];
//}
//int economySeats(int &seats[13][6])
//{
//	return seats[13][6];
//} 
Last edited on
Array parameter is not a copy. Referencing is not needed. In fact,
1
2
void foo( int gaz[] );
void bar( int* gaz );

Type of gaz in these two functions is identical. Two syntaxes, same thing.

See http://www.cplusplus.com/doc/tutorial/arrays/


PS. Consider cout << "Row " << setw(2) << row + 1;
thank you, I want to change the value of the array within the function, is that possible?
yes. its already reference, so change it in the function and it will change the original.
Topic archived. No new replies allowed.