Swapping numbers in a multidimensional array

Click For Summary
SUMMARY

The discussion focuses on the issue of swapping numbers in a multidimensional array using C++. The provided code attempts to swap the values 43 and 435 in a 2x2 matrix but fails due to the swap function inadvertently swapping elements twice. The solution involves modifying the swap function to only target elements above or below the main diagonal, thereby ensuring that each element is swapped only once. This approach is further clarified with an example of a larger 3x3 matrix.

PREREQUISITES
  • Understanding of C++ syntax and structure
  • Familiarity with multidimensional arrays in C++
  • Knowledge of matrix operations, specifically element swapping
  • Basic grasp of loops and conditional statements in programming
NEXT STEPS
  • Implement a swap function for larger matrices, focusing on entries below the main diagonal.
  • Explore C++ STL (Standard Template Library) for more efficient matrix manipulation.
  • Learn about matrix transposition and its applications in algorithms.
  • Investigate debugging techniques in C++ to identify and resolve logical errors in code.
USEFUL FOR

Programmers, computer science students, and software developers looking to enhance their skills in matrix manipulation and debugging in C++.

magnifik
Messages
350
Reaction score
0
i'm trying to swap the 43 and the 435, but instead 435 is printed where 43 should be.

Code:
#include <iostream>
using namespace std;

void print(const int matrix[][2]);
void swap(int matrix[][2]);

int main(){
	int matrix[2][2] = {{14, 435}, {43, 65}};

	print(matrix);
	cout << endl;
	swap(matrix);
}

void print(const int matrix[][2]){
	for (int i = 0; i < 2; i++){
		for (int j = 0; j < 2; j++){
			cout << matrix[i][j] << " ";
		}
		cout << endl;
	}
}

void swap(int matrix[][2]){
	for (int r = 0; r < 2; r++){
		for (int c = 0; c < 2; c++){
			int temp = matrix[r][c];
			matrix[r][c] = matrix[c][r];
			matrix[c][r]= temp;
			cout << matrix[c][r] << " ";
		}
		cout << endl;
	}
}
 
Physics news on Phys.org
The problem is that your swap function is swapping each element in your matrix twice, which is the same as not swapping anything at all.

When r = 0 and c = 1, 435 is swapped for 43 and vice versa. But when r = 1 and c = 0, the 435 and 43 are swapped again. What you need to do is cycle through the matrix entries above the main diagonal or below the main diagonal (one or the other). So for this problem all you need to do is swap the entry at matrix[0][1] and you're done.

It's probably easier to see if you have a larger matrix, say 3 x 3. If you work on the entries below the main diagonal, you want to work on the entries at matrix[1][0], matrix[2][0], and matrix[2][1]. You'll need to figure out how to have your two loops do this.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 23 ·
Replies
23
Views
9K