Question

//Find two smallest integers #include <iostream> #include <string> using namespace std; // implement printArray here //...

//Find two smallest integers

#include <iostream>
#include <string>
using namespace std;

// implement printArray here

// implement findTwoSmallest here


// DO NOT WRITE ANY CODE BELOW THIS LINE (EXCEPT FOR TESTING - REMOVE BEFORE SUBMITTING)
int main() {
    int n;
    int arr[100];
  
    cout << "Enter number of integers: ";
    cin >> n;
  
    cout << "Enter " << n << " integers: ";  
    for(int i = 0; i < n; i++) {
        cin >> arr[i];
    }

    cout << "Array contents: ";
    printArray(arr, n);
  
    findTwoSmallest(arr, n);
  
    return 0;
}

//Sample return
Enter number of integers: 5
Enter 5 integers: 92 10 23 17 10
Array contents: 92 10 23 17 10
Smallest: 10
Second smallest: 17

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <string>

using namespace std;

// implement printArray here
void printArray(int arr[], int size) {
    for (int i = 0; i < size; ++i) {
        cout << arr[i] << " ";
    }
    cout << endl;
}

// implement findTwoSmallest here
void findTwoSmallest(int arr[], int size) {
    int min1 = arr[0], min2 = arr[1];
    if (min1 > min2) {
        int temp = min1;
        min1 = min2;
        min2 = temp;
    }
    for (int i = 2; i < size; ++i) {
        if (arr[i] < min1) {
            min2 = min1;
            min1 = arr[i];
        } else if (arr[i] < min2 && arr[i] != min1) {
            min2 = arr[i];
        }
    }
    cout << "Smallest: " << min1 << endl;
    cout << "Second smallest: " << min2 << endl;
}

// DO NOT WRITE ANY CODE BELOW THIS LINE (EXCEPT FOR TESTING - REMOVE BEFORE SUBMITTING)
int main() {
    int n;
    int arr[100];

    cout << "Enter number of integers: ";
    cin >> n;

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

    cout << "Array contents: ";
    printArray(arr, n);

    findTwoSmallest(arr, n);

    return 0;
}
Add a comment
Know the answer?
Add Answer to:
//Find two smallest integers #include <iostream> #include <string> using namespace std; // implement printArray here //...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT