Answer to Question #63732 in C++ for Tayyaba

Question #63732
 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.
1
Expert's answer
2016-12-04T04:00:12-0500
#include <iostream>
#include <conio.h>

using namespace std;

int getOddNumbers(int* inArr, int* outArr, int n)
{
int nOutArr = 0;
for(int i=0; i<n; i++) {
if(*(inArr+i)%2) {
*(outArr+nOutArr) = *(inArr+i);
nOutArr++;
}
}
return nOutArr;
}

int getEvenNumbers(int* inArr, int* outArr, int n)
{
int nOutArr = 0;
for(int i=0; i<n; i++) {
if(!(*(inArr+i)%2)) {
*(outArr+nOutArr) = *(inArr+i);
nOutArr++;
}
}
return nOutArr;
}

void bublSort(int* inArr, const int n)
{
for(int i=n-1; i>0; i--) {
for(int j=0; j<i; j++) {
if(*(inArr+j)>*(inArr+j+1)) {
int buf = *(inArr+j+1);
*(inArr+j+1) = *(inArr+j);
*(inArr+j) = buf;
}
}
}
}

int main()
{
int arr[10];
int newArr[10];
int n = 0;
char key;

for (int i = 0; i < 10; i++) {
cout << "[" << i + 1 << "]: ";
cin >> arr[i];
}

cout << "\n1 - Showing odd numbers \n2 - Showing even numbers \nYour choice: ";
cin >> key;

if(key=='1') {
n = getOddNumbers(&arr[0], &newArr[0], 10);
} else {
n = getEvenNumbers(&arr[0], &newArr[0], 10);
}

bublSort(&newArr[0], n);

cout << "\n";
for (int i = 0; i < n; i++) {
cout << newArr[i] << " ";
}

cout << endl;

getch();

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!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS