0% found this document useful (0 votes)
5 views2 pages

#Include Stdio.h

The document contains a C++ program that reads integers from input, separates them into even and odd numbers, sorts the even numbers in ascending order, and the odd numbers in descending order. It then outputs the sorted arrays either to the console or as binary data based on user-defined options. The program includes functions for sorting and reversing the order of the odd numbers.

Uploaded by

Cristian CF
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

#Include Stdio.h

The document contains a C++ program that reads integers from input, separates them into even and odd numbers, sorts the even numbers in ascending order, and the odd numbers in descending order. It then outputs the sorted arrays either to the console or as binary data based on user-defined options. The program includes functions for sorting and reversing the order of the odd numbers.

Uploaded by

Cristian CF
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <stdio.

h>
#include <algorithm>
#include <cstdint>

/*void ordenar_pares(int arr[], int n) {


for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

void ordenar_impares(int arr[], int n) {


for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] < arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}*/

void voltear(int arr[], int n) {


for (int i = 0; i < n / 2; i++) {
// Intercambio manual usando una variable temporal
int temp = arr[i];
arr[i] = arr[n - i - 1];
arr[n - i - 1] = temp;
}
}

int main(){

char E, S;
static int32_t arr[100000];
static int32_t pares[100000];
static int32_t impares[100000];
int cont=0, paresCont=0,imparesCont=0;

scanf("%c%c\n",&E,&S);

//entrada
if(E=='T'){
int i=0;
while (scanf("%d", arr[cont]) == 1) {
cont++;
}
} else if(E=='B'){
while (fread(&arr[cont],4,1,stdin) == 1) {
cont++;
}
}
// separa en impares y pares
for(int i=0; i<cont; i++) {
if((arr[i]%2)==0){
pares[paresCont] =arr[i];
paresCont++;
} else {
impares[imparesCont] =arr[i];
imparesCont++;
}
}

// ordenamos pares
std::sort(&pares[0], &pares[0] - paresCont);
// ordenamos impares
std::sort(&impares[0], &impares[0] - imparesCont);
voltear(impares, imparesCont);

//salida
if (S == 'T') {
for (int i = 0; i < paresCont; i++) {
printf("%d ", pares[i]);
}
for (int i = 0; i < imparesCont; i++) {
printf("%d ", impares[i]);
}
printf("\n");
} else if (S == 'B') {
fwrite(pares, sizeof(int), paresCont, stdout);
fwrite(impares, sizeof(int), imparesCont, stdout);
}

return 0;
}

You might also like