# Help with finding the sum of odd numbers using an array

Page 1 of 1

## 3 Replies - 686 Views - Last Post: 06 August 2015 - 01:43 AMRate 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=379765&amp;s=b199192cee1fd943331149b91de261f9&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 xxthefragilex

Reputation: 0
• Posts: 3
• Joined: 15-September 13

# Help with finding the sum of odd numbers using an array

Posted 05 August 2015 - 02:54 AM

Hello there. I'm having a bit of a problem making this code work. I know how to look through an array and find out whether it's odd by using %. In order for this to work, I have a few files being used by the compiler. What I'm confused about is when I call the isOdd function in sumOdds.cpp (line 12), the result will sometimes not work. I found this out on my first attempt by playing with an array {1,2,3,4,5} in sumOddTests.cpp When using the isOdd function for {1,2,3,4,5}, all the elements of the array are added. However, if I don't call the isOdd function and instead just perform in sumOdds the calculation that is defined in utility.cpp, the sum is added correctly and all tests pass for any array. I've probably included more code than necessary. I've been looking at this code for more time than I'd like to admit. What am I doing wrong?

Since I can't upload, here's the code:

sumOdds.cpp
```#include "arrayFuncs.h"
#include <cstdlib>
#include <iostream>

#include "utility.h"

int sumOdds(int a[], int size)
{
int sum = 0;
for(int i = 0; i<=size-1;i++)
{
if(isOdd(a[i]))
{
sum+=a[i];
}
}
return sum;
}

```

utility.h
```bool isOdd(int x);
bool isEven(int x);
bool isPrime(int x);

```

utility.cpp
```bool isOdd(int x) {
if(x%2 != 0)
return true;
}
bool isEven(int x) {
if(x%2 == 0)
return true;
}
bool isPrime(int x) {

bool primeCheck = true;

if( x <= 1 )
return false;
else
for(int i=2; i<x;i++)
{
if(x%i==0)
primeCheck = false;
}
return primeCheck;
}
```

sumOddTests.cpp
```#include "arrayFuncs.h"
#include "tddFuncs.h"
#include "utility.h"
int main() {

// Fill this in with exactly four tests that determine whether your
// code for sumOdds() correctly sums up all the odd numbers in
// arrays of various sizes.  Include both positive, negative and
// zero values in your test arrays.
int fiveThrees[5]={3,3,3,3,3};
assertEquals(15,sumOdds(fiveThrees,5),"sumOdds(fiveThrees,5)");

int zeros[3]={0,0,0};
assertEquals(0,sumOdds(zeros,3),"sumOdds(zeros,3)");

int fiveInts[5]={12,3,4,-9,5};
assertEquals(-1,sumOdds(fiveInts,5),"sumOdds(fiveInts,5)");

int meaning[]={42};
assertEquals(0,sumOdds(meaning,1),"sumOdds(meaning,1)");

/*  assertEquals(9,sumOdds(array1,5),"sumOdds(array1)");

assertEquals(81,sumOdds(array2,6),"sumOdds(array2)");
assertEquals(631,sumOdds(array3,6),"sumOdds(array3)");
assertEquals(0,sumOdds(array4,6),"sumOdds(array4)");
assertEquals(62,sumOdds(array5,6),"sumOdds(array5)");
*/
return 0;
}

```

arrayFuncs.h
```#ifndef ARRAYFUNCS_H
#define ARRAYFUNCS_H

#include <iostream>
#include <string>
#include <sstream> // for ostringstream

std::string intToString(int i);
std::string arrayToString(int a[], int size);

int sumOfArray(int a[], int size);
int minOfArray(int a[], int size);
int maxOfArray(int a[], int size);
int countEvens(int a[], int size);
int sumOdds(int a[], int size);
int countPrimes(int a[], int size);

#endif // ARRAYFUNCS_H

```

tddfuncs.h
```#ifndef TDDFUNCS_H
#define TDDFUNCS_H

#include <string>

void assertEquals(std::string expected,
std::string actual,
std::string message);

void assertEquals(int expected,
int actual,
std::string message);

#endif // TDDFUNCS_H

```

tddfuncs.cpp
```#include "tddFuncs.h"
#include <iostream>
using std::cout;
using std::endl;

void assertEquals(std::string expected,
std::string actual,
std::string message="") {
if (expected==actual) {
cout << "PASSED: " << message << endl;;
} else {
cout << "   FAILED: " << message << endl
<< "   Expected:[\n"
<< expected << "] actual = [\n"
<< actual << "]\n" << endl;
}
}

void assertEquals(int expected,
int actual,
std::string message="") {
if (expected==actual) {
cout << "PASSED: " << message << endl;;
} else {
cout << "   FAILED: " << message << endl
<< "     Expected: " << expected << " Actual: " << actual << endl;
}
}
```

Is This A Good Question/Topic? 0

## Replies To: Help with finding the sum of odd numbers using an array

### #2 ndc85430

• I think you'll find it's "Dr"

Reputation: 801
• Posts: 3,268
• Joined: 13-June 14

## Re: Help with finding the sum of odd numbers using an array

Posted 05 August 2015 - 04:22 AM

When I try to compile your utility.cpp, I get

\$ g++ -Wall -c utility.cpp -o utility.o
utility.cpp: In function ‘bool isOdd(int)’:
utility.cpp:4:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
utility.cpp: In function ‘bool isEven(int)’:
utility.cpp:8:1: warning: control reaches end of non-void function [-Wreturn-type]
}

You should be taking care of the case that you don't enter the if blocks in those functions by ending them with explicit return statements. I've not looked too much at the rest of the code, though.

### #3 darek9576

• D.I.C Lover

Reputation: 203
• Posts: 1,731
• Joined: 13-March 10

## Re: Help with finding the sum of odd numbers using an array

Posted 05 August 2015 - 05:41 AM

```bool isOdd(int x) {
if(x%2 != 0)
return true;
}
bool isEven(int x) {
if(x%2 == 0)
return true;
}

```

What if those if statements return false. You need to return a boolean value on every execution path.

I also do not think you are allowed to use std::array<> or std::vector<> but if you were, then you could do:

```std::vector<int> numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int sum = 0;
std::for_each(std::begin(numbers), std::end(numbers), [&sum](const int & element){ if (element % 2 != 0) sum += element; });
std::cout << "The sum of odd elements is: " << sum << std::endl;

```

### #4 xxthefragilex

Reputation: 0
• Posts: 3
• Joined: 15-September 13

## Re: Help with finding the sum of odd numbers using an array

Posted 06 August 2015 - 01:43 AM

How foolish of me. Thanks to both of you.