Swapping numbers in a multidimensional array

In summary, the issue is that the swap function is swapping each element twice, resulting in no change. To fix this, the function should only swap the elements above or below the main diagonal, depending on which is being targeted. This can be done by modifying the loops to only swap specific elements.
  • #1
magnifik
360
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
  • #2
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.
 

What is a multidimensional array?

A multidimensional array is an array that contains other arrays as its elements. This allows for a table-like structure with rows and columns, where each element is referenced by its position in the array.

How do you swap numbers in a multidimensional array?

To swap numbers in a multidimensional array, you need to first identify the two elements that you want to swap. Then, you can use a temporary variable to store one of the elements while you reassign the other element. Finally, you can use the temporary variable to assign the new value to the other element.

Can you swap numbers in a multidimensional array without using a temporary variable?

Yes, it is possible to swap numbers in a multidimensional array without using a temporary variable. This can be achieved by using mathematical operations such as addition and subtraction to reassign the values of the two elements.

Why would you need to swap numbers in a multidimensional array?

Swapping numbers in a multidimensional array can be useful in various scenarios, such as sorting algorithms or when rearranging data in a table. It allows for efficient manipulation and organization of data within the array.

Are there any challenges to swapping numbers in a multidimensional array?

Yes, there can be challenges to swapping numbers in a multidimensional array. One challenge is ensuring that the correct elements are being swapped, as it can be easy to mix up the rows and columns in a multidimensional array. Additionally, it is important to consider the efficiency and performance of the swapping algorithm when dealing with large arrays.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
756
  • Engineering and Comp Sci Homework Help
Replies
8
Views
842
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
24
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
947
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
Back
Top