-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSORT_MAT.CPP
77 lines (73 loc) · 1.34 KB
/
SORT_MAT.CPP
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include<iostream.h>
#include<conio.h>
#include<process.h>
void sort(int arr[][10],int rows,int col);
void main()
{
int n1,n2,arr[10][10];
l1:
clrscr();
cout<<" To Sort A Matrix Using Row MAjor "<<endl;
cout<<" Enter Number of Rows: ";
cin>>n1;
if(cin.fail()||n1<=0)
{ cin.clear();
cin.ignore(100,'\n');
cout<<" Wrong Input Entered, Input Cannot be 0 or Negative, Be Cautious "<<endl;
cout<<" Enter Again "<<endl;
getch();
goto l1;
exit(0);
}
l2:
cout<<" Enter Number of Columns: ";
cin>>n2;
if(cin.fail()||n2<=0)
{ cin.clear();
cin.ignore(100,'\n');
cout<<" Wrong Input Entered, Input Cannot be 0 or Negative, Be Cautious "<<endl;
cout<<" Enter Again "<<endl;
getch();
goto l2;
exit(0);
}
cout<<" Enter Elements Into "<<n1<<" X "<<n2<<" Matrix ";
for(int i=0;i<n1;i++)
{
for(int j=0;j<n2;j++)
{
cin>>arr[i][j];
}
}
sort(arr,n1,n2);
getch();
}
void sort(int arr[][10],int rows,int col)
{
int mat[10][10],temp;
for(int i=0;i<rows;i++)
{
for(int j=0;j<col;j++)
{
for(int k=j+1;k<col;k++)
{
if(arr[i][j]>arr[i][k])
{
temp=arr[i][j];
arr[i][j]=arr[i][k];
arr[i][k]=temp;
}
}
}
}
cout<<"\nAfter Sorting (Using Row Major Approach)\n";
for(i=0;i<rows;i++)
{
for(int j=0;j<col;j++)
{
cout<<arr[i][j]<<"\t";
}
cout<<endl;
}
getch();
}