Question about how to pass a 2d array in to a function

hi i was wondering how would i pass a 2d array in to a function like bubble sort. when i pass it in as a normal array it works fine as you would see my comment out section below but once i swap to the 2d array it just plain old don't do shit.I was wondering if i can find some help here and find out what i am doing wrong. and yes if you are wondering about the quck sort merge sort and selection sort I took out out since the preview made the post look mad big


thanks for any one that can help me 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
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
#include <iostream>
#include <ctime>
using namespace std;


void bubble_sort(int *,int);//function prototype

void PrintArray(int *,int);// function prototype

const int size = 10;// size of array
const int length= 5;// number of arrays
//int arr[size];
int main()
{
int arr1[length][size];//creates mutidementional array

srand ( time(NULL) );// seeds for random number genarator
for (int cl=0;cl<length;cl++)
{
	for (int wl=0;wl<size;wl++)
	{
	
	arr1[cl][wl]= rand()%100000+1;// assigns random numbers to all the variables
	}
}

//prints out the array  
for (int cl1=0;cl1<length;cl1++)
{
	int numb=cl1;
	cout<<"array "<<numb+1<<endl;
	for (int wl1=0;wl1<size;wl1++)
	{
	cout<<arr1[cl1][wl1]<<'	';
	}
	cout<<endl;
}

// this is the part 
for (int iterate=0;iterate<length;iterate++)
	{
		bubble_sort(arr1[iterate],size);
	}


	/*
	//part one to test for correctness
	// inputs random numbers in to array
	
	for(int input=0; input<size; input++)
	{
		srand ( time(NULL) );
		arr[input]= rand() % 100000 +1;
	}
	
	int arr1[size];
	int arr2[size];
	int arr3[size];
	int arr4[size];
	//coppy function to a new array so it stay unsorted before each sorting call
	for(int copypoint=0; copypoint<size; copypoint++)
	{
		
		 arr1[copypoint] = rand() % 100000 +1;
		 
		 arr2[copypoint] = rand() % 100000 +1;
	
		 arr3[copypoint] = rand() % 100000 +1;
		 
		 arr4[copypoint] = rand() % 100000 +1;
	}
	
	// Merge Sort
	//print out array
	cout<<"This the the merg sort"<<endl;
	PrintArray(arr1,size);
    for (int i = 1; i < size; i *= 2) {
        for (int j = 0; j < size - i; j += 2*i) {
            int iEnd2 = (2*i < size - j) ? 2*i : size - j;
            Merge(&(arr1[j]), i, iEnd2);
        }
    }
	//print out merg sorted array
	PrintArray(arr1,size);

	//cout<<endl<<endl;
	

	// call quick sort
	cout<<"This the Quick sort"<<endl;
	//print out array
	PrintArray(arr2,size);
	QuickSort(arr2,0,size - 1);
	//print quick sorted array
	PrintArray(arr2,size);

	//call bubble sort
	cout<<"This the bubble sort"<<endl;
	//print out array
	PrintArray(arr3,size);
	bubble_sort(arr3,size);
	//prints bubble sorted array
	PrintArray(arr3,size);

	//call selection sort
	cout<<"This calls bubble sort"<<endl;
	//prints our a array
	PrintArray(arr4,size);
	//sorts a array
	selectionSort(arr4,size);
	//prints sorted array
	PrintArray(arr4,size);
	*/
}



void swap(int &a, int &b) // swaps 2 numbers 
{
	int temp;
	temp = a;
	a = b;
	b = temp;
}

void PrintArray(int* arr)// prints out a array
{
	
	 //replace it with test
	for(int i = 0; i < size; i++)
	{
		cout<<arr[i]<<'\t';
	}


void bubble_sort(int arry[],int s)// function to do the bubble sort
{
		
	for(int j=0; j >=s ; s-- )// outer loop is to make it so after each pass and the last number is sorted strink the amout of
									//checking / comparesons needed for the second  iteration if the loop
			{
			for(int i = 0; i < j ; i++)//inner loop to go throught the array
				{

					if(arry[i] > (arry[i+1]))// compares every concetive pair with in the array 
					{
						int temp;//temp to store the number temporally to swap
						temp = arry[i];//swap stucture 
						arry[i] = arry[i+1];//swap stucture
					    arry[i+1] = temp;//swap structure 
						
					}
																				
				}
			}
}
http://www.cplusplus.com/forum/articles/17108/
explains both how to use 2d arrays and what are the alternatives.

see if this helps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void afunc(char arr[][10])
{
	std::cout << arr[0] << std::endl;
	std::cout << arr[1] << std::endl;
}

void afunc2(char (*arr)[10])
{
	std::cout << arr[0] << std::endl;
	std::cout << arr[1] << std::endl;
}

int main()
{

	char arr[2][10] = {"sunday", "monday"};
	afunc(arr);
	afunc2(arr);
}

Topic archived. No new replies allowed.