For starters, an array of 500 cannot equal 0. Just define it, and don't assign value until you're dealing with a certain compartment of it, or assign it in a loop. Also I'd change the while loop into a for loop, and remove the x variable :
1 2 3 4
|
for(i = 0; i <= 501; i++)
{
//code
}
|
also, 0 - 501 is 501 units. your array of 500 is only 500 units. When considering the fact that you count 0, then logically you should assume that you don't count 500... leaving a 500 unit array, not a 501 unit array. You're overloading, and it can cause problems in the stack.
2nd, why use n != -1 when it starts at 0 and escalates?
3rd
1 2 3
|
cout << "Calclulate the average please." << average = n[x] / 2 << endl;
cin >> average;
cout << "Calculate the sum please." << sum = sum(i);
|
- What are you trying to accomplish with this?
- Why are you trying to assign a variable in a cout statement(average = n[x]/2, and sum = sum(i))?
- if sum is an int, why are you trying to treat it like a function?
- In your while loop you have an x controller, that runs from 0 to 501, and after this while loop a loop that runs another 501 times, but in it you're trying to calculate based of the x controller that is only manipulated int he while loop that already ran 501 times (therefor it will always be 501). If you want to run that code for each
4th in your while loop you have this :
1 2 3 4 5 6 7
|
if (n == 1)
{
n[x] = 0;
--x;
break;
}
++x;
|
so if n == 1, then you want to set the value to 0 and (--x? Pretty sure it's x--) and then break the loop. This makes modifying the value of x redundant unless you're planning to use x to control another loop from x down to 0 or something (which if that's what you're trying to do, you're not doing it right because I don't see any modifications to x outside the while loop). Also, I think you wanted n == 1 to be n == -1.
tl;dr : this is what I think you're trying to do -
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
int something()
{
int n[500];
for (int i = 0; i < 500; i++)
{
n[i] = 0;
}
int ncount;
int sum = 0;
int average = 0;
for (int i=0; i < 500; i++)
{
cout << "Please enter your #. Type -1 to finish \n";
cin >> n[i];
if (n[i] == -1)
{
n[i] = 0;
ncount = i;
break;
}
sum = sum + n[i];
average = average + (n[i]/2);
}
cout << "The sum of all numeric input is : " << sum << endl;
cout << "the average of all numeric input is : " << average << endl;
cin.ignore();
return 0;
}
|
Or at least something like that... kinda wrote it without testing, but that's the general direction you're trying to go i believe.
Finally, this looks like it's a homework question. And the topic seems to be debugging and arrays, because the more efficient way to handle an uncertain amount of data in c/c++ would be to use vectors or juggle information between the stack and heap, as it wouldn't require you to make a predetermined array size, it could be done modular as the user inputs more and more information. Also, as you can see, an array is not needed at all for this.
I've cleaned it up for the most part, but I've left a lot of redundant code in there, see if you can finish optimizing it ;)