gpt-4-0314· openai
Suzhen/CodeChat
write a c++ program that ask user to enter 10 elements in array the print the largest an…
- User
write a c++ program that ask user to enter 10 elements in array the print the largest an smallest element in array
- Assistant
#include <iostream> using namespace std; int main() { int n = 10; int numbers[n]; int highest, lowest; // Ask user to input elements for the array cout << "Enter 10 elements of the array separated by space:" << endl; for (int i = 0; i < n; i++) { cin >> numbers[i]; } // Assume the first element is the highest and lowest element highest = numbers[0]; lowest = numbers[0]; // Check each element of the array and compare with the highest and lowest elements for (int i = 1; i < n; i++) { if (numbers[i] > highest) { highest = numbers[i]; } if (numbers[i] < lowest) { lowest = numbers[i]; } } // Print the highest and lowest elements cout << "The largest element in the array is: " << highest << endl; cout << "The smallest element in the array is: " << lowest << endl; return 0; }You can compile and run the program using an online compiler like www.onlinegdb.com or, if you have a compiler installed locally, compile and run with commands like
g++and./a.out. This program will ask the user to enter 10 elements in the array, and it will then print the largest and smallest elements in the array.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.