# Finding Odd in Array

Page 1 of 1

## 3 Replies - 13698 Views - Last Post: 18 April 2007 - 07:17 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=26619&amp;s=f32c703cc318f4e9d6fb937ae819fe5b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 mccannfan

Reputation: 0
• Posts: 8
• Joined: 02-April 07

# Finding Odd in Array

Posted 15 April 2007 - 06:52 PM

I need to write a function that takes an array of int as a parameter and returns the sum of odd numbers in the array.
I have no problem writing a function that takes an array of int as a parameter and returns the sum in the array; however, using sumOdd to find the odd numbers in the array either does not work or I am not writing it correct. So, my problem is not finding the sum but finding the sum of odd numbers.

Here is my code which works for finding the sum:
```// Module 8.2: ques8dot2.cpp
// Compute the sum of the elements of the array

#include <iostream>
using namespace std;

int main()
{
const int arraySize = 16;
int x[ arraySize ] = { 25, 10, 55, 33, 27, 7, 9, 81, 14, 39};
int total = 0;

// sum contents of array x
for ( int i = 0; i < arraySize; i++)
total += x[ i ];

cout << "Total of array elements:" << total << endl;
return 0;

}
```

Here, I attempted to find the sum of odd but it does not work:
```#include <iostream>
using namespace std;
int main()
{
const int arraySize = 16;
int x[ arraySize ] = { 25, 10, 55, 33, 27, 7, 9, 81, 14, 39};
int sumOdd = 0;

// sum contents of array x
for ( int i = 0; i < arraySize; i++)
sumOdd += x[ i ];

cout << "Sum of odd integers of array elements:" << sumOdd << endl;
return sumOdd;

}
```

Any help would be appreciated......

Is This A Good Question/Topic? 0

## Replies To: Finding Odd in Array

• g+ + -o drink whiskey.cpp

Reputation: 250
• Posts: 13,507
• Joined: 12-July 02

## Re: Finding Odd in Array

Posted 15 April 2007 - 07:04 PM

First things first...you have declared your array to be of size 16, you are assigning it far less than that many elements.

Second, you only wish to add the number to the total if it is an odd number - you are adding all the items. To identify an odd number, you can use the modulus operator:

http://www.cprogramm...al/modulus.html

If the result of the number in question being modded with 2 is zero, you know the number is even...therefore is the result is not 0, you know the number is odd, and should be added to the total.

```if(x[i]%2!=0)
total += x[i];

```

### #3 mccannfan

Reputation: 0
• Posts: 8
• Joined: 02-April 07

## Re: Finding Odd in Array

Posted 18 April 2007 - 06:25 PM

Amadeus, on 15 Apr, 2007 - 07:04 PM, said:

First things first...you have declared your array to be of size 16, you are assigning it far less than that many elements.

Second, you only wish to add the number to the total if it is an odd number - you are adding all the items. To identify an odd number, you can use the modulus operator:

http://www.cprogramm...al/modulus.html

If the result of the number in question being modded with 2 is zero, you know the number is even...therefore is the result is not 0, you know the number is odd, and should be added to the total.

```if(x[i]%2!=0)
total += x[i];

```

I made some adjustments; however I now receive the error 14 `i' undeclared (first use this function) of course this repeats for every 'i'

My changed code is:

#include <iostream>
using namespace std;
int main()
{
// int sumOdd;
int sumOdd=0;

const int arraySize = 10;
int x[ arraySize ] = { 25, 10, 55, 33, 27, 7, 9, 81, 14, 39};

// sum contents of array x
x[i]%2!=0

for (int i=0;i<arraySize;i++) {
if (x[i] % 2 != 0)
sumOdd += x[i];
}
cout << sumOdd << " sum of odd number ";

return 0;

}

Thanks for your help and if you can offer any help I would appreciate it.

• g+ + -o drink whiskey.cpp

Reputation: 250
• Posts: 13,507
• Joined: 12-July 02

## Re: Finding Odd in Array

Posted 18 April 2007 - 07:17 PM

See, the problem is you've simply copied and pasted the example given...it was meant to demonstrate the technique to be used, and was not meant as something to be stuck right into your program.
```x[i]%2!=0

```

that line in and of itself means nothing...why do you have it stuck in the middle of the code?

As for the error itself, if it says i is undeclared, what does that tell you to do?