- #1
twotaileddemon
- 260
- 0
Okay so I got the majority of my program complete, however when I try to run it it doesn't display the proper value of the average. I think I somehow messed up using my pointers and arrays, but I can't locate the problem and I've spent hours trying to fix it to no avail. see comments for better description of program.
-------
#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;
}
-------
#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;
}