I have a multidimensional array with 3 rows and 4 columns. The program should use reverseRow()
function to reverse a specific row from an array. Like, let's say user's input is 2, then it should reverse second row and print it.
I have tried a swap method, but it didn't work. I also tried using pointers, but it didn't work as well. Can someone explain it for me? How do I reverse a specific row?
#include <stdlib.h>
int arr[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}};
int n = sizeof(arr) / sizeof(arr[0]);
void reverseRow(int low, int high)
{
if (low < high)
{
int temp = arr[low][0];
arr[low][0] = arr[high][0];
arr[high][0] = temp;
reverseRow(low 1, high - 1);
for (int i = 0; i < n; i )
for (int j = 3; i > 0; j--)
printf("%d ", arr[i][j]);
}
}
void printMenu()
{
printf("\n");
printf("You can choose one of these services: \n");
printf("1. Get the elements of a specific row reversed \n");
printf("Please select one to try ");
int answer;
scanf("%d", &answer);
switch (answer)
{
case 1:
reverseRow(0, n - 1);
break;
case 2:
printf("Bye!\n");
break;
default:
printf("please select carefully! \n");
break;
}
}
int main()
{
printMenu();
return 0;
}
Best regards.
CodePudding user response:
You're reversing the first column, not a user-selected row.
You're not passing the row number to the function.
The loop that prints the array is printing all the columns in reverse order, and it's using n
as the number of rows, not columns. I've renamed the variables to be clearer and fixed the printing loop.
#include <stdio.h>
#include <stdlib.h>
int arr[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}};
int rows = sizeof(arr) / sizeof(arr[0]);
int cols = sizeof(arr[0]) / sizeof(arr[0][0]);
void reverseRow(int rownum, int low, int high)
{
if (low < high)
{
int temp = arr[rownum][low];
arr[rownum][low] = arr[rownum][high];
arr[rownum][high] = temp;
reverseRow(rownum, low 1, high - 1);
} else {
for (int i = 0; i < rows; i ) {
for (int j = 0; j < cols; j ) {
printf("%d ", arr[i][j]);
}
printf("\n");
}
}
}
void printMenu()
{
printf("\n");
printf("You can choose one of these services: \n");
printf("1. Get the elements of a specific row reversed \n");
printf("Please select one to try ");
int answer;
scanf("%d", &answer);
switch (answer)
{
case 1:
printf("Please select row number: ");
int row;
scanf("%d", &row);
if (row >= rows || row < 0) {
printf("Invalid row\n");
break;
}
reverseRow(row, 0, cols - 1);
break;
case 2:
printf("Bye!\n");
break;
default:
printf("please select carefully! \n");
break;
}
}
int main()
{
printMenu();
return 0;
}
CodePudding user response:
What is the row of a two-dimensional array?
It is a one dimensional array.
So what you need is to write a function that reverses a one-dimensional array.
An iterative function can look the following way
void reverseRow( int *a, size_t n )
{
for ( size_t i = 0; i < n / 2; i )
{
int tmp = a[i];
a[i] = a[n-i-1];
a[n-i-1] = tmp;
}
}
A recursive function can look the following way
void reverseRow( int *a, size_t n )
{
if ( !( n < 2 ) )
{
int tmp = a[0];
a[0] = a[n-1];
a[n-1] = tmp;
reverseRow( a 1, n - 2 );
}
}
And either function is called like for example
reverseRow( arr[1], 4 );
that reverses the second row of the original array.
Or if the number of row (starting from 0) is stored in some variable as for example row then the function is called like
reverseRow( arr[row], 4 );
To output the reversed row you need to write a separate function.
Here is a demonstration program.
#include <stdio.h>
void reverseRowIterative( int *a, size_t n )
{
for (size_t i = 0; i < n / 2; i )
{
int tmp = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = tmp;
}
}
void reverseRowRecursive( int *a, size_t n )
{
if (!( n < 2 ))
{
int tmp = a[0];
a[0] = a[n - 1];
a[n - 1] = tmp;
reverseRowRecursive( a 1, n - 2 );
}
}
void printRow( const int *a, size_t n )
{
for (size_t i = 0; i < n; i )
{
printf( "- ", a[i] );
}
putchar( '\n' );
}
int main( void )
{
enum { M = 3, N = 4 };
int arr[M][N] =
{
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 }
};
for ( size_t i = 0; i < M; i )
{
reverseRowIterative( arr[i], N );
printRow( arr[i], N );
}
putchar( '\n' );
for (size_t i = 0; i < M; i )
{
reverseRowRecursive( arr[i], N );
printRow( arr[i], N );
}
putchar( '\n' );
}
The program output is
4 3 2 1
8 7 6 5
12 11 10 9
1 2 3 4
5 6 7 8
9 10 11 12