Adeel Younas
Adeel Younas

Reputation: 41

How to get sum of odd and multiplication of even element in array

How to get the sum of all odd numbers and product of positive even numbers after getting values my program gives error and exit. How to resolve this issue and why this kind of error occurs and if i enter values less than five it gives memory address

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
    int n,sum=0,mul=0,i,j;
    cout<<"How many numbers you want to enter?";
    cin>>n;
    int arr[n],odd[]={0,0,0,0,0},even[5];
    for(i=0;i<n;i++)
    {
        cout<<"Enter Val "<<i+1<<": ";
        cin>>arr[i];
    }
    for(j=0;j<n;j++)
    {
        if(arr[j]%2==0)
        {
            even[j]=arr[j];
        }
        else
        {
            odd[j]=arr[j];
        }
    }
    for(j=0;j<n;j++)
    {
        sum+=odd[j];

        mul*=even[j];

    }
    cout<<"Sum of Odd "<<sum<<endl;
    cout<<"Product of Even "<<mul<<endl;

    return 0;
}

after initializing array odd the sum of odd works, but the product still don't!

Upvotes: 0

Views: 3266

Answers (2)

Amy
Amy

Reputation: 45

So, I agree with the answer provided by Barry. However, if this is, say, a school assignment where you are required to use arrays, you will still need to use them.

If this is the case, to address this issue:

"Furthermore, you're always writing the jth number to either odd[j] or even[j]... which means that if the first number was odd and the second was even, you now wrote a value to even[1] but never wrote anything to even[0]. You're not populating your arrays correctly, and you end up with several uninitialised ints which make your final computations undefined behavior."

Consider running the user-defined-length array (that is, arr[n]) through two different tests for array items: one finding even elements and one finding odd elements.

Unless otherwise specified, you should probably not arbitrarily limit the array size of even[] and odd[] to just five items.

Upvotes: 0

Barry
Barry

Reputation: 304182

There are a few problems in your code. arr[n] is non-standard C++, and you're only allowing yourself 5 slots for your odd and even numbers (what if the user wants to input 20 numbers?) Furthermore, you're always writing the jth number to either odd[j] or even[j]... which means that if the first number was odd and the second was even, you now wrote a value to even[1] but never wrote anything to even[0]. You're not populating your arrays correctly, and you end up with several uninitialised ints which make your final computations undefined behavior.

The easy solution is to just sidestep all of those problems by not using arrays at all, since you don't really need them:

int odd = 0, even = 1;
cin >> n;

int x;
for (int i = 0; i < n; ++i) {
    // get the next value
    cin >> x;

    // either multiply or add it as appropriate
    if (x % 2 == 0) {
        even *= x;
    }
    else {
        odd += x;
    }
}

Upvotes: 3

Related Questions