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.
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<elements;i++)
{
accumulator += array[i];
}
cout << "\n\nThe sum of all the integers in your array is: " << accumulator;
break;
case 2:
for (i=0;i<elements;i++)
{
product*= array[i];
}
cout << "\n\nThe product of all the integers in your array is: " << product;
break;
default:
cout << "\n\nYou didn't enter a 1, 2 or 3.";
}
return;
}
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.
#include <iostream>
#include <cstdio>
#include <cstdlib>
usingnamespace 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<elements;i++)
{
int value;
cout << "\nEnter a value for element " << i+1 << ": ";
cin >> value;
if (value<0)
{
exit;
}
array[i] = value;
}
return;
}
void displayValues(int array[], int elements)
{
int i;
for (i=0;i<elements;i++)
{
cout << "\n Element " << i+1 << " is: " << array[i] << "\n";
}
return;
}
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<elements;i++)
{
accumulator += array[i];
}
cout << "\n\nThe sum of all the integers in your array is: " << accumulator;
break;
case 2:
for (i=0;i<elements;i++)
{
product*= array[i];
}
cout << "\n\nThe product of all the integers in your array is: " << product;
break;
default:
cout << "\n\nYou didn't enter a 1, 2 or 3.";
}
return;
}
char yesOrNo()
{
char yesorno;
cout << "Would you like to perform another operation on your array?"
<< " (Y/N): ";
cin >> yesorno;
return yesorno;
}
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?
Yeah I thought that that might be a problem. You're declaring and initializing variables inside the switch statement, but it isn't in any of the cases. Any code outside of the cases will be ignored and (apparently) cause an error. Just declare/initialize i, accumulator, and product right before the switch statement.
Also, initializing product to 0 won't work. Since all you do with product is multiply it by everything in the array, and anything times 0 is 0, it will still be 0 by the end.
Thanks, I see what the problem was now :)
Control was skipping the parts between the switch() statement and case 1, I forgot that's how switches worked. And product being set to 0 instead of 1 wasn't doing me any favours either hehe.