int main() { int n; cout << "Enter the size of the array: "; cin >> n;
int arr[n]; cout << "Enter the elements of the array: "; for (int i = 0; i < n; i++) { cin >> arr[i]; } int min1 = INT_MAX, min2 = INT_MAX; int index1, index2; for (int i = 0; i < n; i++) { if (arr[i] < min1) { min2 = min1; index2 = index1; min1 = arr[i]; index1 = i; } else if (arr[i] < min2) { min2 = arr[i]; index2 = i; } } cout << "The two smallest elements of the array are: " << min1 << " at index " << index1 << " and " << min2 << " at index " << index2 << endl; return 0;
using namespace std;
int main() {
int arr[n];int n;
cout << "Enter the size of the array: ";
cin >> n;
cout << "Enter the elements of the array: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
int min1 = INT_MAX, min2 = INT_MAX;
int index1, index2;
for (int i = 0; i < n; i++) {
if (arr[i] < min1) {
min2 = min1;
index2 = index1;
min1 = arr[i];
index1 = i;
} else if (arr[i] < min2) {
min2 = arr[i];
index2 = i;
}
}
cout << "The two smallest elements of the array are: " << min1 << " at index " << index1 << " and " << min2 << " at index " << index2 << endl;
return 0;
}