In: Computer Science
Using C++
1. Create a main function in a main.cpp file. The main function should look as follows int main() {return 0;}
2. Create an array.
3. Ask user to enter numbers in size of your array.
4. Take the numbers and store them in your array.
5. Go through your array and add all the numbers.
6. Calculate the average of the numbers.
7. Display the numbers, sum and average.
#include <iostream> using namespace std; // 1. Create a main function in a main.cpp file. The main function should look as follows int main() {return 0;} int main() { //2. Create an array. int *arr; //3. Ask user to enter numbers in size of your array. int size; cout << "Enter size of array: "; cin >> size; //4. Take the numbers and store them in your array. arr = new int[size]; cout << "Enter " << size << " integers: "; for (int i = 0; i < size; ++i) { cin >> arr[i]; } //5. Go through your array and add all the numbers. int sum = 0; for (int i = 0; i < size; ++i) { sum += arr[i]; } //6. Calculate the average of the numbers. double average = sum / (double) size; //7. Display the numbers, sum and average. cout << "Array: "; for (int i = 0; i < size; ++i) { cout << arr[i] << " "; } cout << endl << "Sum: " << sum << ", Average: " << average << endl; return 0; }