fork download
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8. // Seed the random number generator
  9. srand(time(0));
  10.  
  11. // Declare an array of 10 integers
  12. int arr[10];
  13.  
  14. // Fill the array with random numbers between -10 and 10
  15. for (int i = 0; i < 10; i++) {
  16. arr[i] = rand() % 21 - 10; // Generate random numbers in [-10, 10]
  17. }
  18.  
  19. // Initialize variables to store the maximum and minimum values and their indices
  20. int maxVal = arr[0];
  21. int minVal = arr[0];
  22. int maxIndex = 0;
  23. int minIndex = 0;
  24.  
  25. // Find the maximum and minimum elements and their indices
  26. for (int i = 1; i < 10; i++) {
  27. if (arr[i] > maxVal) {
  28. maxVal = arr[i];
  29. maxIndex = i;
  30. }
  31. if (arr[i] < minVal) {
  32. minVal = arr[i];
  33. minIndex = i;
  34. }
  35. }
  36.  
  37. // Print the array elements
  38. cout << "Array elements: ";
  39. for (int i = 0; i < 10; i++) {
  40. cout << arr[i] << " ";
  41. }
  42. cout << endl;
  43.  
  44. // Print the maximum and minimum elements and their indices
  45. cout << "Maximum element: " << maxVal << " at index " << maxIndex << endl;
  46. cout << "Minimum element: " << minVal << " at index " << minIndex << endl;
  47.  
  48. return 0;
  49. }
Success #stdin #stdout 0.01s 5264KB
stdin
Standard input is empty
stdout
Array elements: -1 0 -9 7 -6 3 1 3 3 -2 
Maximum element: 7 at index 3
Minimum element: -9 at index 2