How can I reverse a each column of a matrix - c++

I want to change the a matrix
A=[[1,2],[3,4]] to A'=[[3,4],[1,2]]
How can I write a C++ program to make this change
Here is my attempt. but it fails
Here I am declaring a global variable g=0
and at each iteration I am trying to reverse each row
#include<bits/stdc++.h>
using namespace std;
int g=0;
void rotaterow(vector<vector<int>>&matrix, int a, int j)
{
int n = matrix.size();
int k=n-1;
for (int i = 0; i < n/2; i++)
{
int s=matrix[a][g];
int b=matrix[k][g];
swap(matrix[a][i], matrix[k--][j]);
}
g++;// taking g as a global variable
//matrix[a][n - 1] = temp;
}
int main()
{
int n;
cin>>n;
vector<vector<int>>matrix(n, vector<int>(n));
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cin>>matrix[i][j];
}
}
for(int j=0;j<n;j++)
rotaterow(matrix,0,j);
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
cout<<matrix[i][j]<<" ";
cout<<endl;
}
}

Since you didn't mention any specific problem with your program, you can use std::swap as shown below:
#include <vector>
#include <iostream>
int main()
{
std::vector<std::vector<int>> vec = {{1,2,3}, {4,5,6}};
std::cout<<"before swap: "<<std::endl;
for(std::vector<std::vector<int>>::size_type row = 0; row < vec.size(); ++row)
{
for(std::vector<int>::size_type col = 0; col < vec.at(row).size(); ++col)
{
std::cout<<vec.at(row).at(col)<<" ";
}
std::cout<<std::endl;
}
//swap the 0th and 1st column
std::swap(vec.at(0), vec.at(1));
std::cout<<"after swap: "<<std::endl;
for(std::vector<std::vector<int>>::size_type row = 0; row < vec.size(); ++row)
{
for(std::vector<int>::size_type col = 0; col < vec.at(row).size(); ++col)
{
std::cout<<vec.at(row).at(col)<<" ";
}
std::cout<<std::endl;
}
}
The output of the above program can be seen here.

Related

how to return a 2D array from a function in c++?

this is my code i wanted to return a 2d array from a function and use it in other function inorder to modify it.
#include<bits/stdc++.h>
using namespace std;
int** createMat(int row,int col){
int arr[row][col];
for(int i=1;i<=row;i++){
for(int j=1;j<=col;j++){
cin>>arr[i][j];
}
}
return arr;
}
int main(){
int row,col;
cin>>row;
cin>>col;
createMat(row,col);
}
You should use containers. They are the bread and butter of C++.
Please follow the advice of Some programmer dude. Invest in a good C++ book.
#include <vector>
#include <iostream>
std::vector<std::vector<int>> createMat(int row, int col)
{
std::vector<std::vector<int>> data;
data.resize(row);
for (auto& c : data)
{
c.resize(col);
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
std::cin >> data[i][j];
}
}
return data;
}
int main() {
int row, col;
std::cin >> row;
std::cin >> col;
auto mydata = createMat(row, col);
// do something with it
}
Or if you don't want to use vector, you can use pointer and do it like this.
#include<iostream>
int** createMat(int row,int col)
{
int **arr = 0;
arr = new int*[row];
for (int r = 0; r < row; r++)
{
arr[r] = new int[col];
for (int c = 0; c < col; c++)
{
std::cin>>arr[r][c];
}
}
return arr;
}
int main()
{
int row,col;
std::cin>>row;
std::cin>>col;
int** createdMat = createMat(row,col);
std::cout << "print Array\n";
for (int r = 0; r < row; r++)
{
for (int c = 0; c < col; c++)
{
printf("%i ", createdMat[r][c]);
}
printf("\n");
}
//free memory
for (int r = 0; r < row; r++)
{
delete [] createdMat[r];
}
delete [] createdMat;
}

Passing matrix to a function

I am getting an error when calling the 'printMat' function. My requirement is to create a matrix after accepting the number of rows and columns, and then take input into the matrix, then call the printMat by sending the matrix and print the elements. The error is as follows:
error: parameter 'a' includes pointer to array of unknown bo
und 'int []'
#include<iostream>
using namespace std;
int row,col;
void printMat(int* a[])
{
for(int i=0; i<row; ++i)
{
for(int j=0; j<col; ++j)
{
cout<<a[i][j]<<" ";
}
}
}
int main()
{
cin>>row;
cin>>col;
int mat[row][col];
for(int i=0; i<row; ++i)
{
for(int j=0; j<col; ++j)
{
cin>>mat[i][j];
}
}
printMat(mat);
return 0;
}
int* a[]
is an array of pointer, but you are passing a pointer to an array:
int (*a)[]
The reason it doesn't work is that arrays are just a nightmare. In C++, we use vectors instead.
#include<iostream>
#include<vector>
using namespace std;
void printMat(vector<vector<int>> mat)
{
for(vector<int> one_row : mat)
{
for(int one_cell_in_this_row : one_row)
{
cout << one_cell_in_this_row << ' ';
}
cout << endl;
}
}
int main()
{
int row,col;
cin>>row;
cin>>col;
vector< vector<int> > mat( row , vector<int>(col,0) );
// ^ ^
// initialize the vector ~~~~~/ |
// with 'row' items, each |
// of which is a vector |
// of 'col' integers. ~~~~~~~~~~~~~~~~~~~~~~~~~/
for(int i=0; i<row; ++i)
{
for(int j=0; j<col; ++j)
{
int current_entry;
cin>>current_entry;
mat.at(i).at(j) = current_entry;
}
}
printMat(mat);
return 0;
}
You can solve using pointer arithmetic. See following code.
#include<iostream>
using namespace std;
int row,col;
void printMat(int *a)
{
for(int i=0; i<row; ++i)
{
for(int j=0; j<col; ++j)
{
cout<< *((a+i*col) + j)<<" ";
}
}
}
int main()
{
cin>>row;
cin>>col;
int mat[row][col];
for(int i=0; i<row; ++i)
{
for(int j=0; j<col; ++j)
{
cin>>mat[i][j];
}
}
printMat((int *)mat);
return 0;
}
Other possible solutions are explained in this link

C++: Why is the created vector not passing to the next set of for loops?

I have it so that the user defines the size of the vector and then a vector is filled. Then that vector is sorted using bubble sort (homework assignment). However, when the "sorted" vector is displayed, there are different numbers in it than what were in the original creation. How do I first create the vector, display it, then sort it and display it??
#include <iostream>
#include <vector>
#include <cmath>
#include <numeric>
using namespace std;
int main()
{
int n;
double average=0.0;
int median = 0;
double size = 0.0;
int i=0;
cout<<"Vector Length?: "<<endl;
cin>>n;
vector<int> data;
srand(time(NULL));
//Filling vector
for (int i=0; i<n; i++)
{
data.push_back(rand()%10+1);
}
for (int i=0; i<data.size(); i++)
{
cout<<"Vector: "<< data[i]<<endl;
}
size = data.size();
//Sorting
void bubbleSort(vector<int> & data);
{
for (int k = 1; k < size; k++)
{
for (int i = 0; i<size -1 - k; i++)
{
if (data[i] > data[i +1])
{
int temp = data[i];
data[i] = data[i + 1];
data[i + 1] = temp;
}
cout<<"Sorted vector: "<< data[i]<<endl;
}
}
}
First off:
make sure to include all necessary header files, e.g. stdlib.h for your used rand() function.
get rid of all unused variables, like average, median and size.
declare your bubbleSort function outside of main function, and add additional checkup code to prevent sort if list has not more than one element.
The sort problem is related to this code snippet of yours:
for (int i = 0; i<size -1 - k; i++) { ... }
Simply remove -1
To fix your sort problem, and for better output, use following code:
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <cmath>
using namespace std;
void bubbleSort(vector<int>& data)
{
if(data.size() <= 1)
return;
for(int i=1; i<data.size(); i++)
{
for(int j=0; j<data.size()-i; j++)
{
if(data.at(j) > data.at(j+1))
{
int temp = data.at(j+1);
data.at(j+1) = data.at(j);
data.at(j) = temp;
}
}
}
}
int main()
{
int n;
vector<int> data;
cout << "Vector Length?: ";
cin >> n;
// Filling vector
for(int i=0; i<n; i++)
data.push_back(rand()%10+1);
cout << "Vector: ";
for(int i=0; i<data.size(); i++)
cout << data.at(i) << ", ";
// Sorting
bubbleSort(data);
cout << endl << "Sorted Vector: ";
for(int i=0; i<data.size(); i++)
cout << data.at(i) << ", ";
return 0;
}
Hope this helps ;)
#include <iostream>
#include <vector>
#include <cmath>
#include <numeric>
using namespace std;
void printVector(vector<int> & data)
{
for (int i=0; i<data.size(); i++)
{
cout<<"Vector: "<< data[i]<<endl;
}
}
//Sorting
void bubbleSort(vector<int> & data);
{
size = data.size();
for (int k = 1; k < size; k++)
{
for (int i = 0; i<size -1 - k; i++)
{
if (data[i] > data[i +1])
{
int temp = data[i];
data[i] = data[i + 1];
data[i + 1] = temp;
}
}
}
}
int main()
{
int n;
double average=0.0;
int median = 0;
double size = 0.0;
int i=0;
cout<<"Vector Length?: "<<endl;
cin>>n;
vector<int> data;
srand(time(NULL));
//Filling vector
for (int i=0; i<n; i++)
{
data.push_back(rand()%10+1);
}
printVector(data);
bubbleSort(data);
printVector(data);
}
If you are planning to define function void bubbleSort(vector & data) later you need to declare it before calling it.\
void bubbleSort(vector<int> & data);
int main()
{
// Here your code
bubbleSort(data);
//Here your code
}
You need to define variables only just before you need it. And if you declare and never use it, you will get unused variable warnings. So better you can comment all these variables. You can un-comment whenever you need.
//double average=0.0;
//int median = 0;
You should call your function bubbleSort() from main() as follows:
bubbleSort(data);
You are not using the iterator indexes properly to sort the elements of vector. If you change your function bubbleSort as follows it will work
//Sorting
void bubbleSort(vector<int> & data)
{
int size = data.size();
for (int k = 1; k < size; k++)
{
for (int i = 0; i<size -1 ; i++)
{
if (data[i] > data[k])
{
int temp = data[i];
data[i] = data[k];
data[k] = temp;
}
//cout<<"Sorted vector: "<< data[i]<<endl;
}
}
}
complete program for your reference:
#include <iostream>
#include <vector>
#include <cmath>
#include <numeric>
using namespace std;
//Sorting
void bubbleSort(vector<int> & data)
{
int size = data.size();
for (int k = 1; k < size; k++)
{
for (int i = 0; i<size -1 ; i++)
{
if (data[i] > data[k])
{
int temp = data[i];
data[i] = data[k];
data[k] = temp;
}
//cout<<"Sorted vector: "<< data[i]<<endl;
}
}
}
int main()
{
int n;
//double average=0.0;
//int median = 0;
//double size = 0.0;
//int i=0;
cout<<"Vector Length?: "<<endl;
cin>>n;
// int n =10;
vector<int> data;
srand(time(NULL));
//Filling vector
for (int i=0; i<n; i++)
{
data.push_back(rand()%10+1);
}
for (unsigned int i=0; i<data.size(); i++)
{
cout<<"Vector: "<< data[i]<<endl;
}
bubbleSort(data);
std::cout<<"sorted vector"<<"\n";
for (unsigned int i=0; i<data.size(); i++)
{
cout<<"Vector: "<< data[i]<<endl;
}
}

Sorting Matrix with qsort function C++

Hello guys I need to know what should I pass to qsort function to make this work?
Everything else must stay as it is except arguments of qsort function.
#include <iostream>
#include <stdlib.h>
#include <math.h>
using namespace std;
void printMatrix(int **matrix, int n){
for(int i = 0; i<n; i++){
for(int j =0 ; j < n; j++)
cout<<matrix[i][j]<<" ";
cout<<endl;
}
}
void initMatrix(int **matrix, int n){
for(int i = 0; i<n; i++){
for(int j =0 ; j < n; j++){
matrix[i][j] = rand()%10;
}
}
}
int compar(const void *a, const void *b){
int ia = *((int*)a);
int ib = *((int*)b);
return ia-ib;
}
void deleteMatrix(int **matrix){
delete [] matrix;
}
int main()
{
cout<<"How many rows and cols?"<<endl;
int n;
cin>>n;
int **matrix;
matrix = new int* [n];
for(int j = 0; j < n; j++)
matrix[j] = new int[n];
initMatrix(matrix,n);
printMatrix(matrix,n);
cout<<"Sorted matrix:"<<endl;
qsort(matrix,n*n,sizeof(int),compar); //<-----------
printMatrix(matrix,n);
deleteMatrix(matrix);
return 0;
}
P.S
There must be ** pointer on matrix.
Thanks in advance!
You would probably be better off flattening the matrix to 1 dimension and using a step variable to determine where each row would start. Then sorting is easy.
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <math.h>
using namespace std;
void printMatrix(int *matrix, int matrixsize, int rowsize) {
for (int i = 0; i < matrixsize; i+= rowsize)
{
for (int j = 0; j < rowsize; j++)
{
cout << matrix[i + j] << " ";
}
cout << endl;
}
}
void initMatrix(int *matrix, int n)
{
for (int i = 0; i < n; i++)
{
matrix[i] = rand() % 10;
}
}
void deleteMatrix(int *matrix) {
delete[] matrix;
}
int main()
{
cout << "How many rows and cols?" << endl;
int row;
cin >> row;
int matrixSize = row*row;
int *matrix = new int[matrixSize];
initMatrix(matrix, matrixSize);
printMatrix(matrix, matrixSize, row);
cout << "Sorted matrix:" << endl;
sort(matrix,matrix + matrixSize); //<-----------
printMatrix(matrix, matrixSize, row);
deleteMatrix(matrix);
return 0;
}

How to sort elements into C++ matrix?

I'm new to C++ programming. I need to sort this matrix:
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
int main(int argc, char** argv) {
Mat10 a;
fillRand(a, 5, 5);
prnMat(a, 5, 5);
cout << endl;
return 0;
}
void fillRand(Mat10 m, int n, int k) {
for (int i = 0; i < n; i++)
for (int j = 0; j < k; j++)
m[i][j] = rand() % 1000;
}
void prnMat(Mat10 a, int m, int n) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++)
cout << setw(8) << a[i][j];
cout << endl;
}
}
I need to sort the matrix from the beginning from the beginning. The smallest value must be at the beginning of the of the first column. The next must be below it and so on. The result must be sorted matrix - the smallest number must be at the beginning of the left column - the biggest value must be at the end of the matrix. Would you please help to solve the problem?
EDIT
Maybe I found possible solution:
void sort(int pin[10][2], int n)
{
int y,d;
for(int i=0;i<n-1;i++)
{
for(int j=0; j<n-1-i; j++)
{
if(pin[j+1][1] < pin[j][1]) // swap the elements depending on the second row if the next value is smaller
{
y = pin[j][1];
pin[j][1] = pin[j+1][1];
pin[j+1][1] = y;
d = pin[j][0];
pin[j][0] = pin[j+1][0];
pin[j+1][0] = d;
}
else if(pin[j+1][1] == pin[j][1]) // else if the two elements are equal, sort them depending on the first row
{
if(pin[j+1][0] < pin[j][0])
{
y = pin[j][1];
pin[j][1] = pin[j+1][1];
pin[j+1][1] = y;
d = pin[j][0];
pin[j][0] = pin[j+1][0];
pin[j+1][0] = d;
}
}
}
}
}
But since I'm new to programming I don't understand is this the solution?
Here is a simple example for you:
#include <vector>
#include <algorithm>
using namespace std;
//This is the comparation function needed for sort()
bool compareFunction (int i,int j)
{
return (i<j);
}
int main()
{
//let's say you have this matrix
int matrix[10][10];
//filling it with random numbers.
for (int i = 0; i < 10; i++)
for (int j = 0; j < 10; j++)
matrix[i][j] = rand() % 1000;
//Now we get all the data from the matrix into a vector.
std::vector<int> vect;
for (int i = 0; i < 10; i++)
for (int j = 0; j < 10; j++)
vect.push_back(matrix[i][j]);
//and sort the vector using standart sort() function
std::sort( vect.begin(), vect.end(), compareFunction );
//Finally, we put the data back into the matrix
for (int i = 0; i < 10; i++)
for (int j = 0; j < 10; j++)
matrix[i][j] = vect.at(i*10 + j);
}
After this, the matrix will be sorted by rows:
1 2
3 4
If you want it to be sorted by cols:
1 3
2 4
You need to replace matrix[i][j] in the last cycle only with matrix[j][i]
If you need to read about the the sort() function, you can do it here
Hope this helps.
You can simply call std::sort on the array:
#include <algorithm> // for std::sort
int main() {
int mat[10][10];
// fill in the matrix
...
// sort it
std::sort(&mat[0][0], &mat[0][0]+10*10);
}