Esta sección te permite ver todos los mensajes escritos por este usuario. Ten en cuenta que sólo puedes ver los mensajes escritos en zonas a las que tienes acceso en este momento.
Mostrar Mensajes Menúfacu@nvd:~$ clang ordenamiento.c
facu@nvd:~$ ./a.out
Name: fooA
Age: 10
Name: fooB
Age: 50
Name: fooC
Age: 5
SORTED PEOPLE
Name: fooC
Age: 5
Name: fooA
Age: 10
Name: fooB
Age: 50
#include <stdio.h>
#include <string.h>
// We have a person
struct stPerson
{
char name[20];
int age;
};
// We show a person
void print_person(struct stPerson P)
{
printf("Name: %s \n", P.name);
printf("Age: %d \n", P.age);
}
// We show people
void show_people(struct stPerson *PEOPLE, const int N)
{
int i;
for(i = 0; i < N; i++)
print_person(PEOPLE[i]);
}
// We try to sort
void sort_people(struct stPerson *PEOPLE, const int N)
{
struct stPerson tmp;
int i, j;
for(i = 0; i < N; i++)
for(j = i + 1; j < N; j++)
if(PEOPLE[i].age > PEOPLE[j].age)
{
tmp = PEOPLE[i];
PEOPLE[i] = PEOPLE[j];
PEOPLE[j] = tmp;
}
}
int main()
{
struct stPerson A;
strcpy(A.name, "fooA");
A.age = 10;
struct stPerson B;
strcpy(B.name, "fooB");
B.age = 50;
struct stPerson C;
strcpy(C.name, "fooC");
C.age = 5;
struct stPerson list[3] = {A, B, C};
show_people(list, 3);
sort_people(list, 3);
puts("SORTED PEOPLE");
show_people(list, 3);
return 0;
}
CC
gcc
g++
clang