Manipulating 2d arrays need help

I have a project for my c++ class, and in that project I have to read in from a file a number indicating how many matrixis are in the file. Followed by the size of a square matrix, and the matrix. Then I have to pass each array to 8 differnet function that are supposed to manipulate the array. This is where i am having alot of trouble with the logic. Here are the eight things I have to do:
1. Reverse the elements in the Major diagonal (upper left to lower right)
2. Reverse the elements in the Minor diagonal (lower left to upper right)
3. Reverse the elements in Row i ( 0 <= i < number of rows)
4. Reverse the elements in Col j ( 0 <= j < number of columns)
5. Cycle Row i left (i is user inputed row)
6. Cycle Row i right
7. Cycle Col j up (j is user inputed column)
8. Cycle Col j down

ANY HELP WOULD BE GREATLY APPRECIATED!!!!

Here is what I have so far:
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
//This program reads in m# of matrixis from a user named input file
//and manipulates each matrixis 8 different times and prints
//each manipulated matrix to a user named ouput file
#include <iostream>
#include <fstream>

using namespace std;

const int rows=5;
const int cols=5;

void Rev_Major(char A[][cols], ifstream&fin, ofstream&fout);
//void Rev_Minor(char);
//void Rev_Row(char);
//void Rev_Col(char);
///void Cycle_RLeft(char);
//void Cycle_RRight(char);
//void Cycle_Cup(char);
//void Cycle_Cdown(char);

int main()
{
	char A[rows][cols];
	int i,j,m,p, rows=0, cols=0; 
	char  fname[40], oname[40];

	cout << "What is the name of the input file you wish to use?" << endl;
	cout << endl;
	cin >> fname;
	cout << "What is the name of the output file you wish to use?" << endl;
	cout << endl;
	cin >> oname;

	ifstream fin;
	fin.open(fname);
	fin.clear();

	ofstream fout;
	fout.open(oname);
	fout.clear();

	fin >> m;

	
	for (p=0; p<m; p++)
	{
		fin >> rows;
		cols = rows;
	
		for (i=0; i<rows; i++)
		{		
			for (j=0; j<cols; j++)
				fin >> A[i][j];
		}	


		for (i=0; i<rows; i++)
		{				
			for (j=0; j<cols; j++)
				fout << A[i][j];
			fout << endl;
		}
		fout << endl;
		
		Rev_Major(A,fin,fout);

	//Rev_Minor(char A);

	//Rev_Row(char A);

	//Rev_Col(char A);

	//Cycle_RLeft(char A);

	//Cycle_RRight(char A);

	//Cycle_Cup(char A);

	//Cycle_Cdown(char A);
	
	}
	fin.close();
	fout.close();

	return 0;
}

void Rev_Major(char A[][cols], ifstream&fin, ofstream&fout)
{
This looks pretty good so far, except that your declaration of A limits matrices
to 5x5.

You can actually implement the whole thing in 2 manipulation functions, because
reversing diagonals, rows, or columns can be achieved with a single function with a couple parameters and cycling rows and columns can be achieved with a second function.

I'll help you write the reversing functions, then you should be able to write the rest.

Let's think about reversing a simple array of integers. Let's take { 0, 1, 2, 3, 4 } as
the example. I'll number the elements 0 through 4 since C/C++ is zero based.
In the above example, you need to swap the 0 and 4, and then the 1 and 3. The
2 is in the middle, so it doesn't move. Note that if I swap 2 with itself, it doesn't move.

Let's take a second example: { 0, 1, 2, 3, 4, 5 }. Even number of elements this time. Swap the 0 and 5, 1 and 4, then 2 and 3. All elements get swapped because there is no "middle" element.

Your problem is two-dimensional, but the same rules apply.

How do you swap two numbers/elements? You probably covered it in class. First, you make a copy of element #1. Then copy element #2 over element #1. Then copy the temporary over element #2. As an example of swapping two integers:

1
2
3
4
5
void swap( int* a, int* b ) {
   int temp = *a;
   *a = *b;
   *b = temp;
}


(Note, there are at least three better ways to do this, but I don't know if you've
covered references, templates, or the STL in your class. Let me know.)

Ok. Reversing major diagonal. The major diagonal has coordinates (0,0), (1,1),
(2,2) ... (n,n). So:

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
// Handy little function to swap two elements.  Again, lots of better ways to do
// this, but I'm giving you the way you were probably taught.
void Swap( char* a, char* b ) {
  char temp = *a;
  *a = *b;
  *b = temp;
}

// Note: you'll need to pass in the actual size of the matrix to the function.
//         Also, not sure you need the input file since you're already read it.
void Rev_Major( char A[][cols], int size, ofstream& fout ) {
  // We want to swap element (0,0) with element (size-1,size-1)
  // We want to swap element (1,1) with element (size-2,size-2), etc.
  // Stop when we've gotten halfway through, because we've already
  // swapped all the elements.  If we kept going, we'd swap everything
  // right back into original position!
  for( int p = 0; p < size/2; ++p ) 
     Swap( &A[p][p], &A[size-p-1][size-p-1] );
}

// Reversing minor diagonal is the same algorithm, just different elements.
void Rev_Minor( char A[][cols], int size, ofstream& fout ) {
  // We want to swap element (size-1,0) with element (0,size-1)
  // We want to swap element (size-2,1) with element (1,size-2), etc.
  // Stop when we've gotten halfway through, because we've already
  // swapped all the elements.  If we kept going, we'd swap everything
  // right back into original position!
  for( int p = 0; p < size/2; ++p ) 
     Swap( &A[size-p-1][p], &A[p][size-p-1] );
}


// Reversing columns is easy.  Just pass in the column#.
void Rev_Col( char A[][cols], int size, int column, ofstream& fout ) {
  // We want to swap element (column,0) with element (column,size-1)
  // We want to swap element (column,1) with element (column,size-2), etc.
  // Stop when we've gotten halfway through, because we've already
  // swapped all the elements.  If we kept going, we'd swap everything
  // right back into original position!
  for( int p = 0; p < size/2; ++p ) 
     Swap( &A[column][p], &A[column][size-1-p] );
}


// Reversing rows is just as easy.  Just pass in the row#.
void Rev_Col( char A[][cols], int size, int row, ofstream& fout ) {
  // We want to swap element (0,row) with element (size-1,row)
  // We want to swap element (1,row) with element (size-2,row), etc.
  // Stop when we've gotten halfway through, because we've already
  // swapped all the elements.  If we kept going, we'd swap everything
  // right back into original position!
  for( int p = 0; p < size/2; ++p ) 
     Swap( &A[p][row], &A[size-1-p][row] );
}


[Note: if you've covered any of the STL algorithms, swap() is already provided for you if you #include <algorithm>, which means you wouldn't need to write Swap() and you'd just replace the calls to (upper-case) Swap() with (lower-case) swap().]
Topic archived. No new replies allowed.