68 471
Assignments Done
98,8%
Successfully Done
In December 2018

# Answer to Question #63734 in C++ for Tayyaba

Question #63734
 Write a program that takes input of 10 integers into an array.
 Give two options to the user:
1= copy odd numbers from the array to a new array (odd[] array) and sort “odd[]” array in ascending order
and
2= copy even numbers from the array to a new array (even[] array) and sort “even[]” array in ascending order

 Write a function that accept (odd[] or even[]) array and sort it
 Use bubble sort technique to sort the given array
 Display the sorted array.
#include <iostream>
#include <cstdlib>

#define SIZE 10

using namespace std;

void printArray(int array[], int size) {
int last = size - 1;

for (int i = 0; i < last; i++) {
cout << array[i] << ' ';
}
cout << array[last] << endl;
}

void sortArray(int array[], int size) {
int len = size - 1;
bool swapped = false;

for ( int j = 0; j < len && !(swapped); j++ ) {
swapped = true;
for ( int i = 1; i < size - j; i++ ) {
if ( array[i-1] > array[i] ) {
int swap = array[i-1];

array[i-1] = array[i];
array[i] = swap;
swapped = false;
}
}
}
printArray(array, size);
}

void createOddArray(int array[], int size) {
int oddArray[SIZE];
int newSize = 0;

for ( int i = 0; i < size; i++ ) {
if ( array[i] % 2 == 1 ) {
oddArray[newSize] = array[i];
newSize += 1;
}
}

if ( newSize == 0 ) {
cout << "Sorry. We don't have odd numbers" << endl;
return;
}

sortArray(oddArray, newSize);
}

void createEvenArray(int array[], int size) {
int evenArray[SIZE];
int newSize = 0;

for ( int i = 0; i < size; i++ ) {
if ( array[i] % 2 == 0 && array[i] != 0 ) {
evenArray[newSize] = array[i];
newSize += 1;
}
}

if ( newSize == 0 ) {
cout << "Sorry. We don't have even numbers" << endl;
return;
}

sortArray(evenArray, newSize);
}

int main() {
bool flag = 1;
char choice;
int array[SIZE];

cout << "Please input 10 numbers:" << endl;
for ( int i = 0; i < SIZE; i++ ) {
cout << i + 1 << ") ";
cin >> array[i];
}

do {
cout << "Please choose odd(O) or even(E) numbers: ";
cin >> choice;
if ( choice == 'E' || choice == 'e' || choice == 'o' || choice == 'O' ) {
flag = 0;
} else {
cout << "Sorry. Invalid input data. Try again." << endl;
}
} while( flag );

if ( choice == 'O' || choice == 'o' ) {
createOddArray(array, SIZE);
} else {
createEvenArray(array, SIZE);
}

system("pause");

return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!