This Post Contains A C++ Program For Sorting Of A Matrix By Rows With Correct Source Code, Algorithm & Output. This Program Is Written, Compiled & Executed At Turbo C/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Arrays', 'For-Loops', 'If...else' & 'Nested Loops' From C++ Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.
C++ Program For Sorting Of A Matrix By Rows.
/* Declaration Of Header Files */ #include <iostream.h> #include <conio.h> /* Start Of Main Program */ void main() { /* Declaration Of Variables */ int a[10][10], i, j, r, c, t; clrscr(); /* Asking For The Input From User */ cout << " Enter Number Of Rows & Columns Of 2D Array [ Matrix ] : "; cin >> r >> c ; // Accepting Values Of 2D Array [ Matrix ] cout << " Enter " << r * c << " Values for 2D Array : "; for ( i = 0; i < r; i++ ) { for ( j = 0; j < c; j++ ) { cin >> a [ i ][ j ]; } } // Printing Values Of 2D Array [ Matrix ] cout << " Values Of 2D Array [ Matrix ] Are : "; for ( i = 0; i < r; i++ ) { cout << " \n "; for ( j = 0; j < c; j++ ) { cin >> a [ i ][ j ]; } } /* Source Code For Sorting Of A Matrix By Rows */ for(i=0; i < r; i++) { for(j=0; j < c; j++) { for(k=j+1; k < q; k++) { if(a[i][j] > a[i][k]) { t=a[i][j]; a[i][j]=a[i][k]; a[i][k]=t; } } } } /* Printing The Output Onto The Screen/Console */ cout << " \n After Sorting Elements Of Matrix A : "; for(i=0; i < r; i++) { cout << "\n"; for(j=0; j < c; j++) { cout << a[i][j] << "\t"; } } getch(); } /* End Of Main Program */
Output:
Enter Order For Array A : 3 3 Enter 9 Values For Array : 9 8 7 6 5 4 3 2 1 After Sorting Elements Of Matrix A : 7 8 9 4 5 6 1 2 3
No comments:
Post a Comment