I want to sort an array in C . The criterion is in zig-zag (like the game of the little vibrator), the smaller one must be left down to the right, then it goes up and down without stopping until it reaches the left above.
The following is a 3x3 example, but I want it to work for various dimensions.
8 3 11 (fin) 22 5 < 4
^ v ^
4 1 7 "Debería quedar así" -> 11 7 3
^ v ^
22 9 5 9 < 8 1 (principio)
Code:
#include<iostream>
#include<conio.h>
using namespace std;
int main(){
int numeros[100][100],filas,columnas;
cout<<"Digite el numero de filas: "; cin>>filas;
cout<<"Digite el numero de columnas: "; cin>>columnas;
//Rellenando la matriz
for(int i=0;i<filas;i++){
for(int j=0;j<columnas;j++){
cout<<"Digite un numero ["<<i<<"]["<<j<<"]: ";
cin>>numeros[i][j];
}
}
cout<<"\nMostrando matriz\n\n";
for(int i=0;i<filas;i++){
for(int j=0;j<columnas;j++){
cout<<numeros[i][j];
}
cout<<"\n";
}
getch();
return 0;
}
With a lot of help, I can do it! for there they gave me a clue to use a loop thanks for that.
#include <iostream>
#include <stdlib.h>
#include<stdio.h>
using namespace std;
int filas();
int columnas();
int llenar();
int mostraro();
int burbuja();
int zigzag();
int mostrarz();
int f,c,mayor=0,i,j,cont=0,aux,inv,x,y;
int ma[10][10], mat[10];
int mataux [10][10];
int main()
{
filas();
columnas();
llenar();
mostraro();
burbuja();
zigzag();
mostrarz();
system ("PAUSE");
return 0;
}
int filas(){
cout<<"Intruduzca el numero de Fila:";
cin>>f;
return 0;}
int columnas(){
cout<<"Intruduzca el numero de Columna:";
cin>>c;
return 0;}
int llenar(){
cout<<"Matriz ("<<f<<","<<c<<")."<<endl;
for(int i=1; i<=f; i++)
{
for(int j=1; j<=c; j++)
{
cout<<"introducir datos ("<<i<<","<<j<<"):";
cin>>ma[i][j];
}
}
cout<<endl;
return 0;}
int mostraro(){
cout<<"Los Datos De La Matriz son:"<<endl;
for(int i=1; i<=f; i++)
{
for(int j=1; j<=c; j++)
{
cout<<ma[i][j]<<"\t";
}
cout<<endl;
}
return 0;}
int burbuja(){
for(i=1; i<=f; i++){
for(j=1; j<=c; j++){
for(x=1; x<=f;x++){
for(y=1; y<=c; y++){
if(ma[i][j]<ma[x][y]){
mayor=ma[i][j];
ma[i][j]=ma[x][y];
ma[x][y]=mayor;
}
}
}
}
}
return 0;}
int zigzag(){
cout<<"Los Datos De La Matriz Fueron Ordenados de menor a mayor en zigzag vertical"<<endl;
if (c%2==0) {
int conte=0;
for(i=1; i<=f; i++)
{
conte=conte%2;
cout<<endl;
for(j=1; j<=c; j++)
{
if (conte==1)
{
mataux[i][j]=ma[i][j];
}
if (conte==0){
for(x=1; x<=c; x++){
mat[x]=ma[i][c-x+1];
}
mataux[i][j]=mat[j];
}
}
conte=i;
}
}
if (c%2!=0) {
int conta=-1;
for(int i=1; i<=f; i++)
{
for(int j=1; j<=c; j++)
{
conta=i;
conta=conta%2;
if (conta==1){
for(x=1; x<=c; x++){
mat[x]=ma[i][c-x+1];
}
mataux[i][j]=mat[j];
}
if (conta==0){
mataux[i][j]=ma[i][j];
}
}
}
}
return 0;}
int mostrarz(){
for(i=1; i<=f; i++)
{
for(j=1; j<=c; j++)
{
cout<<mataux[j][i]<<"\t";
}
cout<<endl;
}
return 0;}