Passing an Array to Function

Hi,

The issue I'm having is to simply print a table of a 3x3 array when the function is called.

I made a function called 'void display_table', and I'm trying to call it on line 63 by giving the name of the array, 'a', and the size of the array. This is where I may be running into at least 1 problem. I thought the "size" of the array is 3x3, so I was trying to input 'rows' and 'columns', but that doesn't seem to work.

Any ideas?


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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Examples of things to do with Multiple-Subscripted Arrays, referancing p.273

#include <iostream>
#include <cstdlib>
#include <array>
#include <iomanip>

using namespace std;

// display table function
void display_table(int array_name[], int array_size_rows, int array_size_columns)
{
cout << "\n" << endl;
cout << "Show full table: " << "\n" << endl;
int i, j, rows, columns;

	for (i = 0; i < rows; i++)
	{
		for (j = 0; j < columns; j++)
		{
			cout << array_name[i][j] << " ";
		}
	cout << endl;
	}
}

int main()
{

//create a 3x3 table
const int rows = 3;
const int columns = 3;
int a[rows][columns];// = { {} , {}, {} }; create a 3x3 table, initalize all data to 0, in all rows & columns (although don't have to if overwriting it

// enter numbers into the data
//int array_modification(int a, int 2)
int i, j;
int user_choice;   // user inputs a row to display
int enter_data;
bool change = true;

// edit element varaibles
char edit_element, display_table;
int edit_row;
int edit_column;
int new_value;

cout << "Table is: " << rows << " x " << columns << endl;

// Populate data into Table
for (int i = 0; i < rows; i++)
{
	//cout << "Enter data into Row " << i + 1;

	for (int j = 0; j < columns; j++)
	{
		cout << "\nEnter data into Row " << i + 1 << ", Column " << j + 1 << " : ";
		cin >> enter_data;
		a[i][j] = enter_data;       // place data into cell
	}
}
// Display Table
display_table(a, rows, columns)

/*
// Display Table
cout << "\n" << endl;
cout << "Show full table: " << "\n" << endl;

for (i = 0; i < rows; i++)
{
	for (j = 0; j < columns; j++)
	{
		cout << a[i][j] << " ";
	}
cout << endl;
}
*/

// Modify elements in table
while (change == true)     // default set to true so this will code will run
{

	cout << "\nDo you want to modify any elements in the table? (y/n): ";
	cin >> edit_element;

	if (edit_element == 'y')
	{
		// ask user to enter which element they want to change
		cout << "\nEnter row #: ";
		cin  >> edit_row;

		cout << "Enter column #: ";
		cin  >> edit_column;

		cout << "Enter new value: ";
		cin >> new_value;

		a[edit_row - 1][edit_column - 1] = new_value;
	}

		
	else
	{
		break;
	}
		
	cout << "Display Table? (y/n): ";
	cin >> display_table;

	if (display_table == 'y')
		{ // replace with call function which shows table
			// Display Table
			cout << "\n" << endl;
			cout << "Show full table: " << "\n" << endl;

			for (i = 0; i < rows; i++)
			{
				for (j = 0; j < columns; j++)
				{
					cout << a[i][j] << " ";
				}
			cout << endl;
            }
		}

	else
		{// empty, loop back to top
		}

}
// Show a row, chosen by the user
cout << "\n" << endl;
cout << "Enter a row number to show (1-" << rows << "): "; 
cin >> user_choice;

for (j = 0; j < columns; j++)
{
	cout << a[user_choice-1][j] << " ";   // where a row_choice of 1 will equal 0 in the computer
}
 
// Show a column, chosen by the user
cout << "\n" << endl;
cout << "Enter a column number to show (1-" << columns << "): "; 
cin >> user_choice;

for (i = 0; i < rows; i++)
{
	cout << a[i][user_choice-1] << endl;   // where a row_choice of 1 will equal 0 in the computer.
}										   // hold the column number constant, while increasing row number

// Sum a column, chosen by the user
cout << "\n" << endl;
cout << "Enter a column to sum (1-" << columns << "): "; 
cin >> user_choice;
int column_sum = 0; // reset to 0

for (i = 0; i < rows; i++)
{
	column_sum = column_sum + a[i][user_choice-1];   // where a row_choice of 1 will equal 0 in the computer
} 

cout << "\nSum of Column " << user_choice << ": " << column_sum;


// Sum a row, chosen by the user
cout << "\n" << endl;
cout << "Enter a row to sum (1-" << rows << "): "; 
cin >> user_choice;
int row_sum = 0; // reset to 0

for (j = 0; j < columns; j++)
{
	row_sum = row_sum + a[user_choice-1][j];   // where a row_choice of 1 will equal 0 in the computer
} 

cout << "\nSum of Row " << user_choice << ": " << row_sum << endl;

cin.get();
cin.ignore();

return 0;
}

You must declare array size at the point of creation.
1
2
3
4
5
6
int main()
{
    const int x = 5, y = 5;
    int arr[x][y];
    return 0;
}


Will compile (although I wouldn't recommend it). Array sizes are expected to be known and since your array size is known, why on Earth are you not just declaring it properly?
1
2
3
4
5
int main()
{
   int arr[3][3]; 
   return 0;
}


That doesn't seem like it will help my problem. My program actually does not compile. I get 3 errors given, with the first saying that "subscript requires array or pointer type in line 21", which is referring the 'j'. Not sure how to correct this issue.

closed account (D80DSL3A)
Try this for the function prototype.
void display_table(int array_name[][3], int array_size_rows);
The array passed to this function must have 3 columns, though it can have any number of rows.
The easiest way to handle this is to use a typedef for your array type:

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
const int array_type_rows = 3;
const int array_type_columns = 3;
typedef int array_type[ array_type_rows ][ array_type_columns ];

void print_array( array_type a )
{
  for (int i = 0; i < array_type_rows; i++)
  {
    for (int j = 0; j < array_type_columns; j++)
    {
      cout << a[ i ][ j ] << " ";
    }
    cout << endl;
  }
}

int main()
{
  array_type xs; 

  ...

  print_array( xs );

}

Hope this helps.
fun2code - I took your example (as it was the least amount of code modification), and put it in on line 12. It seems to now only give me 1 error:

Line 64: "term does not evaluate to a function taking 2 arguments"

I'm stumped. Any ideas what's wrong and how to fix it? It looks ok to 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Examples of things to do with Multiple-Subscripted Arrays, referancing p.273

#include <iostream>
#include <cstdlib>
#include <array>
#include <iomanip>

using namespace std;

// display table function
void display_table(int array_name[][3], int array_size_rows) // void display_table(int array_name[], int array_size_rowsint array_size_columns)
{
cout << "\n" << endl;
cout << "Show full table: " << "\n" << endl;
int i, j, rows, columns;

	for (i = 0; i < rows; i++)
	{
		for (j = 0; j < columns; j++)
		{
			cout << array_name[i][j] << " ";
		}
	cout << endl;
	}
}

int main()
{

//create a 3x3 table
const int rows = 3;
const int columns = 3;
int a[rows][columns];// = { {} , {}, {} }; create a 3x3 table, initalize all data to 0, in all rows & columns (although don't have to if overwriting it

// enter numbers into the data
//int array_modification(int a, int 2)
int i, j;
int user_choice;   // user inputs a row to display
int enter_data;
bool change = true;

// edit element varaibles
char edit_element, display_table;
int edit_row;
int edit_column;
int new_value;

cout << "Table is: " << rows << " x " << columns << endl;

// Populate data into Table
for (int i = 0; i < rows; i++)
{
	//cout << "Enter data into Row " << i + 1;

	for (int j = 0; j < columns; j++)
	{
		cout << "\nEnter data into Row " << i + 1 << ", Column " << j + 1 << " : ";
		cin >> enter_data;
		a[i][j] = enter_data;       // place data into cell
	}
}
// Display Table
//display_table(a, rows, columns)
display_table(a, rows);
/*
// Display Table
cout << "\n" << endl;
cout << "Show full table: " << "\n" << endl;

for (i = 0; i < rows; i++)
{
	for (j = 0; j < columns; j++)
	{
		cout << a[i][j] << " ";
	}
cout << endl;
}
*/

// Modify elements in table
while (change == true)     // default set to true so this will code will run
{

	cout << "\nDo you want to modify any elements in the table? (y/n): ";
	cin >> edit_element;

	if (edit_element == 'y')
	{
		// ask user to enter which element they want to change
		cout << "\nEnter row #: ";
		cin  >> edit_row;

		cout << "Enter column #: ";
		cin  >> edit_column;

		cout << "Enter new value: ";
		cin >> new_value;

		a[edit_row - 1][edit_column - 1] = new_value;
	}

		
	else
	{
		break;
	}
		
	cout << "Display Table? (y/n): ";
	cin >> display_table;

	if (display_table == 'y')
		{ // replace with call function which shows table
			// Display Table
			cout << "\n" << endl;
			cout << "Show full table: " << "\n" << endl;

			for (i = 0; i < rows; i++)
			{
				for (j = 0; j < columns; j++)
				{
					cout << a[i][j] << " ";
				}
			cout << endl;
            }
		}

	else
		{// empty, loop back to top
		}

}
// Show a row, chosen by the user
cout << "\n" << endl;
cout << "Enter a row number to show (1-" << rows << "): "; 
cin >> user_choice;

for (j = 0; j < columns; j++)
{
	cout << a[user_choice-1][j] << " ";   // where a row_choice of 1 will equal 0 in the computer
}
 
// Show a column, chosen by the user
cout << "\n" << endl;
cout << "Enter a column number to show (1-" << columns << "): "; 
cin >> user_choice;

for (i = 0; i < rows; i++)
{
	cout << a[i][user_choice-1] << endl;   // where a row_choice of 1 will equal 0 in the computer.
}										   // hold the column number constant, while increasing row number

// Sum a column, chosen by the user
cout << "\n" << endl;
cout << "Enter a column to sum (1-" << columns << "): "; 
cin >> user_choice;
int column_sum = 0; // reset to 0

for (i = 0; i < rows; i++)
{
	column_sum = column_sum + a[i][user_choice-1];   // where a row_choice of 1 will equal 0 in the computer
} 

cout << "\nSum of Column " << user_choice << ": " << column_sum;


// Sum a row, chosen by the user
cout << "\n" << endl;
cout << "Enter a row to sum (1-" << rows << "): "; 
cin >> user_choice;
int row_sum = 0; // reset to 0

for (j = 0; j < columns; j++)
{
	row_sum = row_sum + a[user_choice-1][j];   // where a row_choice of 1 will equal 0 in the computer
} 

cout << "\nSum of Row " << user_choice << ": " << row_sum << endl;

cin.get();
cin.ignore();

return 0;
}
closed account (D80DSL3A)
Line 43. A char variable declared with the same name as the function. Try changing one of the names to resolve this naming conflict.
Thank you everyone - I got it working now!
Topic archived. No new replies allowed.