### Problem with summing arrays

Hey, I'm having a problem with summing the elements within my array, and also the product. When I tell the program (on the output) to calculate the sum or product of my array, the answer is definitely wrong, so there must be something wrong with my code :( Here is the function which deals with these operations:

The purose of the program: Asks the user to input integers into an array. The amount of integers to be input is also specified by the user beforehand. It then asks if the user wants to perform any operations on the elements within the arrays, at the moment the options are either calculating the sum of all the elements in the array or calculating the product. There are various extra snippets which allows the user to loop back round to perform more operations too.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041`` `````` void operations(int array[],int elements) { int choice; cout << "\nChoose from one of the following options to\n" << "perform the specified operation" << "\n\n Option 1: The sum of all the integers in your array" << "\n Option 2: The product of all the integers in your array" << "\n\n Enter the number of the option you wish to perform: "; cin >> choice; switch (choice) { int i; int accumulator=0; int product=0; case 1: for (i=0;i

I'm sure the function which handles entering values into the arrays are fine, since I have another function which outputs the values and they all come out fine. But here is what the whole program looks like anyway, although most of it may be irrelevant for the purpose of this thread.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147`` ``````#include #include #include using namespace std; int briefing(); void inputValues(int array[],int elements); void displayValues(int array[], int elements); void operations(int array[],int elements); char yesOrNo(); int main() { char answer; int numElements = briefing(); if (numElements<0) { exit; } int userArray[128]; inputValues(userArray, numElements); displayValues(userArray,numElements); system("PAUSE"); for (answer='Y';answer=='Y';) { operations(userArray,numElements); answer=yesOrNo(); for (;(answer!='Y')&&(answer!='N');) // This logical expression is true if the answer does not equal either N or Y. { // If true, control then proceeds into the loop asking the user to re-enter the value. cout << "\n\nYou entered an invalid character." << "\n Please enter N for NO or Y for YES: "; cin >> answer; } } return 0; } int briefing() { int NumElements; cout << "This program performs operations on an array defined by \n" << "you. Please enter the number of integers to be stored into \n" << "your array. Enter a negative number to terminate the \n" << "program.\n"; cin >> NumElements; return NumElements; } void inputValues(int array[],int elements) { int i; for (i=0;i> value; if (value<0) { exit; } array[i] = value; } return; } void displayValues(int array[], int elements) { int i; for (i=0;i> choice; switch (choice) { int i; int accumulator=0; int product=0; case 1: for (i=0;i> yesorno; return yesorno; } ``````

Any help is appreciated!
Last edited on
You need to initialize your variables (product and accumulator) before using them. When you declare them, they get some space in memory, but no value will be set to them. They will have whatever value that their bits happen to have. So when you start just adding and multipling on to these numbers, you have no idea what you're going to get.

Also I'm a bit surprised that you're allowed to declare variables in a switch outside of any of the cases, but if that works then that's not a problem.

You just need to initialize accumulator and product and you should be good. What values do you think you should set them to?
Thanks for reply. I initialized them to 0 (I have ammended my code in the original post) and I now can't run the program at all. I get several errors: