Arrays to functions

Alright so I'm trying to send an array to a function but I only want to mess with certain values for certain rows or columns. I kind of wrote a quick code so kinda show what i'm getting after. I hope this makes sense to someone and thanks for your help in advance.

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
#include <iostream>
#include <iomanip>
#include <conio.h>

using namespace std;


int functionname(int[4][4], int)


	int numbers[4][4] = {
							{23, 0, 56, 2}, 
							{10, 23, 45, 6},
							{8, 10, 15, 3},
							{2, 19, 1, 6}
						};
	


int main()
{

	//now i want to call my array
	
	cout << functionname(numbers, 16)
	
	
}

int funtionname(numbers [4][4], int sixteen)

//here in my function what do I have to do to only mess with certain rows or columns?

//for example if i want to add all of row[2] do I have to put rowtot = row[2][0] + row [2][1] + row [2][2] + row [2][3] ?? 
your function prototype needs to match your function definition.
you should initialize your array outside your function.
you can have pass a flag to check that you want to modify column or row.

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
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <algorithm>
#include <vector>
#include <iomanip>

using namespace std;

int funtionname(int numbers[4][4], int sixteen, bool flag);

int main(int argc, char **argv)
{
	int numbers[4][4] = {{ 23, 0, 56, 2 },
		        		 { 10, 23, 45, 6 },
						 { 8, 10, 15, 3 },
						 { 2, 19, 1, 6 }};

	funtionname(numbers, 16, true);
		
	int x;
	cin >> x;

	return 0;
}

int funtionname(int numbers[4][4], int sixteen, bool flag)
{
  if (flag == true)
 {
     // process row
     std::cout << "processing row" << std::endl;
 }
 else
 {
     // process column
     std::cout << "processing column << std::endl;
 }

	// do your processing here
	for (int rows = 0; rows < 4; rows++)
	{
		for (int columns = 0; columns < 4; columns++)
		{
			std::cout << numbers[rows][columns] << " ";
		}
		std::cout << endl;
	}

	return 0;
} 
Last edited on
rafae11 thanks for your help! I thought I replied and closed this question a while ago. Thanks again for your help.
Topic archived. No new replies allowed.