Block Matrix Transpose - c++

I wanted to implement transposition of a matrix by dividing the input matrix into blocks and then transposing them. I referred to the corresponding post A Cache Efficient Matrix Transpose Program? and wrote my code like this:
#include<iostream>
#include<stdlib.h>
#define m 4
#include<sys/time.h>
#include<time.h>
#include<malloc.h>
using namespace std;
int **a, **b, **c;
int count = 0;
clock_t t1, t2;
int blocksize = 2;
int main(){
a = (int **)malloc(m*sizeof(int *));
for(int i = 0;i<m;i++){
a[i] = (int *)malloc(m*sizeof(int));
}
b = (int **)malloc(m*sizeof(int *));
for(int i = 0;i<m;i++){
b[i] = (int *)malloc(m*sizeof(int));
}
for(int i=0; i<m; i++){
for(int j =0; j<m; j++){
a[i][j]=(2*i)+(3*j);
}
}
for(int i=0; i<m; i++){
for(int j =0; j<m; j++){
cout << a[i][j] << "\t";
}
cout << "\n";
}
cout << "\n";
t1 = clock();
// MAIN BLOCK TRANSPOSE CODE
for (int i = 0; i < m; i += blocksize) {
for (int j = 0; j < m; j += blocksize) {
for (int k = i; k < i + blocksize; ++k) {
for (int l = j; l < j + blocksize; ++l) {
b[k + l*m] = a[l + k*m];
}
}
}
}
t2 = clock();
for(int i=0; i<m; i++){
for(int j =0; j<m; j++){
cout << b[i][j] << "\t";
}
cout << "\n";
}
free(a);
free(b);
cout << "\n";
cout << (double)(t2-t1)/CLOCKS_PER_SEC << "\n";
return 0;
}
However, the code is not working as expected. I implemented the code that is said to be working in the corresponding post. Please help if possible.
Input Array:
0 3 6 9
2 5 8 11
4 7 10 13
6 9 12 15
Expected Output Array:
0 2 4 6
3 5 7 9
6 8 10 12
9 11 13 15
Obtained Result:
0 3 6 9
Segmentation fault

I think your matrix is supposed to be encoded in a single array, not in an array of arrays (See the Edit 2 of the linked question).
You might want to try that instead:
int *a, *b, *c;
a = (int *)malloc(m*m*sizeof(int));
b = (int *)malloc(m*m*sizeof(int));
for(int i=0; i<m; i++){
for(int j =0; j<m; j++){
a[i*m+j]=(2*i)+(3*j);
}
}
for(int i=0; i<m; i++){
for(int j =0; j<m; j++){
cout << a[i*m+j] << "\t";
}
cout << "\n";
}
cout << "\n";

Related

Square matrix in c ++

I did the above code to calculate the matrix of a square matrix but it does not work, can someone explain the error to me please
#include <iostream>
using namespace std;
int main() {
int M[3][3]={{2, 4, -6}, {1, 5, 3}, {1, 3, 2}};
int n, mult;
n=3;
cout << "Matrix: " << endl;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
cout << M[i][j] << "\t";
cout << "\n";
}
for (int i=0; i<n; i++){
for (int j=i+1; j<n; j++){
mult=M[j][i]/M[i][i];
for (int k=i; k<n; k++){
M[j][k]=M[j][k]-mult*M[i][k];
}
}
}
cout << "Echelon matrix: " << endl;
for (int i=0; i<n; i++){
for (int j=i+1; j<n; j++){
for(int k=i; k<n; k++){
cout << M[j][k] << "\t";
cout << "\n";
}
}
}
return 0;
}
In this code, I replaced int by double, even if that, for this particular matrix, it would work with int.
The main problem was that the output of the result was broken, 3 loops when two only are needed.
Note that you don't check that the Gauss pivot M[i][i] is not null. The code should be completed to handle this case.
Output:
Matrix:
2 4 -6
1 5 3
1 3 2
Echelon matrix:
2 4 -6
0 3 6
0 0 3
#include <iostream>
//using namespace std;
int main() {
double M[3][3]={{2, 4, -6}, {1, 5, 3}, {1, 3, 2}};
int n = 3;
std::cout << "Matrix: " << std::endl;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
std::cout << M[i][j] << "\t";
std::cout << "\n";
}
for (int i = 0; i < n; i++){
for (int j = i+1; j < n; j++){
double mult = M[j][i]/M[i][i];
for (int k = i; k < n; k++){
M[j][k] = M[j][k] - mult*M[i][k];
}
}
}
std::cout << "Echelon matrix: " << std::endl;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
std::cout << M[i][j] << "\t";
}
std::cout << std::endl;
}
return 0;
}

Finding min and max elements of every column and row in a matrix C++

I was trying to do the following:
Have a matrix, print the entire thing out, print at end of every row the biggest element of said row and print at the bottom of every column the smallest element of said column.
I'm pretty much a beginner at C++.
So here's what I've done so far:
#include <iostream>
#include <iomanip>
#define M 50
#define N 50
using namespace std;
int main()
{
int m,n;
int a[M][N];
int b[M],c[N];
do {
cout<<"m=";
cin>>m;
cout<<endl<<"n=";
cin>>n;
cout<<endl;
}
while(m!=n);
for(int i=0;i<m; i++) {
for(int j=0; j<n; j++){
cout<<"a["<<i<<"]["<<j<<"]=";
cin>>a[i][j];
}
}
int max_row;
max_row=0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (a[i][j] > max_row) {
max_row = a[i][j];
b[i] = max_row;
}
}
}
for (int i=0; i<m; i++)
{ for(int j=0; j<n; j++){
cout<<setw(3)<<a[i][j]<<"\t";
}
cout<<"|"<<b[i];
cout<<endl;
}
for(int i=0; i<m; i++){
cout<<setw(3)<<"-";}
cout<<endl;
for(int j=0; j<n; j++)
{cout<<c[j]<<"\t";
}
system("pause");
}
Most of the time the max_row are the correct ones such as this case:
3 2 1 |3
4 6 5 |6
7 8 9 |9
Other times they get messed up and it goes like this:
1 2 3 |3
4 33 6 |33
7 8 9 |-858993460
I really have no idea what causes it and since there are no error messages I got really confused. Also I have no idea how to make the min column ones. Any help would be appreciated.
The problem with these loops
max_row=0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (a[i][j] > max_row) {
max_row = a[i][j];
b[i] = max_row;
}
}
}
is that the value of max_row should be initialized with each iteration of the outer loop. Otherwise all rows after the first row deal with the maximum value of the previous row and in general can not have en element that is greater than the current value of max_row. So the corresponding element of the array b will not be initialized.
Also the user can enter for the matrix negative values in this case your program will output zeroes instead of maximum values.
To find maximum elements in rows and minimum elements in columns it is enough to have one pair of nested loops/
Here is a demonstrative program/
#include <iostream>
#include <iomanip>
int main()
{
const size_t N = 3;
int a[N][N] =
{
{ 1, 2, 3 },
{ 4, 33, 6 },
{ 7, 8, 9 }
};
int b[N], c[N];
for ( size_t i = 0; i < N; i++ )
{
b[i] = a[i][0];
c[i] = a[0][i];
for ( size_t j = 1; j < N; j++ )
{
if ( b[i] < a[i][j] ) b[i] = a[i][j];
if ( a[j][i] < c[i] ) c[i] = a[j][i];
}
}
for ( size_t i = 0; i < N; i++ )
{
for ( size_t j = 0; j < N; j++ )
{
std::cout << std::setw( 3 ) << a[i][j] << '\t';
}
std::cout << '|' << b[i] << '\n';
}
for ( size_t i = 0; i < N; i++ )
{
std::cout << std::setw( 3 ) << '-' << '\t';
}
std::cout << '\n';
for ( size_t i = 0; i < N; i++ )
{
std::cout << std::setw( 3 ) << c[i] << '\t';
}
std::cout << '\n';
return 0;
}
Its output is
1 2 3 |3
4 33 6 |33
7 8 9 |9
- - -
1 2 3

Fill an array diagonally

I have following program. with Input 3 5
3 rows
5 growth of numbers
The output should be:
1 2 4 7 10
3 5 8 11 13
6 9 12 14 15
But my program gives:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
Here is what I have tried so far
int main() {
int n, m, c = 0;
cin >> n >> m;
int a[n][m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
a[i][j] = ++c;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
cout << setw(4) << a[i][j];
cout << endl;
}
}
What I am doing wrong or missing?
About the spaces: Can't find reason for such behavior(first spaces are ignored), displayed on screenshot. Tried to run in different IDE's with different compilers and had such problem only in testing system.
Hi try to use tab instead.
#include <iostream>
using namespace std;
int main() {
int n, m, c = 0;
cin >> n >> m;
int *a = new int[n * m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
a[i * n + j] = ++c;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
cout << "\t" << a[i * n + j];
cout << endl;
}
delete[] a;
return 0;
}
Can't remember how I solved this problem in secondary school, but with n less than m, the following code works:
#include <iostream>
using namespace std;
void nextij(long n,long m,long& i,long& j) {
if (i==n-1) { //bottom row
if (j<n-1) { //in the left square
j = i+j+1;
i = 0;
}
else { //out of the left square
i = j-(n-1)+1;
j = m-1;
}
}
else { //other rows
if (j==0) { //left most column
j = i+1;
i = 0;
}
else { //other columns
i++;
j--;
}
}
}
int main() {
long n = 3;
long m = 5;
long a[3][5];
long i = 0;
long j = 0;
long c = 1;
while (c<=n*m) {
a[i][j] = c;
nextij(n,m,i,j);
c++;
}
for (i=0; i<n; i++) {
for (j=0; j<m; j++)
cout <<a[i][j] <<" ";
cout <<endl;
}
}
/*
output:
1 2 4 7 10
3 5 8 11 13
6 9 12 14 15
*/

looping matrix ordo 4X4

i have problem with code,where i can't looping data in c++.
so here my code:
int j,i;
int matriks[5][5];
for(i=0; i<5; i++)
{
for(j=0; j<5; j++)
matriks[i][j]=j;
}
for(i=1; i<5; i++)
{
for(j=1; j<5; j++)
cout<<matriks[i][j]<<" ";
cout<<endl;
getch();
}
the code above only shows
1 2 3 4
i want output like this:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
where is wrong code,how to create looping matrix ordo 4x4?
Try this code; I did not compile or test, but it looks like what you are trying to achieve.
int matrix[5][5];
for (int i = 0, k = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
matrix[i][j] = k++;
}
}
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
cout << matrix[i][j] << " ";
}
cout << endl;
}

Sort 2D Array Row's Depend on the last column

I want to Write a Program in c++ that swap the row but my code only sort the element of the array.
For example:
I made a array of 3*5.
I input data as follows
1 2 3 4 10
5 6 7 8 26
9 1 2 3 15
Now I want to swap the rows by ascending order depends on the last column.i.e.
5 6 7 8 26
9 1 2 3 15
1 2 3 4 10
My code Only Sort the Row form left to Right.
int arr[3][5], sum, temp;
for(int i =0; i<3; i++)
{
sum=0;
for(int j =0; j<4; j++)
{
cout << "Enter "<<j+1<<" Number of "<<i+1<<" Student";
cin >> arr[i][j];
sum = sum + arr[i][j];
}
arr[i][4]=sum;
}
for(int i = 0; i<3; i++)
{
for(int j = 0; j<5; j++)
{
cout << arr[i][j]<<"\t";
}
cout << endl;
}
cout<<"----------------"<<endl;
for(int i=0;i<3;i++)
{
for(int j = 0;j<4;j++)
{
for(int k =0;k<4;k++)
{
if(arr[i][j]<arr[i][k])
{
temp=arr[i][j];
arr[i][j]=arr[i][k];
arr[i][k]=temp;
}
}
}
}
for(int i =0;i<3;i++)
{
for(int j =0;j<5;j++)
{
cout<<arr[i][j]<<"\t";
}
cout<<endl;
}
If performance is not of significant concern, just use the same idea as Bubble sort.
Compare last column element between each pair of rows, and if not in order swap the rows. This can be done with a loop and a temporary variable.
if(matrix[i][cols-1] > matrix[j][cols-1])
{
int temp ;
for(int k = 0; k < cols; ++k)
{
temp = matrix[i][k] ;
matrix[i][k] = matrix[j][k] ;
matrix[j][k] = temp ;
}
}
Since you are using C++, you can use std::array if your matrix has fixed size rows and columns. i.e.
std::array<std::array<int, colums>, rows> matrix ;
Here is a working example which uses std::swap to do the swapping of rows. The sorting routine is essentially the bubble sort.
for(int i = 0; i < rows; ++i)
for(int j = 0; j < i; ++j)
if(matrix[j][cols-1] > matrix[i][cols-1])
std::swap(matrix[i], matrix[j]);
Maybe this is your answer:
int arr[3][5], sum, temp;
for (int i = 0; i<3; i++)
{
sum = 0;
for (int j = 0; j<4; j++)
{
cout << "Enter " << j + 1 << " Number of " << i + 1 << " Student";
cin >> arr[i][j];
sum = sum + arr[i][j];
}
arr[i][4] = sum;
}
cout << endl << endl;
for (int i = 0; i<3; i++)
{
for (int j = 0; j<5; j++)
{
cout << arr[i][j] << "\t";
}
cout << endl;
}
cout << "----------------" << endl << endl;
int tmp[5],i,j,k;
for ( i = 0; i<3; i++)
{
for ( j = 0; j<3; j++)
{
if (arr[j][4]<arr[i][4])
{
for ( k = 0; k <= 4; k++){
int t = arr[i][k];
arr[i][k] = arr[j][k];
arr[j][k] = t;
}
}
}
}
for (int i = 0; i<3; i++)
{
for (int j = 0; j<5; j++)
{
cout << arr[i][j] << "\t";
}
cout << endl;
}