Swapping array elements using pointer addressing
Oct 13, 2020 at 6:00pm UTC
Hi, I have created a 1d array of 25 elements that I'd like to manipulate using pointers. So far I have the array printing out as if it were a [5][5] 2d array. I would like to swap the first and fourth column of the array. I'd appreciate any help on finding and correcting my mistake.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
void swap_column(char *arr)
{
const int SIZE = 25;
for (size_t i = 0; i < SIZE; i++)
{
char temp_array = *arr;
*arr = *(arr + i);
*(arr + i) = temp_array;
}
cout<< "Columns Swapped!" << endl;
}
output currently looks like:
e w e s t
h e a r t
e g r i t
c l o n e
o d o r s
should look like:
s s w e e
t h e a r
t e g r i
t c l o n
e o d o r
Last edited on Oct 13, 2020 at 6:19pm UTC
Oct 13, 2020 at 6:33pm UTC
*arr is arr[0], which is the first element of the array. So you're always swapping with the first element.
There are 5 elements per column, so you'd only need to swap 5 pairs of elements.
You can access a (row, column) element using
arr[row * 5 + column]
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
// Example program
#include <iostream>
// column number starts from 0. Feel free to add 1.
void swap_columns_5x5(char arr[], int column_A, int column_B)
{
for (size_t row = 0; row < 5; row++)
{
size_t index_A = 5 * row + column_A;
size_t index_B = 5 * row + column_B;
char temp_char = arr[index_A]; // (it's a single char, not an array)
arr[index_A] = arr[index_B];
arr[index_B] = temp_char;
}
}
void print_5x5(char arr[]) // like your other thread
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
std::cout << arr[5 * i + j] << ' ' ;
}
std::cout << '\n' ;
}
}
int main()
{
char arr[] = "abcde12345vwxyzABCDE!@#$%" ;
print_5x5(arr);
std::cout << '\n' ;
swap_columns_5x5(arr, 0, 3);
print_5x5(arr);
}
a b c d e
1 2 3 4 5
v w x y z
A B C D E
! @ # $ %
d b c a e
4 2 3 1 5
y w x v z
D B C A E
$ @ # ! %
Also, instead of
1 2 3
char temp_char = arr[index_A];
arr[index_A] = arr[index_B];
arr[index_B] = temp_char;
you can use std::swap
1 2 3 4 5
#include <algorithm>
// ...
std::swap(arr[index_A], arr[index_B]);
Last edited on Oct 13, 2020 at 6:37pm UTC
Oct 14, 2020 at 10:20am UTC
I would like to swap the first and fourth column of the array
Current:
e w e s t
h e a r t
e g r i t
c l o n e
o d o r s
Required:
s s w e e
t h e a r
t e g r i
t c l o n
e o d o r
The required does not follow from swapping first and forth column. Swapping first and forth column would give:
s w e e t
r e a h t
i g r e t
n l o c e
r d o o s
Does not compute ????
Oct 15, 2020 at 7:53pm UTC
I ended up using two constant variables, one to let the for loop know when the end of a row was, and the other was used to control the swap.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
void swap_column(char *arr)
{
const int SIZE = 25;
const int ROW = 5;
const int COL = 3;
for (size_t i = 0; i<SIZE; i+=ROW)
{
char temp = *(arr + i);
*(arr + i) = *(arr + i+COL);
*(arr + i+ COL) = temp;
}
}
Topic archived. No new replies allowed.