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.

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
30
31
32
33
34
35
36
37
38
39
40
41

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.

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <iostream>
#include <cstdio>
#include <cstdlib>

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<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;
}



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:

error: Jump to case label
error: Crosses initialization of 'int product'
error: Crosses initialization of 'int accumulator'
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.
Last edited on
Topic archived. No new replies allowed.