Answer to Question #161237 in C++ for Ashes

Question #161237

Write a C++ program to find the second and third smallest elements in a given array of

integers


1
Expert's answer
2021-02-11T04:20:21-0500
#include <iostream>
using namespace std;


void bubbleSort(int array[], int size) {


  // run loops two times: one for walking throught the array
  // and the other for comparison
  for (int step = 0; step < size - 1; ++step) {
    for (int i = 0; i < size - step - 1; ++i) {
      if (array[i] < array[i + 1]) {


        // swap if greater is at the rear position
        int temp = array[i];
        array[i] = array[i + 1];
        array[i + 1] = temp;
      }
    }
  }
}


// function to print the array
void printArray() {
    cout << " second largest =" << array[1]<<endl;


   cout << " Third largest= " << array[2]<<endl;


  }
 


int main() {
  int data[] = {12, 45, 87, 88, 79};
  int size = sizeof(data) / sizeof(data[0]);
  bubbleSort(data, size);
  printArray();
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
New on Blog