Sorting Matrix with qsort function C++ - 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;
}

Related

Printing out matrix in c++, class method problem

I want to display a matrix using class and methods. Im getting "C:\Users\PC\AppData\Local\Temp\cc41EMS5.o:lab6.cpp:(.text+0x3d6): undefined reference to `Matrix::Matrix()'
collect2.exe: error: ld returned 1 exit status" error. I guess its because of printMatrix method(that worked not as a method in main), but I dont know how to fix that.
Here is the code:
#include <cmath>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
class Matrix {
public:
Matrix();
Matrix(int rowSize, int colSize);
void setSize(int rowSize, int colSize);
string printMatrix(int rowSize, int colSize);
void free_data();
void allocate_data();
private:
int rowSize;
int colSize;
double **p;
//vector<vector<int>> matrix(rowSize, vector<int>(colSize));
};
void Matrix::allocate_data() {
p = new double *[rowSize];
for (int i = 0; i < rowSize; i++) {
p[i] = new double[colSize];
}
}
void Matrix::free_data() {
for (int i = 0; i < rowSize; i++) {
delete[] p[i];
}
delete[] p;
}
void Matrix::setSize(int rowSize, int colSize) {
int newSize = 0;
p[rowSize][colSize] = newSize;
}
Matrix::Matrix(int rowSize, int colSize) {
this->rowSize = rowSize;
this->colSize = colSize;
allocate_data();
for (int i = 0; i < rowSize; i++) {
for (int j = 0; j < colSize; j++) {
p[i][j] = 0;
}
}
}
string Matrix::printMatrix(int rowSize, int colSize) {
int i, j;
int matrix[i][j];
for (int i = 0; i < rowSize; i++) //it worked in main, but doesn't in class
{
for (int j = 0; j < colSize; j++) {
0 >> matrix[i][j];
}
cout << endl;
}
cout << "Matrix" << endl;
for (int i = 0; i < rowSize; i++) {
for (int j = 0; j < colSize; j++) {
cout << matrix[i][j] << " ";
}
cout << "\n";
}
cout << endl;
}
int main() {
int rowSize;
int colSize;
cout << "Enter size of rows and columns: ";
cout << "Rows: ";
cin >> rowSize;
cout << "Cols: ";
cin >> colSize;
Matrix m;
m.printMatrix(rowSize, colSize);
return 0;
}
Please send help
You declare two constructors:
Matrix();
Matrix(int rowSize, int colSize);
But you define only one:
Matrix::Matrix(int rowSize, int colSize)
{
this->rowSize=rowSize;
this->colSize=colSize;
allocate_data();
for(int i=0; i<rowSize; i++)
{
for(int j=0; j<colSize; j++)
{
p[i][j]=0;
}
}
}
Furthermore, in `main` you call the constructor that you happened to not define:
> ```
> Matrix m;
> ```
Instead, get rid of the declaration for `Matrix::Matrix()` since you lack a definition, and use the constructor that takes two arguments:
int main()
{
int rowSize;
int colSize;
cout<<"Enter size of rows and columns: ";
cout<<"Rows: ";
cin>>rowSize;
cout<<"Cols: ";
cin>>colSize;
Matrix m(rowSize, colSize);
m.printMatrix(rowSize,colSize);
return 0;
}
## Additional improvements
You don't need to pass the size of the matrix to `Matrix::printMatrix`, since the matrix already has the size as members. By passing those parameters to `printMatrix`, you're making the class more confusing and error-prone to use. If I constructed a 3x3 matrix and tried to call `printMatrix(10,10)` on it, I would certainly invoke undefined behavior and likely crash the program.
Get rid of the parameters (in both definition and declaration) and use the existing `rowSize` and `columnSize` fields to control the loops in that function. The return type also ought to be `void`.
Also, `0 >> matrix[i][j];` makes no sense. The correct syntax is `matrix[i][j] = 0;`.
You define and use a parameterless Matrix constructor (Matrix();) but you do not implement it.
You may either:
Delete the Matrix() definition and use the constructor that you define (Matrix m(rowSize, colSize);)
Implement the parameterless constructor, creating the matrix with some predefined values. You can use a delegating constructor to call the constructor that accepts parameters
Matrix::Matrix() : Matrix(4,4) {
}
I would recommend the first option

Using pointer Notation to print an array

I would like to point out some random integers using the regular print function, then print again the same integers using pointer notation. When I use pointer notation I run into some trouble. If anyone could send some tips it'd be much appreciated. If i comment out a specific line of code, the program will compile completely, but not with the outputs I'd like.
#include <iostream>
#include <ctime>
#include <stdlib.h> //srand(), rand()
using namespace std;
void printArray(int[], int);
//void printToday(int , );
int main()
{
int i = 0;
const int SZ = 100;
int myArray[SZ] ={0};
srand(time(0));
int myArrayTotal = 0;
int *thelight;
thelight = myArray;
for (int i = 0; i <=100; i++)
{
myArray[i]= i+rand()%1000 ;
}
cout << "Array Notation:\n\n";
printArray(myArray, SZ);
system("pause");
system("cls");
cout << "Pointer Notation: \n\n";
int k = 0;
for (int i = 0; i < 10; ++i)
{
for (int j = 0; j < 10; ++j)
{
cout<< *(thelight + k)<< "\t";
++k; //if I comment out this line the second part of the program will run, but it isn' the values I want.
} cout<< endl;
}
}
void printArray(int ArrayName[], int ArraySize)
{
int k = 0;
for (int i = 0; i < 10; ++i)
{
for(int j = 0; j < 10 ; ++j)
{
cout << ArrayName[k] << "\t";
++k;
}cout << endl;
}
}
Thank you

Choose the starting point of the Origin?

I was wondering if it was possible to move the starting point of the Origin to the bottom-left-corner of my grid.
This is the code I'm working with:
#include <iostream>
using namespace std;
char **createBoard(int n, int m); // Býr til tvívítt kvikt fylki og skilar því til baka
void initiaizeBoard(char **p, int n, int m); // Upphafsstillum allt með '.'
void printBoard(int n, int m, char **p); // Prentum út leikborðið
int main()
{
int rows, columns;
int xhnit;
int yhnit;
cin >> rows >> columns >> xhnit >> yhnit;
char **board = createBoard(rows, columns);
initiaizeBoard(board, rows, columns);
board[xhnit][yhnit] = player;
printBoard(rows, columns, board);
return 0;
}
char **createBoard(int n, int m)
{
char **p = new char*[n];
for (int i = 0; i < n; i++)
{
p[i] = new char[m];
}
return p;
}
void initiaizeBoard(char **p, int n, int m)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
p[i][j] = '.';
}
}
}
void printBoard(int n, int m, char** p)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cout << p[i][j];
}
cout << endl;
}
}
For the input "10 10 5 6" my output is as follows:
..........
..........
..........
..........
..........
......X...
..........
..........
..........
..........
The Origins is now set in the top left corner as you can see from the output. I've been searching on this site and the internet in general and I can't seem to figure this out.
In a way, top-left and bottom-left are just arbitrary distinctions before you print the array. The array elements don't really have a physical location before you assign them one. So, in order to move the origin you can just print the rows from first to last.
void printBoard(int n, int m, char** p)
{
for (int i = n-1; i >= 0; i--)
{
for (int j = 0; j < m; j++)
{
cout << p[i][j];
}
cout << endl;
}
}
You can treat everything the same and simply print the rows in reverse order:
void printBoard(int n, int m, char** p) {
for (int i = n-1; i > -1; i--) { // Print in reverse order!
for (int j = 0; j < m; j++) {
cout << p[i][j];
}
cout << endl;
}
}
Here is a live example: http://ideone.com/GJVw8M
I am probably misunderstanding the question but is it just the output display that needs to change?
void printBoard(int n, int m, char** p)
{
for (int i = (n-1); i >=0; i--)
{
for (int j = 0; j < m; j++)
{
cout << p[i][j];
}
cout << endl;
}
}

How to read matrix and sum them up?

I wanted to do a coding where it reads a 4x4 matrices and sum them up. I dont know where I did wrong. My result is it keeps on asking to enter the elements. I just wanted a 4x4. Can anyone help me?
#include <iostream>
using namespace std;
const int SIZE = 4;
double sumColumn(const double m[][SIZE], int rowSize, int columnIndex)
{
int sum = 0;
for (int i = 0; i<SIZE; i++)
{
for (int j = 0; j<SIZE; j++)
{
sum = sum + m[i][j];
}
}
return sum;
}
int main()
{
double m[SIZE][SIZE], sum = 0;
cout << "Enter the elements of the matrix" << endl;
for (int i = 0; i<SIZE; i++)
for (int j = 0; j<SIZE; j++)
cin >> m[i][j];
sum = sumColumn(m, SIZE, SIZE);
cout << sum << endl;
return 0;
}
A good practice is using curly braces even if the content of the "for" has one line, but in your case must be in the next way
for (int i = 0; i<SIZE; i++) {
for (int j = 0; j<SIZE; j++) {
cin >> m[i][j];
sum = sumColumn(m, SIZE, SIZE);
}
}
Greetings
The complete code is:
#include <iostream>
using namespace std;
const int SIZE = 4;
double sumColumn(const double m[][SIZE], int rowSize, int columnIndex)
{
int sum = 0;
for (int i = 0; i<SIZE; i++)
{
for (int j = 0; j<SIZE; j++)
{
sum = sum + m[i][j];
}
}
return sum;
}
int main()
{
double m[SIZE][SIZE], sum = 0;
cout << "Enter the elements of the matrix" << endl;
for (int i = 0; i<SIZE; i++)
{
for (int j = 0; j<SIZE; j++)
{
cin >> m[i][j];
sum = sumColumn(m, SIZE, SIZE);
}
}
cout << sum << endl;
return 0;
}

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;
}
}