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].
Related
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
Given this 3x3(nxm) matrix, how can I traverse and get the sum of all possible square-submatrices i.e(1x1,2x2 in this particular case)
2 2 3
3 4 5
4 5 5
I know here,
every element is an individual submatrix(1x1),
and the rest square-submatrices are as follow:
2 2
3 4
2 3
4 5
3 4
4 5
4 5
5 5
I've tried my approach and failed multiple times, the main reason is I get confused with matrices in programming.
My Approach:
Here, 'l' is size of square-sub matrix, 'n' size of rows of main matrix, 'm' size of cols of main matrix,
for (i=0; i<n-l; i++){
for (j=1; j<n-i+1; j++){
sum = 0;
for (p=i; p<l+i; p++){
for (q=1; q<l+j+1; q++){
sum += a[p][q];
}
}
cout << sum << endl;
}
l++;
}
This is the solution of your case.
for(int i=0; i+(l-1)<n; i++){
for(int j=0; j+(l-1)<m; j++){
int sum = 0;
for(int a=0; a<l; a++){
for(int b=0; b<l; b++){
sum += matrix[i+a][j+b];
}
}
cout << sum << endl;
}
}
or you can try to run the full code on Tio.run
I rewrote your code to make it correct, also made full example:
Try it online!
#include <vector>
#include <algorithm>
#include <iostream>
int main() {
std::vector<std::vector<int>> a = {{2, 2, 3}, {3, 4, 5}, {4, 5, 5}};
size_t n = a.size(), m = a.size() > 0 ? a[0].size() : 0;
for (size_t l = 1; l <= std::min(n, m); ++l)
for (size_t i = 0; i < n - l + 1; ++i)
for (size_t j = 0; j < m - l + 1; ++j) {
int sum = 0;
for (size_t p = i; p < i + l; ++p)
for (size_t q = j; q < j + l; ++q)
sum += a[p][q];
std::cout << l << ", " << i << ", " << j << ": " << sum << std::endl;
}
}
Input:
2 2 3
3 4 5
4 5 5
Output:
1, 0, 0: 2
1, 0, 1: 2
1, 0, 2: 3
1, 1, 0: 3
1, 1, 1: 4
1, 1, 2: 5
1, 2, 0: 4
1, 2, 1: 5
1, 2, 2: 5
2, 0, 0: 11
2, 0, 1: 14
2, 1, 0: 16
2, 1, 1: 19
3, 0, 0: 33
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 using 2 ARRAYS OF DIFFERENT SIZES in One 2-Dimensional Vector, and my output is not correct. The arrays are size 4 and size 13.
I want COLUMN 0 to have: 55, 66, 77, 88.
I want COLUMNs 1-12 to have 1,2,3,4,5,6,7,8,9,10,10,10,11 in EACH ROW.
It would seem that the 2nd loop for the size 13 array would need to loop 4 times in order to fill 4 rows, however, I'm not sure how to do that.
Here is what I have so far in code and output:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int typeArray[4] = {55,66,77,88};
int valArray[13] = {1,2,3,4,5,6,7,8,9,10,10,10,11};
// 4 = LENGTH or NUMBER of ROWS; 13 = WIDTH or NUMBER of COLUMNS;
// 0 = VALUE all cells are initialized to
vector< vector <int> > myVector(4, vector<int> (14,0));
for (int i = 0; i < myVector.size(); i++)
{
myVector[i][0] = typeArray[i];
for (int j = 0; j < myVector[i].size(); j++)
{
myVector[1][j] = valArray[j];
}
}
// print vector to screen with 4 ROWS, 1 COLUMNS
for (int i = 0; i < myVector.size(); i++)
{
for (int j = 0; j < myVector[i].size(); j++)
{
cout << myVector[i][j] << ' ';
}
cout << '\n';
}
OUTPUT----------------------------------------------------------------------------------------------------------------------------------------------------
55 0 0 0 0 0 0 0 0 0 0 0 0----first row-----------------------------------------------------------------------------------------------------------------
1 2 3 4 5 6 7 8 9 10 10 10 11----second row-----------------------------------------------------------------------------------------------------
77 0 0 0 0 0 0 0 0 0 0 0 0-------third row----------------------------------------------------------------------------------------------------------
88 0 0 0 0 0 0 0 0 0 0 0 0-------fourth row
int typeArray[4] = {55,66,77,88};
int valArray[14] = {0,1,2,3,4,5,6,7,8,9,10,10,10,11};
// 4 = LENGTH or NUMBER of ROWS; 13 = WIDTH or NUMBER of COLUMNS;
// 0 = VALUE all cells are initialized to
vector< vector <int> > myVector(4, vector<int> (14,0));
for (int i = 0; i < myVector.size(); i++)
{
myVector[i][0] = typeArray[i];
for (int j = 0; j < myVector[i].size(); j++)
{
myVector[1][j + 1] = valArray[j];
}
}
// print vector to screen with 4 ROWS, 14 COLUMNS
for (int i = 0; i < myVector.size(); i++)
{
for (int j = 0; j <= myVector[i].size(); j++)
{
cout << myVector[i][j] << ' ';
}
cout << '\n';
}
OUTPUT----------------------------------------------------------------------------------------------------------------------------------------------------
55 1 2 3 4 5 6 7 8 9 10 10 10 11----first row-------------------------------------------------------------------------------------------------------
66 1 2 3 4 5 6 7 8 9 10 10 10 11----second row--------------------------------------------------------------------------------------------------
77 1 2 3 4 5 6 7 8 9 10 10 10 11----third row----------------------------------------------------------------------------------------------------
88 1 2 3 4 5 6 7 8 9 10 10 10 11----fourth row
This is the revised code per Kerrek's update, it works perfectly:
int typeArray[4] = {55,66,77,88};
int valArray[13] = {1,2,3,4,5,6,7,8,9,10,10,10,11};
// 4 = LENGTH or NUMBER of ROWS; 13 = WIDTH or NUMBER of COLUMNS;
// 0 = VALUE all cells are initialized to
vector< vector <int> > myVector(4, vector<int> (14,0));
for (int i = 0; i < myVector.size(); i++)
{
myVector[i][0] = typeArray[i];
for (int j = 1; j < myVector[i].size(); j++)
{
myVector[i][j] = valArray[j - 1];
}
}
// print vector to screen with 4 ROWS, 14 COLUMNS
for (int i = 0; i < myVector.size(); i++)
{
for (int j = 0; j < myVector[i].size(); j++)
{
cout << myVector[i][j] << ' ';
}
cout << '\n';
}
OUTPUT IS THE SAME AS THE 2ND BLOCK OF CODE, YES!
Something like this perhaps:
vector< vector <int> > myVector(4, vector<int> (14, 0)); // one longer!
for (int i = 0; i < myVector.size(); i++)
{
myVector[i][0] = typeArray[i];
for (int j = 1; j < myVector[i].size(); j++)
{
myVector[i][j] = valArray[j - 1];
}
}
i'm trying to figure out how to fill a multidimentional array in this way:
Input: rows = 3 , cols = 3 :
1 4 7
2 5 8
3 6 9
can somebody give me an idea?
P.S My task is to find how many nubers stay in the same position in both arrangements. Ex:
1 4 7 1 2 3
2 5 8 4 5 6
3 6 9 7 8 9
so the numbers that are in the same position are : 1 5 9.
i've tryied :
//n = 3 , m = 3
for(int i = 0; i <n; i++) {
for(int j = 0; j <m; j++){
if(array[i][j] == array2[i][j]) {
lol++;
}
}
}
cout<<lol;
/*
1 2 3
4 5 6
7 8 9
1 4 7
2 5 8
3 8 9
*/
it must show me 3 , but it shows 0, where is the problem?
Populate at initialization:
int a[3][3] = { { 1, 4, 7},
{ 2, 5, 8},
{ 3, 6, 9}
};
EDIT (unsure if resolved):
After update to question here is an example application that (with modification to accept input from user) will diff two arrays and construct an array indicating the elements that were the same and a count of the number of identical elements:
#include <iostream>
int** make_array(const size_t a_rows, const size_t a_columns)
{
int** result = new int*[a_rows];
for (size_t i = 0; i < a_rows; i++)
{
*(result + i) = new int[a_columns];
}
return result;
}
void print_array(int** a_array, const size_t a_rows, const size_t a_columns)
{
for (size_t r = 0; r < a_rows; r++)
{
for (size_t c = 0; c < a_columns; c++)
{
std::cout << *(*(a_array + r) + c) << " ";
}
std::cout << "\n";
}
std::cout << "\n";
}
int main()
{
// Example data.
int a[3][3] = { { 1, 4, 7},
{ 2, 5, 8},
{ 3, 6, 9}
};
int b[3][3] = { { 1, 2, 3},
{ 4, 5, 6},
{ 7, 8, 9}
};
size_t rows = 3;
size_t columns = 3;
// Create three arrays:
// - two input arrays
// - array that represents which elements are the same
int** in_1 = make_array(rows, columns);
int** in_2 = make_array(rows, columns);
int** diff = make_array(rows, columns);
// Populate with example data.
for (size_t r = 0; r < rows; r++)
{
for (size_t c = 0; c < columns; c++)
{
*(*(in_1 + r) + c) = a[r][c];
*(*(in_2 + r) + c) = b[r][c];
}
}
// Diff.
// The 'diff' array will hold '1' for elements that
// were the same and '0' for elements that were not.
size_t same_count = 0;
for (size_t r = 0; r < rows; r++)
{
for (size_t c = 0; c < columns; c++)
{
*(*(diff + r) + c) = *(*(in_1 + r) + c) == *(*(in_2 + r) + c);
same_count += *(*(diff + r) + c);
}
}
std::cout << "\n";
// Results.
print_array(in_1, rows, columns);
print_array(in_2, rows, columns);
print_array(diff, rows, columns);
std::cout << "Same element count: " << same_count << "\n";
// Free...
return 0;
}
Output:
$ ./cpp/main.exe
1 4 7
2 5 8
3 6 9
1 2 3
4 5 6
7 8 9
1 0 0
0 1 0
0 0 1
Same element count: 3
Create a dynamically allocated array if you only know the dimensions at runtime:
int** x = new int*[rows];
for ( int i = 0 ; i < rows ; i++ )
x[i] = new int[cols];
And then populate it:
for ( int i = 0 ; i < rows ; i++ )
for ( int j = 0 ; i < cols ; j++ )
x[i][j] = y;
Or better yet, use a vector of vectors, this will give you more flexibility:
std::vector<std::vector<int> > x;
A simple solution would be this:-
int k = 1;
for(int i = 0; i < row; i++){
for(int j = 0; j < col; j++){
a[j][i] = k; // filling it in the column first order
++k;
}
}