- #1

- 260

- 0

-------

#include <iostream>

using namespace std;

/* Write a program to read in a series of numbers and display how many

are greater than their average. The user will indicate that there are no more numbers.

by entering a -999. This lab requires that you use new and delete. */

int main ( )

{

// Declaration of variables

int index = 0;

int count = 0;

int amount = 0;

int sum = 0;

int capacity = 2;

int * number;

number = new int[capacity];

// Puts the numbers the user enters into an array

// Keeps track of their sum;

// Keeps track of the numbers entered;

while(true)

{

cout <<"Enter a number: ";

cin >> number[index];

if (number[index] == -999)break;

sum = sum + number[index];

amount++;

if (index == capacity)

{

int * tmp = new int[capacity];

number = tmp;

delete []tmp;

}

index++;

}

// Accumulator variable to count numbers larger than average

for (index = 0; index <= amount; index++)

{

if (number[index] > (sum / amount)) count++;

}

// Output

cout << "There are " << count

<< " numbers greater than their average." << endl;

return 0;

}