227k views
2 votes
C++ In a program, declare an integer array of size of 10, ask

user to input all the array elements, the program will output all
the elements which are greater than average.

2 Answers

5 votes



#include <iostream>

int main() {

const int size = 10;

int arr[size];

int sum = 0;

std::cout << "Enter " << size << " elements:\\";

// Input array elements

for (int i = 0; i < size; ++i) {

std::cout << "Element " << i + 1 << ": ";

std::cin >> arr[i];

sum += arr[i];

}

double average = static_cast<double>(sum) / size;

std::cout << "Elements greater than average (" << average << "):\\";

// Output elements greater than average

for (int i = 0; i < size; ++i) {

if (arr[i] > average) {

std::cout << arr[i] << " ";

}

}

std::cout << std::endl;

return 0;

}

This program first declares an array of size 10 and initializes a variable to keep track of the sum of all the elements. It then asks the user to input the elements of the array using a loop, and calculates the average by dividing the sum by the size of the array.

Finally, another loop is used to iterate through the array and output all the elements that are greater than the average.

User Mash
by
8.3k points
3 votes

Answer:

Here's a C++ program that declares an integer array of size 10, asks the user to input all the array elements, and outputs all the elements which are greater than average:

```

#include <iostream>

using namespace std;

int main() {

int arr[10];

int sum = 0;

cout << "Enter 10 integers: ";

for (int i = 0; i < 10; i++) {

cin >> arr[i];

sum += arr[i];

}

double average = (double)sum / 10;

cout << "Elements greater than average: ";

for (int i = 0; i < 10; i++) {

if (arr[i] > average) {

cout << arr[i] << " ";

}

}

return 0;

}

```

Step-by-step explanation:

1. Declare an integer array of size 10.

2. Ask user to input all the array elements using a for loop and store the sum of all elements.

3. Compute the average of the elements in the array.

4. Loop through the array and output all the elements which are greater than the average.

User Dtrunk
by
8.4k points

No related questions found

Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.