Here's an example:
1 2 3
4 5 6
7 8 9
After rotating:
4 1 2
7 5 3
8 9 6
4x4 example:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
5 1 2 3
9 10 6 4
13 11 7 8
14 15 16 12
5x5 is similar
I can rotate 90° but that's not true in this exercise
A element of the matrix just move once
I have tried for 2 hours to find the algorithm but my code doesn't work.
Please help me solve this problem
#include <iostream>
using namespace std;
void input(int **a,int row,int column)
{
for(int i=0; i<row; i++)
{
for(int j=0; j<column; j++)
cin>>a[i][j];
}
}
void output(int **a,int row,int column)
{
for(int i=0; i<row; i++)
{
for(int j=0; j<column; j++)
cout<<a[i][j]<< " ";
cout<<endl;
}
}
void rotate(int **a,int **b,int m,int n)
{
for(int i=0; i<n; i++)
{
int k=m-1;
for(int j=0; j<m; j++)
{
b[i][j]=a[k][i];
k--;
}
}
}
int main()
{
int m,n;
cin>>m>>n;
int **a=new int*[m];
for(int i=0; i<m; i++)
a[i]=new int[n];
int **b=new int*[n];
for(int i=0; i<n; i++)
b[i]=new int[m];
input(a,m,n);
rotate(a,b,m,n);
output(b,n,m);
return 0;
}
Although I didn't find some single mathematical formula for this rotation, I did it using four tiny loops.
Also my program supports any arbitrary size of matrix NxM, not only square, and not only odd sizes.
For simplicity of running following code snippet instead of reading matrix from std::cin, I inlined values of matrix elements as constant in code.
Also for simplicity I used std::vector not to do any new/delete operations of plain matrix. It is quite obvious how to adopt my solution to your case of plain array.
Try it online!
#include <vector>
#include <iomanip>
#include <iostream>
void output(auto const & a) {
for (size_t i = 0; i < a.size(); ++i) {
for (size_t j = 0; j < a[i].size(); ++j)
std::cout << std::setw(2) << a[i][j] << " ";
std::cout << std::endl;
}
}
void rotate(auto & a) {
int i_first = 0, i_last = a.size() - 1,
j_first = 0, j_last = a[0].size() - 1;
auto b = a;
while (i_first <= i_last && j_first <= j_last) {
for (int j = j_first + 1; j <= j_last; ++j)
b[i_first][j] = a[i_first][j - 1];
for (int i = i_first + 1; i <= i_last; ++i)
b[i][j_last] = a[i - 1][j_last];
for (int j = j_last - 1; j >= j_first; --j)
b[i_last][j] = a[i_last][j + 1];
for (int i = i_last - 1; i >= i_first; --i)
b[i][j_first] = a[i + 1][j_first];
++i_first; --i_last;
++j_first; --j_last;
}
a = b;
}
int main() {
std::vector<std::vector<int>> a = {
{ 0, 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},
};
std::cout << "Before:" << std::endl;
output(a);
rotate(a);
std::cout << "After:" << std::endl;
output(a);
}
Output:
Before:
0 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
After:
6 0 1 2 3 4
12 13 7 8 9 5
18 19 15 14 10 11
24 20 21 22 16 17
25 26 27 28 29 23
Related
I want to make a program that converts an array into matrix. I made something that sort of works but I'm not quite satisfied with the result. I am getting output that actually makes sense but I don't know what I should change to get the one I want.
The size of matrix doesn't have to be 4x4 it can be whatever in fact I want to make one that can make me a 4x4 out of an array[15].
#include<iostream>
#include<vector>
#include<ctime>
#include<cstdlib>
typedef unsigned int uint;
void print(const std::vector<int>& array)
{
for(uint i=0; i< array.size(); i++)
std::cout << array[i] << " ";
std::cout << "\n" << std::endl;
}
void random(std::vector<int>& array, int size = 4)
{
srand(time(0));
std::vector<std::vector<int>> mat;
for(uint i=0; i<4; i++)
{
for(uint j=0; j<4; j++)
{
array.push_back(rand()%10);
}
mat.push_back(array);
}
print(array);
for(uint i=0; i<4; i++)
{
for(uint j=0; j<4; j++)
{
std::cout << mat[i][j] << " ";
}
std::cout << std::endl;
}
}
main()
{
std::vector<int> A;
random(A);
}
Example of an output since it's random
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
The one i would like to get would look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
After you've added array to mat with mat.push_back(array), you need to clear array, otherways array will always grow.
std::vector<std::vector<int>> mat;
for (uint i = 0; i < 4; i++)
{
for (uint j = 0; j < 4; j++)
{
array.push_back(rand() % 10);
}
mat.push_back(array);
array.clear(); // <<<< add this
}
Change the code that prints mat to this:
for (uint i = 0; i < mat.size(); i++)
{
for (uint j = 0; j < mat[i].size(); j++)
{
std::cout << mat[i][j] << " ";
}
std::cout << std::endl;
}
and you'll see what happens if you don't call array.clear() as shown above.
I am attempting to print out an array in a specific order, where it is formatted with columns and rows in ascending order, but with the bottom row containing the lowest values. The array is created via a for loop and a pointer.
here is my code so far:
#include <iostream>
#include <iomanip>
int main()
{
// Creation of the array
int* array = new int[24];
for (int i = 0; i < 24; i++)
{
array[i] = i;
}
// Displaying in grid format with 3 rows and 8 columns
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 8; j++)
{
std::cout << std::setw(2) << *array << ' ';
array++;
}
std::cout << '\n';
}
}
The output of my code is:
0 1 2 3 4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23
The desired output is:
16 17 18 19 20 21 22 23
8 9 10 11 12 13 14 15
0 1 2 3 4 5 6 7
How would I go about "reversing" the rows to reach the desired output?
SOLVED WITH THE FOLLOWING
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int* table = new int[40];
for (int i = 0; i < 40; ++i)
{
table[i] = i;
}
for (int i = 4; i >= 0; --i)
{
for (int j = 0; j < 8; ++j)
{
cout << setw(2) << table[j + (8 * i)] << ' ';
}
cout << "\n";
}
}
Thanks for the help.
I am having trouble with making a turn inside a two dimensional array to output the elements in spiral. I tried this code, but it is outputting not enough elements, I tried to make some if statements outside of the loop to cover all cases for which the general algorithm doesn't output. Can you help suggesting some way to manage the correct output.
CODE
#include <iostream>
#include <algorithm>
//#include <cmath>
using namespace std;
int main() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int r, c;
cin >> r >> c;
int r_beg = 0, r_end = r - 1, c_beg = 0, c_end = c - 1;
int **m = new int*[r];
for (int i = 0; i < r; i++)
{
m[i] = new int[c];
for (int j = 0; j < c; j++)
{
cin >> m[i][j];
}
}
for (int runs = min(r, c) / 2; runs--;) {
for (int i = c_beg; i < c_end; i++)
cout << m[r_beg][i] << " ";
for (int i = r_beg; i < r_end; i++)
cout << m[i][c_end] << " ";
for (int i = c_end; i > c_beg; i--)
cout << m[r_end][i] << " ";
for (int i = r_end; i > r_beg; i--)
cout << m[i][c_beg] << " ";
r_beg++;
c_beg++;
r_end--;
c_end--;
}
if (r <= c && c_beg <= c_end) {
for (int i = c_beg; i <= c_end; i++)
cout << m[r_end][i] << " ";
}
else if (r >= c && r_beg <= r_end) {
for (int i = r_beg; i <= r_end; i++)
cout << m[i][c_end] << " ";
}
for (int i = 0; i < r; i++)
delete[] m[i];
delete[] m;
return 0;
}
Example:
Input:
3 3
1 2 3
4 5 6
7 8 9
Output: 1 2 3 6 9 8 7 4 5
If you have for example 3x10 matrix. It doesn't output.
Input:
3
7
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
Output: should get to 13, but it stops at 8.
The code, as presented, loops towards center until the smaller of the two dimensions is consumed up. However, if that smaller dimension has odd size, then parts of the central row or column respectively haven't been printed out. You can cover that one with some special case handling after your outer loop:
for (int runs = std::min(r, c) / 2; runs--;)
{
// ...
}
if(c < r)
{
if(c & 1)
{
for (int i = r_beg; i <= r_end; i++)
// ^ (!)
// don't forget to print last element: there's no second loop
// that would print the corner element a second time now!
std::cout << m[i][c_end] << " ";
}
}
else
{
// handles the square matrix case as well
if(r & 1)
{
for (int i = c_beg; i <= c_end; i++)
std::cout << m[r_beg][i] << " ";
}
}
This can be solved by carefully fine-tuning the bail-out conditions / ranges of the for loops:
#include <iostream>
using namespace std; // :-(
int main() {
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int r, c;
cin >> r >> c;
int r_beg = 0, r_end = r - 1, c_beg = 0, c_end = c - 1;
int **m = new int*[r];
for (int i = 0; i < r; i++)
{
m[i] = new int[c];
for (int j = 0; j < c; j++)
{
cin >> m[i][j];
}
}
for (int runs = min(r, c);;)
{
for (int i = c_beg; i <= c_end; i++)
cout << " " << m[r_beg][i];
++r_beg;
for (int i = r_beg; i <= r_end; i++)
cout << " " << m[i][c_end];
--c_end;
if (!--runs) break;
for (int i = c_end; i >= c_beg; i--)
cout << " " << m[r_end][i];
--r_end;
for (int i = r_end; i >= r_beg; i--)
cout << " " << m[i][c_beg];
++c_beg;
if (!--runs) break;
}
for (int i = 0; i < r; i++)
delete[] m[i];
delete[] m;
return 0;
}
Input:
3 7
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
Output:
1 2 3 4 5 6 7 14 21 20 19 18 17 16 15 8 9 10 11 12 13
Live Demo on ideone
Input:
4 4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Output:
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
Notes:
I changed the bail-out of the for loop:
Instead of min(r, c) / 2, I use min(r, c) and decrement/check runs twice in the body.
I adjusted the update of r_beg, r_end, c_beg, and c_end.
I'm stuck on this. Could you please help me?
Write a function which initialize a two-dimensional array. The array is a square
matrix (i.e., its width and height are identical.) The array should be initialized in the
zig-zag style. Specifically, we start at the top-left corner and go downward, and put a
number starting from 1. Once we hit the bottom, we go to the next column and fill in
the numbers from bottom to top. We fill the numbers downward in the third
column, upward in the fourth column, and so on. The process ends when all the
elements in the array are filled.
However, How do I get my output like this?
1 10 11 20 21
2 9 12 19 22
3 8 13 18 23
4 7 14 17 24
5 6 15 16 25
#include <iomanip>
#include <iostream>
using namespace std;
const int SIZE = 5; // Note SIZE can be anything between 1 to 9
void initGrid(int grid[SIZE][SIZE]);
void printGrid(int grid[SIZE][SIZE]);
int main() {
int grid[SIZE][SIZE];
initGrid(grid);
printGrid(grid);
}
void initGrid(int grid[SIZE][SIZE]) {
int inc = 1;
for (int j = 0; j < SIZE; j++) {
for (int i = 0; i < SIZE; i++) {
grid[i][j] = inc;
inc++;
}
}
}
void printGrid(int grid[SIZE][SIZE]) {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
// setw() function handles the printing format.
cout << setw(2) << grid[i][j] << " ";
}
cout << endl;
}
}
Every two columns of the grid share the same filling pattern, the odd ones are filled in ascending order, the even ones in descending order. All you have to do is translate that into code:
template<size_t Rows, size_t Cols>
void initGrid(int (&grid)[Rows][Cols]) {
int value = 1;
for (size_t j = 0; j < Cols; ++j) {
// first fill the odd column in descending order
for (size_t i = 0; i < Rows; ++i, ++value) {
grid[i][j] = value;
}
// then, if there is one, fill the even column
++j;
if (j == Cols )
break;
for (size_t i = Rows; i > 0; ++value) {
--i; // size_t is unsigned, so I have to check i before decrementing
grid[i][j] = value;
}
}
}
I used the same data structure you used (but a different function signature) only to focus on the algorithm, but I'd use a class instead.
If you don't want to traverse the array column-wise (which, for big arrays, could result in performance drop due to cache misses) you can calculate the differences between values in every row:
template<size_t Rows, size_t Cols>
void zig_zag_fill(int (&grid)[Rows][Cols])
{
int diff_up = 1;
int diff_down = Rows * 2 - 1;
for (size_t i = 0; i < Rows; ++i, diff_down -= 2, diff_up += 2)
{
int value = i + 1;
size_t j = 0;
while ( j < Cols )
{
grid[i][j] = value;
value += diff_down;
++j;
if ( j == Cols )
break;
grid[i][j] = value;
value += diff_up;
++j;
}
}
}
A complete test program like this:
#include <iostream>
#include <iomanip>
template<size_t Rows, size_t Cols>
void zig_zag_fill(int (&grid)[Rows][Cols]);
template<size_t Rows, size_t Cols>
void printGrid(int (&grid)[Rows][Cols]);
int main() {
int grid[5][6];
zig_zag_fill(grid);
printGrid(grid);
std::cout << '\n';
int grid2[6][5];
zig_zag_fill(grid2);
printGrid(grid2);
std::cout << '\n';
int grid3[5][5];
zig_zag_fill(grid3);
printGrid(grid3);
std::cout << '\n';
int grid4[6][6];
zig_zag_fill(grid4);
printGrid(grid4);
std::cout << '\n';
}
template<size_t Rows, size_t Cols>
void initGrid(int (&grid)[Rows][Cols]) {
int value = 1;
for (size_t j = 0; j < Cols; ++j) {
for (size_t i = 0; i < Rows; ++i, ++value) {
grid[i][j] = value;
}
++j;
if (j == Cols )
break;
for (size_t i = Rows; i > 0; ++value) {
--i;
grid[i][j] = value;
}
}
}
template<size_t Rows, size_t Cols>
void zig_zag_fill(int (&grid)[Rows][Cols])
{
int diff_up = 1;
int diff_down = Rows * 2 - 1;
for (size_t i = 0; i < Rows; ++i, diff_down -= 2, diff_up += 2)
{
int value = i + 1;
size_t j = 0;
while ( j < Cols )
{
grid[i][j] = value;
value += diff_down;
++j;
if ( j == Cols )
break;
grid[i][j] = value;
value += diff_up;
++j;
}
}
}
template<size_t Rows, size_t Cols>
void printGrid(int (&grid)[Rows][Cols]) {
for (size_t i = 0; i < Rows; ++i) {
for (size_t j = 0; j < Cols; ++j) {
std::cout << std::setw(2) << grid[i][j] << " ";
}
std::cout << '\n';
}
}
Would output:
1 10 11 20 21 30
2 9 12 19 22 29
3 8 13 18 23 28
4 7 14 17 24 27
5 6 15 16 25 26
1 12 13 24 25
2 11 14 23 26
3 10 15 22 27
4 9 16 21 28
5 8 17 20 29
6 7 18 19 30
1 10 11 20 21
2 9 12 19 22
3 8 13 18 23
4 7 14 17 24
5 6 15 16 25
1 12 13 24 25 36
2 11 14 23 26 35
3 10 15 22 27 34
4 9 16 21 28 33
5 8 17 20 29 32
6 7 18 19 30 31
Firstly, if I was writing this program I would represent my data in a different manner. I.e. I would likely use a vector and avoid global size constants. The being said, here is how I would do this. Let's write out the sequence the indexes follow and try to see if we can create some kind of sequence.
1 4 7
2 5 8
3 6 9
Is what is in your array after creating it and we want
1 6 7
2 5 8
3 4 9
Our indexes to the original array looks like this
(0, 0) (2, 1) (0, 2)
(1, 0) (1, 1) (1, 2)
(2, 0) (0, 1) (2, 2)
Well our j component is easy, it's just a simple arithmetic sequence where you add 1, i.e. 1, 2, 3, 4, 5...
for (int j = 0; j < 3; j++){
...
}
Now we need a sequence that follows the patten 0, 1, 2, 2, 1, 0, 0, 1, 2, ... repeating. Since we have repeating digits, I am thinking of clock arithmetic or modular arithmetic. Let's start with the sequence 0, 1, 2, 0, 1, 2 ... repeating. I.e the integers mod 3 (Z mod 3). For the first column we want the indicies straight up as they are (Z mod 3). Then for the reversed columns let's say we are given this sequence and we are iterating through it from 0 to 2. We can cleverly use modular arithmetic to get to our reversed sequence. For example if we had (2 mod 3), (4 mod 3), (6 mod 3), we would have 2, 1, 0. How do we get 0, 1, 2 to 2, 4, 6? Like this f(x) : (x + 1) * 2.
bool reversed = false;
for (int i = 0; i < 3; i++){
int idx = i;
if(reversed){
int offset = (i + 1) * 2;
idx = (offset) % 3;
}
}
reversed = !reversed;
Now we just have to put it all together. This should be our new init grid as the print function is fine.
for (int j = 0; j < SIZE; j++){
bool reversed = false;
for (int i = 0; i < SIZE; i++){
int idx = i;
if(reversed){
int offset = (i + 1) * (SIZE - 1);
idx = (offset) % SIZE;
}
arr[idx][j] = inc;
inc++;
}
reversed = !reversed;
}
That should do it. I got it to work in repl.it, hopefully this helps.
I am trying to transpose a 4 by 5 matrix to a 5 by 4 matrix. This is what I have done so far:
#include <stdio.h>
void transposeMatrix(int A2[4][5],int A1[5][4])
{
int i, j;
for (i = 0; i < 5; ++i){
for (j = 0; j < 4; ++j)
A1[j][i]=A2[i][j];
}
}
int main(void)
{
int A2[4][5] = {
{ 7, 4, 2, 1, 12 },
{ 4, 6, 22, 11, 6 },
{ 12, 10, 3, 1, 2 },
{ 20, 4, 1, 3, 4 },
};
int A1[5][4];
int i, j;
printf("Original matrix:\n ");
for (i = 0; i < 4; ++i){
for (j = 0; j < 5; ++j)
printf("%5i", A2[i][j]);
printf("\n");
}
printf("Transposed matrix:\n");
transposeMatrix(A2, A1);
for (i = 0; i < 5; ++i){
for (j = 0; j < 4; ++j)
printf("%5i", A1[i][j]);
printf("\n");
}
return 0;
}
When I run my code, this is what I get:
Original matrix:
7 4 2 1 12
4 6 22 11 6
12 10 3 1 2
20 4 1 3 4
Transposed matrix:
7 4 12 20
-858993460 6 10 4
998952744 22 3 1
11599080 11 1 3
10495049-858993460-858993460-858993460
It looks like it only transposes the first row. What am I doing wrong?
You have one error in your code.
It is an index problem in the transpose function.
for (i = 0; i < 5; ++i){
for (j = 0; j < 4; ++j)
A1[i][j]=A2[j][i];
}
and not A1[j][i]=A2[i][j].