Cześć, ostatnio mam do napisania program sortujący macierz 5 na 5.
Zastanawiam się jak te 5 kolumn wrzucić w pętle. No bo po co było pisać to 5 razy, skoro wystarczy jeden loop, tylko właśnie nie wiem jak to w ogóle zawrzeć, z góry dziękuję za pomoc.
Mam coś takiego:
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <time.h>
using namespace std;
const int n = 5;
int succesful;
int main(){
int tab[n][n], line;
srand(time(NULL));
cout<<"PRZED SORTOWANIEM"<<endl;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
tab[i][j] = (rand() %9 ) + 1;
cout<<tab[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
cout<<"PO SORTOWANIU OD NAJMNIEJSZEJ DO NAJWIEKSZEJ"<<endl;
for(int i = 0; i < n - 1; i++){
for(int j = 0; j < n - 1; j++){
if(tab[0][j] > tab[0][j + 1]) swap(tab[0][j], tab[0][j + 1]);
}
}
for(int i = 0; i < n - 1; i++){
for(int j = 0; j < n - 1; j++){
if(tab[1][j] > tab[1][j + 1]) swap(tab[1][j], tab[1][j + 1]);
}
}
for(int i = 0; i < n - 1; i++){
for(int j = 0; j < n - 1; j++){
if(tab[2][j] > tab[2][j + 1]) swap(tab[2][j], tab[2][j + 1]);
}
}
for(int i = 0; i < n - 1; i++){
for(int j = 0; j < n - 1; j++){
if(tab[3][j] > tab[3][j + 1]) swap(tab[3][j], tab[3][j + 1]);
}
}
for(int i = 0; i < n - 1; i++){
for(int j = 0; j < n - 1; j++){
if(tab[4][j] > tab[4][j + 1]) swap(tab[4][j], tab[4][j + 1]);
}
}
for(int i = 0; i < n - 1; i++){
for(int j = 0; j < n - 1; j++){
if(tab[5][j] > tab[5][j + 1]) swap(tab[5][j], tab[5][j + 1]);
}
}
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
cout<<tab[i][j]<<" ";
}
cout<<endl;
}
return 1337;
}