Right rotation for 2D array - arrays

I tried to code a right rotation for a 2D array.
Here is the main code:
for (int k = 0; k < rotate; k++) { //rotate is the no of times to shift right
for (int i = 0; i < n1; i++) { //n1 is no: of rows
for (int j = 0; j <n2; j++) { //n2 is no: of columns
//Shifting right
int temp = A[i][n2 - 1];
A[i][n2 - 1] = A[i][j + 1];
A[i][j + 1] = A[i][j];
A[i][j] = temp;
}
}
}
for (int i = 0; i < n1; i++) {
for (int j = 0; j < n2; j++) {
printf("%d", A[i][j]);
}
printf("\n");
}
It is working for size 2x2 where:
Input:
1 2
3 4
Output:
2 1
4 3
But it's not working for size 3x3 and above.
Input:
1 2 3
4 5 6
7 8 9
Output:
3 2 1
6 5 4
9 8 7
Where expected output is:
3 1 2
6 4 5
9 7 8
Please guide me about where I'm wrong and I apologize for any mistakes in my question.

See:
https://www.programiz.com/c-programming/examples/matrix-transpose ;)
You can change that solution to use one array.

In your code you are referring to both left and right neighbours (though left one is wrongly referred because it should be last cell only for first interation) and don't keep value for next iteration.
It should be implemented as follows:
For each row left is initalized with the value of very last item in the row, because it is on the left of 0th item. Then while iterating over row items we first save current value to temp to use it later, then save left to current item, and then use previosly saved temp as new left for next iteration.
for (int k = 0; k < rotate; k++) { //rotate is the no of times to shift right
for (int i = 0; i < n1; i++) { //n1 is no: of rows
int left = A[i][n2 - 1];
for (int j = 0; j < n2; j++) { //n2 is no: of columns
//Shifting right
int temp = A[i][j];
A[i][j] = left;
left = temp;
}
}
}

Related

Printing A Pattern in C

I am a beginner in programming, I studied all about C, I started to solve problems from hackerrank.com, there I faced a problem to print a pattern like given below
(the output of problem program):
4 4 4 4 4 4 4
4 3 3 3 3 3 4
4 3 2 2 2 3 4
4 3 2 1 2 3 4
4 3 2 2 2 3 4
4 3 3 3 3 3 4
4 4 4 4 4 4 4
the input will be an integer which will provide the data for the length of pattern square, here it is 4 in image,
I tried a lot to type a proper logic and I end up with this useless code bellow:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main()
{
int n;
scanf("%d", &n);
int array[n- 1 + n][n - 1 + n];
array[(n - 1 + n) / 2][(n - 1 + n) / 2] = 1;
int f[n];
for(int i = 0; i < n; i++) {
f[i] = i;
}
for(int i = 0; i < n - 1 + n; i++) {
for(int j = 0; j < n - 1 + n; j++) {
array[j][i] = n - f[j]; //top
array[i][j] = n - f[j]; //left
array[(2 * n - 1 - 1) - i][j] = n - f[i]; //bottem
array[j][(2 * n - 1 - 1) - i] = n - f[i]; //rigth
}
}
for(int i = 0; i < n - 1 + n; i++) {
for(int j = 0; j < n - 1 + n; j++) {
printf("%d ", array[i][j]);
}
printf("\n");
}
return 0;
}
my logic was to make all four borders correct in for loop which will end at center, but its not working, I want a new logic or to improve my logic, if you want to help me out then please give me the way to solve problem instead of giving me a direct code.
It is observable that the pattern consists of n stacked squares:
Square #0 is drawn with ns.
Square #1 is drawn with n-1s.
...
Square #n-1 is drawn with 1s.
Implementing the above:
void draw_pattern(const size_t n)
{
const size_t dim = 2*n-1;
int array[dim][dim];
for (size_t i = 0; i < n; ++i) { // Outer square #i
// Row #0 of the outer square #i
for (size_t j = i; j < dim-i; ++j)
array[i][j] = n-i;
// Row #n-1 of the outer square #i
for (size_t j = i; j < dim-i; ++j)
array[dim-i-1][j] = n-i;
// Col #0 of the outer square #i
for (size_t j = i; j < dim-i; ++j)
array[j][i] = n-i;
// Col #n-1 of the outer square #i
for (size_t j = i; j < dim-i; ++j)
array[j][dim-i-1] = n-i;
}
print_array(dim, array);
}
This is print_array():
void print_array(const size_t dim, int array[dim][dim])
{
for (size_t i = 0; i < dim; ++i) {
for(size_t j = 0; j < dim; ++j)
printf("%d ", array[i][j]);
printf("\n");
}
}
Output:
4 4 4 4 4 4 4
4 3 3 3 3 3 4
4 3 2 2 2 3 4
4 3 2 1 2 3 4
4 3 2 2 2 3 4
4 3 3 3 3 3 4
4 4 4 4 4 4 4
The worst case time complexity is O(n2).
When you get a problem like this, try to dumb it down as much a possible. This square can be separated into 8 same, just rotated "slices" that look like:
4 | 4444 | 4444 | 4
43 | 333 | 333 | 34
432 | 22 | 22 | 234
4321 | 1 | 1 | 1234
... and the same for the bottom half, just flipped.
You can see this in the code bellow, to check what line is writing what part of the square, comment it and you will see what section shows zeroes.
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n;
scanf("%d", &n);
int array[2 * n - 1][2 * n - 1];
for(int i = 0; i < 2 * n - 1; i++){
for(int j = 0; j < 2 * n - 1; j++){
array[i][j] = 0;
}
}
int f[n];
for(int i = 0; i < n; i++)
{
f[i] = i;
}
for(int i = 0; i < n; i++)
{
for(int j = i; j < n; j++)
{
array[i][j] = n - i;
array[j][i] = n - i;//top left
array[j][2*n - i - 2] = n - i;
array[i][2*n - j - 2] = n - i;//bottom left
array[2*n - j - 2][i] = n - i;
array[2*n - i - 2][j] = n - i;//top right
array[2*n - i - 2][2*n - j - 2] = n - i;
array[2*n - j - 2][2*n - i - 2] = n - i;//bottom right
}
}
for(int i = 0; i < n - 1 + n; i++)
{
for(int j = 0; j < n - 1 + n; j++)
{
printf("%d ", array[i][j]);
}
printf("\n");
}
return 0;
}

Sorting a 2D Array c++

I am bubble sorting a 2D array that looks like this. I am confuse on how to make my largest value as 1 and make the 2nd row's value follow to 1st row's counterpart.
Input:
13 9 1 8 5
1 2 3 4 1
Actual output:
1 5 8 9 13
1 2 3 4 1
This is the expected output that i am trying to make.
Output:
5 8 9 13 1
1 4 2 1 1
Here is my code for sorting the cards (col = 5 and row = 2):
void sortedCards(int card[][col])
{
int i, j, k, temp;
printf("\n\nSorted Cards\n");
for (k = 0; k < 10; k++)
{
for (i = 0; i < row - 1; i++)
{
for (j = 0; j < col - 1; j++)
{
if (card[i][j] > card[i][j + 1])
{
temp = card[i][j];
card[i][j] = card[i][j + 1];
card[i][j + 1] = temp;
}
}
}
}
for (i = 0; i < row; i++)
{
if (i == 1)
{
printf("\n");
}
for (j = 0; j < col; j++)
{
printf("%i ", card[i][j]);
}
}
}
If your sorting is only dependent on the first row, there is no need to iterate through the second row. Just set both rows at the same time while checking the first row.
Also, if you want 1 to be treated as larger than all other numbers, you need to add that to your Boolean logic. Adjusting your for loop like below should do it.
int j, k, temp, temp2;
for (k = 0; k < 10; k++)
{
for (j = 0; j < col-1; j++)
{
//here we only test row 0, and we check if the value is 1
if (card[0][j] == 1 || (card[0][j] > card[0][j+1] && card[0][j+1] != 1))
{
//all other reassignment is the same but you do both rows at the same time
temp = card[0][j];
temp2 = card[1][j];
card[0][j] = card[0][j + 1];
card[1][j] = card[1][j + 1];
card[0][j + 1] = temp;
card[1][j + 1] = temp2;
}
}
}

2d-arrays in bubble sorting

I'm trying to create a 2d-array in bubble sort, arranged 25 numbers 5 by 5 in ascending order
my inputs
Enter 25 integers:
Input No.[0][0]: 4
Input No.[0][1]: 5
Input No.[0][2]: 8
Input No.[0][3]: 9
Input No.[0][4]: 4
Input No.[1][0]: 2
Input No.[1][1]: 1
Input No.[1][2]: 0
Input No.[1][3]: 2
Input No.[1][4]: 4
Input No.[2][0]: 6
Input No.[2][1]: 7
Input No.[2][2]: 4
Input No.[2][3]: 5
Input No.[2][4]: 5
Input No.[3][0]: 4
Input No.[3][1]: 8
Input No.[3][2]: 9
Input No.[3][3]: 1
Input No.[3][4]: 2
Input No.[4][0]: 4
Input No.[4][1]: 5
Input No.[4][2]: 2
Input No.[4][3]: 1
Input No.[4][4]: 9
my output shows
Ascending:
4 4 5 8 9
0 1 2 2 4
4 5 5 6 7
1 2 4 8 9
1 2 4 5 9
as you can see its not in proper arranged, it only arranged the 5 numbers each lines not the whole numbers
can anybody help arranged my integers like this
Ascending:
0 1 1 1 2
2 2 2 4 4
4 4 4 4 5
5 5 5 6 7
8 8 9 9 9
this is my code so far
int main(){
int rows = 5, cols = 5;
int arr[rows][cols];
int i,j,k,swap;
printf("Enter 25 integers:\n");
for(i = 0; i < rows; i++){
for(j = 0; j < cols; j++){
printf("Input No.[%d][%d]: ", i+0,j+0);
scanf("%d", &arr[i][j]);
}
}
for(k = 0; k < rows; k++){
for(i = 0 ; i < cols; i++){
for(j = i + 1; j < cols; j++){
if(arr[k][i] > arr[k][j]){
swap = arr[k][i];
arr[k][i] = arr[k][j];
arr[k][j] = swap;
}
}
}
}
printf("Ascending:\n");
for( i = 0 ; i < rows; i++){
for( j = 0 ; j < cols; j++){
printf("%3d", arr[i][j]);
}
printf("\n");
}
getch();
}
Improving on Ahmad's answer, I would like to add the following code (for shorting the table in ascending order):
#include <stdio.h>
#define COL 5
#define ROW 6
int main()
{
int temp, t, i, j;
int arr[ROW][COL]={30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1};
for(t=1; t<(ROW*COL); t++)
{
for(i=0; i<ROW; i++)
{
for(j=0; j<COL-1; j++)
{
if (arr[i][j]>arr[i][j+1])
{
temp=arr[i][j];
arr[i][j]=arr[i][j+1];
arr[i][j+1]=temp;
}
}
}
for(i=0; i<ROW-1; i++)
{
if (arr[i][COL-1]>arr[i+1][0])
{
temp=arr[i][COL-1];
arr[i][COL-1]=arr[i+1][0];
arr[i+1][0]=temp;
}
}
}
for(i=0; i<ROW; i++)
{
printf("\n");
for(j=0; j<COL; j++)
printf("%3d", arr[i][j]);
}
return 0;
}
replace the input with your table and the definitions with the size of your given array and you're done.
output of the above when executed:
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
void twoDimBubbleSort(int** arr, int row, int col) {
for (int i = 0; i < (row * col); ++i) {
for (int j = 0; j < (row * col) - 1; ++j) {
int cr = j / col; // current row
int cc = j % col; // current column
int nr = (j + 1) / col; // next item row
int nc = (j + 1) % col; // next item column
if (arr[cr][cc] > arr[nr][nc])
swap(&arr[cr][cc], &arr[nr][nc]); // any way you want to swap variables
}
}
}
You don't necessarily need to create a 1D array, you can consider your 2D array is a 1D array and transform coordinates when you set/get them.
Consider a structure point with x and y, and ARR_LEN is 5.
int from2Dto1D(point p){ return p.x+ p.y*ARR_LEN;}
Point from1Dto2D(int i){ Point p; p.x = i%ARR_LEN; p.y=i/ARR_LEN; return p;}
Now you can use the normal bubble sorting algorithm with a 1D index on 2D squares array, you just need to convert your index into 2 Point and access/switch data using these Point. (2 because you need a Point with index and a Point with index+1
Put all the array elements from 2-D array to 1-D array then
sort that 1-D array and then put 1-D array in the matrix format
Try this code ....works according to the above given logic
#include<stdio.h>
int main(){
int arr[5][5],l=0;
int result[25],k=0,i,j,temp;
arr[0][0]= 4;
arr[0][1]= 5;
arr[0][2]= 8;
arr[0][3]= 9;
arr[0][4]= 4;
arr[1][0]= 2;
arr[1][1]= 1;
arr[1][2]= 0;
arr[1][3]= 2;
arr[1][4]= 4;
arr[2][0]= 6;
arr[2][1]= 7;
arr[2][2]= 4;
arr[2][3]= 5;
arr[2][4]= 5;
arr[3][0]= 4;
arr[3][1]= 8;
arr[3][2]= 9;
arr[3][3]= 1;
arr[3][4]= 2;
arr[4][0]= 4;
arr[4][1]= 5;
arr[4][2]= 2;
arr[4][3]= 1;
arr[4][4]= 9;
//convert 2 D array in 1 D array
for(i=0;i<5;i++){
printf("\n");
for(j=0;j<5;j++){
printf(" %d",arr[i][j]);
result[k++]=arr[i][j];
}
}
// sort 1 D array
for(i=0;i<25;i++){
for(j=0;j<24;j++){
if(result[j] > result[j+1]){
temp=result[j];
result[j]=result[j+1];
result[j+1]=temp;
}
}
}
/*
for(i=0;i<25;i++){
printf("\n%d",result[i]);
}*/
// convert 1 D array to 2 D array
i=0;
l=0;k=0;
while(i<25){
for(j=0;j<5;j++){
arr[k][j]=result[l];
l++;
}
k++;
i=i+5;
}
//Print matrix i.e 2D array
for(i=0;i<5;i++){
printf("\n");
for(j=0;j<5;j++){
printf(" %d",arr[i][j]);
}
}
}
This works !
#define COL 5
#define ROW 2
int main(){
int temp ;
int arr[2][5]= {2,15,26,14,12,18,1,2,3,4 };
int arr2[10] = {0};
int index = 0 ;
for(int t = 0 ; t<50 ; t++ ){
for (int i =0 ; i < ROW ; i++){
for( int j = 0; j < 5-1 ; j++){
if (arr[i][j] > arr[i][j+1]){
temp = arr[i][j];
arr[i][j] = arr[i][j+1];
arr[i][j+1] = temp;
}
}
//checking for
for( int k = 0 ; k < ROW-1 ; k++){
if (arr[k][COL-1] > arr[k+1][0]){
temp = arr[k][COL-1];
arr[k][COL-1] = arr[k+1][0];
arr[k+1][0] = temp ;
}
}
//---------
}
}
return 0 ;
}

C programming. Sorting rows in a 2D array

I'm trying to sort the elements within the individual rows of a 2D array. I understand how to sort the elements inside a 1D array, but I am having serious trouble getting it to sort the 2D.
Code for the 1D array:
for (i = 0; i < size; i++)
{
for (j = i +1; j < size; ++j)
{
if (array2[i] > array2[j])
{
swap = array2[i];
array2[i] = array2[j];
array2[j] = swap;
}
}
}
What I want to do: 2D Array before sorting
9 2 0 1 6 3
0 9 1 2 3 8
4 2 5 4 3 6
3 6 4 3 9 3
0 2 1 2 0 4
4 1 9 4 2 7
2D array after sorting:
0 1 2 3 6 9
0 1 2 3 8 9
2 3 4 4 5 6
3 3 3 4 6 9
0 0 1 2 2 4
1 2 4 4 7 9
My code for the 2D so far:
size: the user defined dimensions (in the above case it is 6)
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
if(array[i][j] > array[i][j+1])
{
swap = array[i][j];
array[i][j] = array[i][j+1];
array[i][j+1] = swap;
}
}
}
Any help or advice would be much appreciated. Thank you all.
If you want to use your single array sorting algorithm (bubble sort) to sort the two dimensional array then you have to add the another for loop: An outer for loop which will take care of each row. Let's say m is number of row and n is number of column.
for(k=0; k< m; k++) {
for (i = 0; i < n; i++) {
for (j = i +1; j < n; ++j) {
if (array2[k][i] > array2[k][j]) {
int swap = array2[k][i];
array2[k][i] = array2[k][j];
array2[k][j] = swap;
}
}
}
}
But this is not an efficient approach to sort the array, it's time complexity will be O(mn^2)
copy all the elements of the 2d array into an 1d array
then apply any sorting algorithm on 1d array & then copy back the sorted 1d array to the 2d array.
please don't mind
if you have a better solution then post it that will be helpfull for me.
You can simply use STL to sort 2D array row-wise..
for (i=0;i<n;i++){
for ( j=0;j<n;j++){
cin>>a[i][j];
}
sort(a[i],a[i]+n);
}
int tmp,l;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
tmp = a[i][j];
l = j + 1;
for (int k = i; k < 2; k++) {
while (l < 2) {
if (tmp < a[k][l]) {
tmp = a[k][l];
a[k][l] = a[i][j];
a[i][j] = tmp;
}
l++;
}
l = 0;
}
}
}

Delete matched pairs in an Array

I'm attempting to write a game of Go Fish where points are given for matched pairs of cards. However, I can't seem to find a way to remove the matched pairs from the hand.
I have implemented a little loop to remove single cards used in other aspects of the game, as shown here:
for ( i = position - 1 ; i < user_size - 1 ; i++)
{
user_hand[i] = user_hand[i+1];
user_count[i]--;
user_size--;
}
Where the user_size is the number of cards in the user's hand, and the user_count is how many of each of the 13 card values the user holds. I can't find a way to remove pairs of the same valued cards however.
For example if the user_hand was: 2 2 4 5 6 6 6 6 1 2
I would like to remove all but one of the 2's (one pair) and all four of the 6's (two pairs). Leaving the user_hand as: 4 5 1 2. But for the life of me I can't think of a way to do this. Any help would be greatly appreciated!
In case reordering cards in users hand is not a problem, you could:
int *hand;
int handSize;
...
sort(hand, handSize); // 1. sort
int *newHand = malloc(sizeof(int) * handSize);
int i, newHandSize = 0;
for (i = 1; i < handSize; ++i) {
if (hand[i - 1] == hand[i]) {
hand[i] = -1; // 2. "flag" last value of a pair
continue;
}
newHand[newHandSize] = hand[i - 1]; // 3. copy last examined card to new hand
newHandSize++;
}
if (hand[handSize - 1] != -1) { // 4. copy last element if needed
newHand[newHandSize] = hand[handSize - 1];
newHandSize++;
}
int* handToFree = hand;
hand = newHand; // 5. replace users hand with new hand
free(handToFree); // 6. clean up
but I don't think it can go under O( n*log(n) + n )
Normally you would use 1 array for the cards and one variable to hold the number of cards.
Then you can iterate with a nested loop like this:
for (int i = 0; i < user_size; i++){
for (int j = i+1; j < user_size; j++){
if(user_hand[i] == user_hand[j]){
/* remove card at index i */
for(int z = i; z < user_size - 1; z++) user_hand[z] = user_hand[z + 1];
user_size--;
/* remove card at index j */
for(int z = j; z < user_size - 1; z++) user_hand[z] = user_hand[z + 1];
user_size--;
}
}
}
But in your example, you also got user_count[]. If I understood you right (otherwise please correct me) user_count[] holds how many cards of a specific value the user has on his hand.
So in your example with: 2 2 4 5 6 6 6 6 1 2
user_count[0] = 0
user_count[1] = 1
user_count[2] = 3
user_count[3] = 0
user_count[4] = 1
user_count[5] = 1
user_count[6] = 4
If that's what user_count[] is for, then you could simply do:
for (int i = 0; i < user_size; i++){
for (int j = i+1; j < user_size; j++){
if(user_hand[i] == user_hand[j]){
int cardNumber = user_hand[i];
/* remove card at index i */
for(int z = i; z < user_size - 1; z++) user_hand[z] = user_hand[z + 1];
user_size--;
/* remove card at index j */
for(int z = j; z < user_size - 1; z++) user_hand[z] = user_hand[z + 1];
user_size--;
/* decrement user_count by 2 */
user_count[cardNumber] = user_count[cardNumber] - 2;
}
}
}
void removePairs(int * hand, int size)
{
for(int i = 0; i < size; i++)
{
for(int j = i+1; j < size; j++)
{
if((hand[i] == hand[j]) && hand[i] != -1)
{
hand[i] = -1; // set card "ready to remove"
hand[j] = -1;
break;
}
}
}
for(int k = 0; k < size; k++)
{
if(hand[k] != -1)
{
printf("%d ", hand[k]); // you can store remaining cards here
}
}
}
Start by looping through your user_count array, and keep calling remove_card twice as long as user_count[rank] >= 2:
for(int rank = 1; rank <= 13; rank++)
while(user_count[rank] >= 2) {
remove_card(user_hand, user_count, &user_size, rank);
remove_card(user_hand, user_count, &user_size, rank);
}
For remove_card, just find the first matching card, and call your existing routine:
void remove_card(int *user_hand, int *user_count, int *user_size, int rank) {
for(int pos = 0; pos < *user_size; pos++)
if(user_hand[pos] == rank)
remove_card_at(user_hand, user_count, user_size, pos+1);
}
remove_card_at is the routine you provided in your original post to remove a card at a particular position. Note you'll have to turn user_size into a pointer and dereference it in order to modify the variable in the calling function.
Also, you should really look into using a structure or class to hold the user's hand, depending on if you're using C or C++.

Resources